2017-05-11 27 views
2

嗨,遊標提取聲明,呼籲第一行repetedly

我創建了簡單的SELECT語句的遊標它只有10行 當我去後打印,而@@ FETCH_STATUS = 0,那麼它遞歸調用 僅第一行,執行沒有停止。 我不知道爲什麼越來越只顯示第一行repetedly光標不移動到第二行

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 
+3

你必須在你的while循環中重新獲取,否則你永遠不會得到下一行 – Adam

+0

遊標被大多數人鄙視,因爲它們效率非常低 - 它們有它們的用途,但它們很少和很遠......你最好在這裏使用不同的方法,這取決於你需要做什麼。 – JiggsJedi

回答

2

你必須有一個取...下一while循環也在裏面......

... 
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 
... 
+0

當你運行你的select語句時,你的查詢返回多少行?你的光標語法似乎沒問題... – Kevin

+0

@JiggsJedi它的工作原理錯過了開始和結束 – sachin