試試
insert into B(ID,FromID,其他欄位)
select isNull((select max(ID) from B),0)
+ Row_Number()Over(Order by a.ID)
,a.ID as FromID
,b.其他欄位
from A,b
CREATE TABLE TestX
(
AA nvarchar(40),
BB nvarchar(40),
CC nvarchar(40)
);
INSERT into TestX
values
(N'A1',N'B1',N'C1'),
(N'A2',N'B2',N'C2'),
(N'A3',N'B3',N'C3'),
(N'A4',N'B4',N'C4'),
(N'A5',N'B5',N'C5');
CREATE TABLE TestY
(
DD nvarchar(40),
EE nvarchar(40),
FF nvarchar(40)
);
INSERT into TestY
values
(N'D1',N'E1',N'F1'),
(N'D2',N'E2',N'F2');
CREATE TABLE TestZ
(
AA nvarchar(40),
BB nvarchar(40),
CC nvarchar(40),
DD nvarchar(40),
EE nvarchar(40),
FF nvarchar(40)
);
INSERT TestZ (AA,BB,CC,DD,EE,FF)
SELECT AA,BB,CC,DD,EE,FF
FROM TestX,TestY
ORDER BY AA,BB,CC,DD,EE,FF
如果你是想製作5*500筆資料,
可以試試看CROSS JOIN:
1.新建一張資料表
2.INSERT(SELECT表A CROSS JOIN 表B)