我正在嘗試創建一個程式。當我在設定@flag 之后設定@age 時,出現了問題。但要么我試圖洗掉 set @age,要么set @flag它會起作用。
錯誤:else附近有語法錯誤
我想知道這是什么原因造成的。感謝幫助。
select * from sc
if exists(select *
from sysobjects
where name='pro5' and type ='P')
drop procedure pro5
go
create procedure pro5 @stuNo char(10),@flag int output
as
declare @age int
begin
set @age=0
set @flag=1
if exists(select *
from sc
where sno=@stuNo)
set @flag=0
set @age=0
else
delete
from student
where sno=@stuNO
end
return @age
go
declare @flag int
exec pro5 '991102',@flag output
if @flag=1
print 'success'
else
print 'failure'
uj5u.com熱心網友回復:
您需要在陳述句中定義 a beginand endaround 所有行,if默認情況下僅假定單行屬于“if”塊:
if exists(select * from sc where sno=@stuNo)
begin
set @flag=0
set @age=0
end
else
delete
from student
where sno=@stuNO
但是,您可以稍微簡化一下,因為您不需要設定age已經為 0 的值。我假設您實際上可能需要將 @age 設定為1,這是您的代碼的精簡示例,您可以根據需要進行修改:
create procedure pro5 @stuNo char(10), @flag int output
as
declare @age int = 0;
if exists(select * from sc where sno = @stuNo)
set @flag = 0
else
begin
delete from student where sno = @stuNO;
set @flag = 1;
end
return @age;
uj5u.com熱心網友回復:
稍微改變了語法:
create procedure pro5 (
@stuNo char(10),
@flag int output
)
as
begin
declare @age int
set @age=0
set @flag=1
if exists(select *
from sc
where sno=@stuNo)
begin
set @flag=0
set @age=0
end
else
delete
from student
where sno=@stuNO
return @age
end
go
declare @flag int
exec pro5 '991102',@flag output
if @flag=1
print 'success'
else
print 'failure'
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/483414.html
標籤:sql服务器
上一篇:如何更改鍵盤快捷鍵-F5到Ctrl Enter在SQLServerManagementStudio中執行SQL命令
