0

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

1
  • 3
    A simple join between your current table and a numbers table should produce the output you want whilst using zero cursors. Why are you using cursors? Commented Nov 6, 2015 at 9:24

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot, works perfect, although I do not understand it yet...! I have to learn about 'tally tables' - never heard before, but seems to be very helpful. rgds Michael
sorry, I am still fighting with stackoverflow page, which is very difficult to understand what to do for a non native Speaker like me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.