SQL Server 2008R2:无法构造嵌套的游标

时间:2015-11-06 09:22:10

标签: sql sql-server tsql cursor

我有嵌套游标的问题,要么是无限循环,要么是 错误是

  

Msg 16916,Level 16,State 1,Line 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,但我总是以第一顺序循环。所以我找不到这个循环。

谢谢你的帮助 迈克尔

1 个答案:

答案 0 :(得分:1)

不要使用游标。带有计数表的简单JOIN就足够了:

CREATE TABLE #tab(orders_ID INT,
orders_products_ID INT, customers_Lastname VARCHAR(100), quantity INT);

INSERT INTO #tab
VALUES(1, 1, 'Mark', 1),(1, 2, 'Mary', 3),(2, 3, 'Paul', 2),(3, 4, 'Linda', 2);


WITH tally(N) AS
(
SELECT TOP 1000 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM sys.columns s1
CROSS JOIN sys.columns s2
)
SELECT t.orders_ID, t.orders_products_ID , t.customers_Lastname , 1 
FROM #tab t
JOIN tally t2
  ON t2.N <= t.quantity
ORDER BY t.orders_ID;

LiveDemo

修改

  

Tally表 - 就像任何其他表一样,但只有一列   序号,从1(或0)到某些N(int)的值   号。

当然,你可以使用 many methods

使用子查询/派生表而不是“真实”表