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