添加外部数据并将单行拆分为多行
Add external data and split single row into many rows
我在数据库中有一个 table,其中一个 ID 有 1 行,根据要求我需要添加数据标志 A,B,C
所以现在在临时 table 我单个 ID 需要 3 行。
数据库中的数据
ID product
---------------
1 computers
2 Laptops
3 Speakers
现在我想将数据插入临时 table as
ID product Flag //Flag is user defined and will be only 3 any time
----------------------
1 computers A
1 computers B
1 computers C
2 Laptops A
2 Laptops B
2 Laptops C
3 Speakers A
3 Speakers B
3 Speakers C
使用VALUES
:
SELECT YT.ID,
YT.Product
V.C AS Flag
FROM YourTable YT
CROSS APPLY (VALUES('A'),('B'),('C')) V(C);
我在数据库中有一个 table,其中一个 ID 有 1 行,根据要求我需要添加数据标志 A,B,C
所以现在在临时 table 我单个 ID 需要 3 行。
数据库中的数据
ID product
---------------
1 computers
2 Laptops
3 Speakers
现在我想将数据插入临时 table as
ID product Flag //Flag is user defined and will be only 3 any time
----------------------
1 computers A
1 computers B
1 computers C
2 Laptops A
2 Laptops B
2 Laptops C
3 Speakers A
3 Speakers B
3 Speakers C
使用VALUES
:
SELECT YT.ID,
YT.Product
V.C AS Flag
FROM YourTable YT
CROSS APPLY (VALUES('A'),('B'),('C')) V(C);