我需要根据产品表中每条记录的条件,在表{strong>产品中将多行插入表 ProductImages ,并在ProductImages table
中插入3次
以下是product table
Sno | Productid
1 | ABC
2 | XYZ
3 | PQR
我想将ProductImages表作为
Sno | ProductId | ImageId
1 | ABC | ABC_Small
2 | ABC | ABC_Big
3 | ABC | ABC_Biggest
4 | XYZ | XYZ_Small
5 | XYZ | XYZ_Big
6 | XYZ | XYZ_Biggest
是否可以从与Productid的父关系表中获取上述多条记录?请帮我。感谢
答案 0 :(得分:4)
declare @Product table(Sno int, Productid char(3))
declare @ProductImages Table(Sno int identity(1,1), ProductId char(3), ImageId varchar(20))
insert @Product
values(1, 'ABC'),(2, 'XYZ'),(3, 'PQR')
insert @ProductImages (ProductId, ImageId)
select p.ProductId,p.ProductId+ '_' +a.plus from @Product p
cross apply
(values ('Small'), ('Big'), ('Biggest')) a(plus)
select * from @ProductImages
编辑sql 2000
insert @Product values(1, 'ABC')
insert @Product values(2, 'XYZ')
insert @Product values(3, 'PQR')
insert @ProductImages (ProductId, ImageId)
select p.ProductId,p.ProductId+ '_' +a.plus from @Product p
join
(select 'Small' plus
union all select 'Big'
union all select 'Biggest') a(plus)
on 1 = 1
结果:
Sno ProductId ImageId
1 ABC ABC_Small
2 ABC ABC_Big
3 ABC ABC_Biggest
4 XYZ XYZ_Small
5 XYZ XYZ_Big
6 XYZ XYZ_Biggest
7 PQR PQR_Small
8 PQR PQR_Big
9 PQR PQR_Biggest
答案 1 :(得分:3)
试试这个
insert ProductImages (ProductId, ImageId)
select
p.Productid,
p.Productid+'_'+size
from Products p
cross join (
Select size
From (
Values
('Small'),
('Big'),
('Biggest')
) s(size)
) sizes