简体   繁体   中英

SQL Server 2008R2: cannot construct nested Cursors

I have a problem with nested Cursors, either it is an endless Loop or the error is

Msg 16916, Level 16, State 1, Line 1
A cursor with the name 'quantity_Cursor' does not exist.

This is my latest Version of my code, which leads to this error.

The table look like this

orders_ID  orders_products_ID customers_Lastname  quantity   
-----------------------------------------------------------
1           1                  Mark                  1   
1           2                  Mary                  3  
2           3                  Paul                  2  
3           4                  Linda                 2  

So what I ned to achive is a split into a table 'ordered goods' where all quantities are 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  

Code:

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

I tried a lot of putting the Fetch Next from orders_Cursor , but i always Loop in the first order. so I cannot find out of this Loop.

Thanks your help Michael

Do not use cursor for this. Simple JOIN with tally table is sufficient:

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

EDIT:

Tally table - is like any other table but having a single column of sequential numbers, values starting from 1 (or 0) to some N (int) number.

Of course you can use subquery/derived table instead of "real" table using many methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM