Sql游标在无限循环中。这段代码有什么问题?

时间:2013-07-10 10:51:28

标签: sql cursor infinite-loop

您好我正在尝试为表中的每个员工ID循环。

BEGIN
declare @empId nvarchar(50)
declare cur Cursor LOCAL for
select EmpId from EmployeeMaster 
open cur
fetch next from cur into @empId
     while @@FETCH_STATUS =0     
      begin      
       select  @empId  
      end   
close cur
END

这是我在存储过程中的查询。这有什么问题?它在无限循环中给我第一个员工ID。 如果我在@@ FETCH_STATUS = 1时检查,则不给出输出。只是说  Command(s) completed successfully.

1 个答案:

答案 0 :(得分:6)

您需要在选择

后添加fetch命令
BEGIN
declare @empId nvarchar(50)
declare cur Cursor LOCAL for
select EmpId from EmployeeMaster 
open cur
fetch next from cur into @empId
     while @@FETCH_STATUS =0     
      begin      
       select  @empId  
       fetch next from cur into @empId
      end   
close cur
END