SQL 服务器:我想知道 else 附近有什么语法错误
SQL Server: I wonder what syntax error is near else
我正在尝试创建一个程序。当我在设置@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'
稍微更改了语法:
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'
您需要在 if
语句中的所有行周围定义 begin
和 end
,默认情况下仅假定一行属于“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
并且这个是您的代码的 cut-down 示例,您可以根据需要进行修改:
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;
我正在尝试创建一个程序。当我在设置@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'
稍微更改了语法:
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'
您需要在 if
语句中的所有行周围定义 begin
和 end
,默认情况下仅假定一行属于“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
并且这个是您的代码的 cut-down 示例,您可以根据需要进行修改:
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;