我想基于select语句在表中一次插入3行..
考虑查询
insert into tblTemp
(
a,b
)
select a, b from tblTemp2
这让我在tblTemp ..
中插入一行我的要求是为每个插入的b添加3行迭代值a,a + 1,a + 2。
答案 0 :(得分:4)
使用交叉连接生成额外的行(通过笛卡尔积),而不是使用UNION ALL查询tblTemp2三次
insert into tblTemp (a, b)
select a + offset, b
from
tblTemp2
cross join
(SELECT 0 AS offset UNION ALL SELECT 1 UNION ALL SELECT 2) foo
答案 1 :(得分:1)
insert into tblTemp ( a,b )
select a, b from tblTemp2 UNION ALL
select a+1, b from tblTemp2 UNION ALL
select a+2, b from tblTemp2 UNION ALL
答案 2 :(得分:1)
Insert Into tblTemp (a,b)
SELECT T.a + T1.Num , T.B
FROM
tblTemp2 T
CROSS JOIN
(
SELECT 0 as Num
UNION ALL
SELECT 1 as Num
UNION ALL
SELECT 2 as Num
) T1
答案 3 :(得分:0)
declare @rbr int
set @rbr=0
while @rbr<3
begin
insert into tblTemp2
select a+@rbr, b from tblTemp2
set @rbr=@rbr+1
end