我有一个嵌套的游标的问题,无论它是一个无限循环或 错误是的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'应出示您想同时使用输出的“真正的”表子查询/派生表*零*游标。你为什么使用游标? –