Sql course in an infinite loop. What is wrong with this code?

Hi, I am trying to loop for each employee id in a table.

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 

This is my request in a stored procedure. What is wrong with that? it gives me the first employee id in an infinite loop. If I checked while @@ FETCH_STATUS = 1, I did not give a way out. just saying Command(s) completed successfully.

+4
source share
1 answer

You need to add fetch command after selecting

 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 
+6
source

Source: https://habr.com/ru/post/1490692/


All Articles