MS SQL 循环中循环
MS SQL Loop within a loop
我有两个 tables - 一个叫做 #final 并且有很多客户数据(所有信息的综合)。十几列左右 - first_name, middle_name, last_name, address, attendees
等。我还有第二个 table
create table #diff_temp
(
customer_no int,
num_reception int,
num_guests int,
diff int
)
第二个 table 填充客户 ID、总接待和客人门票的方式以及 diff = reception-guests
之间的区别
我需要做的是 运行 一个循环,并将名称、地址信息插入#final 的次数等于每条记录的差异。
例如。
#diff_Temp
有这样的数据。
customer_no num_reception num_guests diff
1 5 1 4
2 12 10 2
3 3 1 2
4 32 20 12
5 12 10 2
6 8 6 2
我需要做的是,客户 1 有一个循环,循环 运行s 4 次,数据输入 #final
4 次。对于客户 2,循环将 运行 2 次,对于客户 4,它将 运行 12 次,依此类推。
对于每个客户,diff 列中的值循环 运行s 次。然后循环根据大型 sql 查询将数据插入#final。
我似乎无法弄清楚如何使游标或循环在这里工作。
这是我得到的脚本 - 它 运行 但没有执行任何操作。
当 i 运行 内部游标仅占用行数 (6) 并在每行中输入 6 次。不是我想要的。
新更新的脚本:
DECLARE @Iteration INT = 1 -- Loop
DECLARE @diff INT = 1 -- Cursor
DECLARE @owner_customer_no INT -- Cursor
BEGIN
DECLARE loop_cursor CURSOR FOR
SELECT owner_customer_no, diff
FROM #diff_temp
OPEN loop_cursor
FETCH NEXT FROM loop_cursor INTO @owner_customer_no, @diff
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Iteration = 1
WHILE @Iteration <= @diff
BEGIN
insert into #final
select distinct
e.customer_no,
0 as guest_cust_no,
h.fname,
...
where e.campaign_no = 1119
and sc.coding_scenario = 2
PRINT @Iteration
PRINT @diff
PRINT @owner_customer_no
SET @Iteration = @Iteration + 1
END
FETCH NEXT FROM loop_cursor INTO @owner_customer_no, @diff
END
CLOSE loop_cursor
DEALLOCATE loop_cursor
END
此代码生成以下内容
(6 row(s) affected)
iteration 1
diff 4
customer 1
(6 row(s) affected)
iteration 2
diff 4
customer 1
(6 row(s) affected)
iteration 3
diff 4
customer 1
(6 row(s) affected)
iteration 4
diff 4
customer 1
(6 row(s) affected)
iteration 1
diff 2
customer 2
(6 row(s) affected)
iteration 2
diff 2
customer 2
每个 iteration/loop 插入 6 行 - 我想要它做的是插入 1 行。
声明变量@diff
时,它的值为NULL。
那你马上尝试在循环中使用它:
DECLARE @diff INT, @owner_customer_no INT -- Cursor
WHILE @Iteration <= @diff
但是由于 @Iteration
永远不会 "less than or equal to" NULL,WHILE 循环不会执行。
编辑:实际上,您需要移动这个循环:
WHILE @Iteration <= @diff
BEGIN
...
SET @Iteration = @Iteration + 1
END
到您的光标内部,以便它对 #diff_temp
中的每一行执行。
编辑 2:
这一行之后,
FETCH NEXT FROM loop_cursor INTO @owner_customer_no, @diff
将 @Iteration
设置回 1,这样内部循环将正确执行 #diff_temp
的下一行。
我有两个 tables - 一个叫做 #final 并且有很多客户数据(所有信息的综合)。十几列左右 - first_name, middle_name, last_name, address, attendees
等。我还有第二个 table
create table #diff_temp
(
customer_no int,
num_reception int,
num_guests int,
diff int
)
第二个 table 填充客户 ID、总接待和客人门票的方式以及 diff = reception-guests
我需要做的是 运行 一个循环,并将名称、地址信息插入#final 的次数等于每条记录的差异。
例如。
#diff_Temp
有这样的数据。
customer_no num_reception num_guests diff
1 5 1 4
2 12 10 2
3 3 1 2
4 32 20 12
5 12 10 2
6 8 6 2
我需要做的是,客户 1 有一个循环,循环 运行s 4 次,数据输入 #final
4 次。对于客户 2,循环将 运行 2 次,对于客户 4,它将 运行 12 次,依此类推。
对于每个客户,diff 列中的值循环 运行s 次。然后循环根据大型 sql 查询将数据插入#final。
我似乎无法弄清楚如何使游标或循环在这里工作。
这是我得到的脚本 - 它 运行 但没有执行任何操作。 当 i 运行 内部游标仅占用行数 (6) 并在每行中输入 6 次。不是我想要的。
新更新的脚本:
DECLARE @Iteration INT = 1 -- Loop
DECLARE @diff INT = 1 -- Cursor
DECLARE @owner_customer_no INT -- Cursor
BEGIN
DECLARE loop_cursor CURSOR FOR
SELECT owner_customer_no, diff
FROM #diff_temp
OPEN loop_cursor
FETCH NEXT FROM loop_cursor INTO @owner_customer_no, @diff
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Iteration = 1
WHILE @Iteration <= @diff
BEGIN
insert into #final
select distinct
e.customer_no,
0 as guest_cust_no,
h.fname,
...
where e.campaign_no = 1119
and sc.coding_scenario = 2
PRINT @Iteration
PRINT @diff
PRINT @owner_customer_no
SET @Iteration = @Iteration + 1
END
FETCH NEXT FROM loop_cursor INTO @owner_customer_no, @diff
END
CLOSE loop_cursor
DEALLOCATE loop_cursor
END
此代码生成以下内容
(6 row(s) affected)
iteration 1
diff 4
customer 1
(6 row(s) affected)
iteration 2
diff 4
customer 1
(6 row(s) affected)
iteration 3
diff 4
customer 1
(6 row(s) affected)
iteration 4
diff 4
customer 1
(6 row(s) affected)
iteration 1
diff 2
customer 2
(6 row(s) affected)
iteration 2
diff 2
customer 2
每个 iteration/loop 插入 6 行 - 我想要它做的是插入 1 行。
声明变量@diff
时,它的值为NULL。
那你马上尝试在循环中使用它:
DECLARE @diff INT, @owner_customer_no INT -- Cursor
WHILE @Iteration <= @diff
但是由于 @Iteration
永远不会 "less than or equal to" NULL,WHILE 循环不会执行。
编辑:实际上,您需要移动这个循环:
WHILE @Iteration <= @diff
BEGIN
...
SET @Iteration = @Iteration + 1
END
到您的光标内部,以便它对 #diff_temp
中的每一行执行。
编辑 2:
这一行之后,
FETCH NEXT FROM loop_cursor INTO @owner_customer_no, @diff
将 @Iteration
设置回 1,这样内部循环将正确执行 #diff_temp
的下一行。