在sql server 2008中,如何在声明时在游标中使用if条件?
我有如下疑问在sql server 2008中,如何在声明时在游标中使用if条件?,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有如下疑问 Declare Cur_1 cursor for if @EmployeeId <> is not null begin select EmployeeID from tbl_Employee where EEmployeeStatus='Active' and EmployeeID = @EmployeeId end else b
Declare Cur_1 cursor for
if @EmployeeId <> is not null
begin
select EmployeeID from tbl_Employee where EEmployeeStatus='Active' and EmployeeID = @EmployeeId
end
else
begin
select EmployeeID from tbl_Employee where EEmployeeStatus='Active'
end
Open Cur_1
Fetch next from Cur_1 into @EmpId
While @@FETCH_STATUS=0
Begin
end
Close Cur_1
deallocate Cur_1
为
如果@EmployeeId不为空
开始
从tbl_Employee中选择EmployeeID,其中EEmployeeStatus='Active'和EmployeeID=@EmployeeID
结束
其他的
开始
从tbl_Employee中选择EmployeeID,其中EEmployeeStatus='Active'
结束
开放curu_1
从Cur_1取下一个到@EmpId
而@@FETCH\u STATUS=0
开始
结束
关闭Cur_1
解除分配Cur_1
但它不起作用。这是一个语法错误
在if条件下显示语法错误
请帮助我……如果@EmployeeId不为空,则正确的语法必须为:
不需要before is not null,因为这里的操作符是is
。不要在游标内使用if结构,而是有两种可能的声明:
if @EmployeeId is not null
begin
Declare Cur_1 cursor for
select EmployeeID from tbl_Employee where EEmployeeStatus='Active' and EmployeeID = @EmployeeId
end
else
begin
Declare Cur_1 cursor for
select EmployeeID from tbl_Employee where EEmployeeStatus='Active'
end
Open Cur_1
Fetch next from Cur_1 into @EmpId
While @@FETCH_STATUS=0
Begin
end
Close Cur_1
deallocate Cur_1
如果@EmployeeId不是空的,那么就没有必要了,也许你需要对你的答案讲一点代码确定..下次我会关心这个的..谢谢你
if @Feild is not null
begin
Declare your_Cursor_Name cursor for
select Feild1 from Your_table where 1=1
end
else
begin
Declare your_Cursor_Name cursor for
select Feild1 from Your_table where 1=1
end
Open your_Cursor_Name
Fetch next from your_Cursor_Name into @xyz
While @@FETCH_STATUS=0
Begin
`enter code here
end
Close your_Cursor_Name