sachin
sachin

Reputation: 23

cursor fetch statement calling first row repetedly

Hi ,

i created a cursor with simple select statement which has only 10 rows when i am going to print after while @@fetch_status=0 then it recursively call only first row,the execution did not stop. i dont know why is getting display only first rows repetedly the cursor does not move to second row

  Here below is my code

  declare cur_data  cursor for

 select DISTINCT pkd.boxnumber  from packagedetail pkd
 inner join PalletDetail pld on pld.boxnumber=pkd.boxnumber
 INNER JOIN TRACKING T ON T.BOXNUM=PKD.SCANBOXID
 where pkd.shipmentlocation='NYWH' AND pld.shipmentnumber='SH0675535'

 declare @boxnumber NVARCHAR(50)

 open cur_data 

 fetch next from cur_data  into @boxnumber
 while @@fetch_status=0
 begin

 print @boxnumber

END
close cur_data
deallocate cur_data

Upvotes: 2

Views: 1636

Answers (1)

Kevin
Kevin

Reputation: 781

you have to have a fetch ... next inside your while loop also ...

 ...
 open cur_data 
 fetch next from cur_data  into @boxnumber
 while @@fetch_status=0
 BEGIN
     print @boxnumber
     fetch next from cur_data  into @boxnumber
 END
 ...

Upvotes: 2

Related Questions