SQL Server 2008R2:无法构造嵌套游标
SQL Server 2008R2: cannot construct nested Cursors
我对嵌套游标有疑问,要么是无限循环,要么是
错误是
Msg 16916, Level 16, State 1, Line 1
A cursor with the name 'quantity_Cursor' does not exist.
这是我的代码的最新版本,导致了这个错误。
table看起来像这样
orders_ID orders_products_ID customers_Lastname quantity
-----------------------------------------------------------
1 1 Mark 1
1 2 Mary 3
2 3 Paul 2
3 4 Linda 2
所以我需要实现的是拆分成 table 'ordered goods',其中所有数量都是 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
的方法,但我总是按第一顺序循环。所以我找不到这个循环。
感谢您的帮助
迈克尔
不要为此使用游标。简单的 JOIN
和计数 table 就足够了:
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;
编辑:
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.
当然你可以使用 subquery/derived table 而不是 "real" table 使用 many methods
我对嵌套游标有疑问,要么是无限循环,要么是 错误是
Msg 16916, Level 16, State 1, Line 1
A cursor with the name 'quantity_Cursor' does not exist.
这是我的代码的最新版本,导致了这个错误。
table看起来像这样
orders_ID orders_products_ID customers_Lastname quantity
-----------------------------------------------------------
1 1 Mark 1
1 2 Mary 3
2 3 Paul 2
3 4 Linda 2
所以我需要实现的是拆分成 table 'ordered goods',其中所有数量都是 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
的方法,但我总是按第一顺序循环。所以我找不到这个循环。
感谢您的帮助 迈克尔
不要为此使用游标。简单的 JOIN
和计数 table 就足够了:
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;
编辑:
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.
当然你可以使用 subquery/derived table 而不是 "real" table 使用 many methods