【发布时间】:2019-12-10 07:37:07
【问题描述】:
我有这几行 SQL:
begin
declare @eid int;
declare cursor_emp cursor for
select id from employee2;
/*open the dam curson*/
open cursor_emp;
fetch next from cursor_emp into @eid;
/*0 =The FETCH statement was successful.*/
while @@FETCH_STATUS =0
begin
if (@eid %2 =0)
begin
print ('This one is even!');
end;
/* I don't know why the repeating line necessary*/
fetch next from cursor_emp into @eid;
end;
close cursor_emp;
deallocate cursor_emp
end
它工作得很好。它应该检查 id 是否是偶数。我不明白为什么我需要两次线路
/* I don't know why the repeating line necessary*/
fetch next from cursor_emp into @eid;
在循环(while)中,如果我删除该行,那么 myloop 将永远消失!为什么要重复。
【问题讨论】:
-
你为什么要使用光标是更好的问题。
-
谢谢。我认为首先
fetch已经从列中提取所有记录。
标签: sql sql-server database database-cursor