我有一個嵌套的遊標的問題,無論它是一個無限循環或 錯誤是的SQL Server 2008 R2:無法構造嵌套遊標
消息16916,級別16,狀態1,行1
一名稱爲'quantity_Cursor'的遊標不存在。
這是我的最新版本的代碼,導致此錯誤。
表看起來像這樣
orders_ID orders_products_ID customers_Lastname quantity
-----------------------------------------------------------
1 1 Mark 1
1 2 Mary 3
2 3 Paul 2
3 4 Linda 2
所以我定義才達到一個分裂成表「訂購貨物」,其中所有的量是1
1 1 Mark 1
1 2 Mary 1
1 2 Mary 1
1 2 Mary 1
2 3 Paul 1
2 3 Paul 1
3 4 Linda 1
3 4 Linda 1
代碼:
declare orders_Cursor Cursor for
SELECT orders_ID, orders_products_ID, customers_Lastname FROM tblAlleWebshopBestellungen;
open orders_Cursor;
FETCH NEXT FROM orders_Cursor into @orders_ID_o, @orders_products_ID_o, @lastname_o;
SET @Outer_loop = @@FETCH_STATUS
WHILE @Outer_loop = 0
BEGIN
DECLARE quantity_Cursor CURSOR FOR
SELECT products_quantity FROM tblAlleWebshopBestellungen where orders_products_ID= @orders_products_ID_o;
OPEN quantity_Cursor;
--Fetch the first record from quantity_Cursor
FETCH NEXT FROM quantity_Cursor into @quantity_q;
set @Counter_q=1
WHILE @Counter_q <= @quantity_q
BEGIN
--set @text_q= convert(nvarchar,@orders_products_ID_q)+' '+ @lastname_q +' '+ convert(nvarchar,@quantity_q)
PRINT convert(nvarchar,@orders_ID_o) +' '+ convert(nvarchar,@orders_products_ID_o)+' '+ @lastname_o +' ' +convert(nvarchar,@quantity_q) +' ' + convert(nvarchar,@counter_q)
set @Counter_q= @Counter_q+1
--Fetch next record from quantity_Cursor
FETCH NEXT FROM quantity_Cursor into @quantity_q;
END
CLOSE quantity_Cursor;
DEALLOCATE quantity_Cursor;
end
FETCH NEXT FROM orders_Cursor into @orders_ID_o, @orders_products_ID_o, @lastname_o;
CLOSE orders_Cursor;
DEALLOCATE orders_Cursor;
GO
我試了很多把Fetch Next from orders_Cursor
,但我總是循環在第一順序。所以我找不到這個Loop。
感謝您的幫助 邁克爾
當前表和數字表之間的簡單'join'應出示您想同時使用輸出的「真正的」表子查詢/派生表*零*遊標。你爲什麼使用遊標? –