我想知道我应该使用什么样的SQL Server,Table1中的foreach记录在Table2和Table3中插入数据?
temp Table1(15 columns + ID_autoinc)
通过数据进行迭代并插入实际
Table2(10 colums + ID_autoinc) and real Table3(6 columns + ID_autoinc)?
每个记录也同时在表2中插入数据,我需要身份将其插入到表3。
Conceptualy我需要:
SELECT * FROM Table1 into #TempTable1
FOREACH-> Record in #TempTable1 {
INSERT INTO Table2(col1 ... col10) VALUES(#TempTable1.col1 ... #TempTable1.col10)
DECLARE @Table2Identity as int
@Table2Identity = SCOPE_IDENTITY()
INSERT INTO Table3(col1 ... col6) VALUES(@Table2Identity, #TempTable1.col11 ... #TempTable1.col15)
}
什么是插入到从临时表中各表的标准是什么? 您可以使用merge.knowing样本数据和条件很重要。 – KumarHarsh
不要循环。 '插入表2(col1,col2,col3)从table1'中选择col1,col2,col3。重复table3。 – Blorgbeard
我更新了我的问题,即时通讯仍然不确定是否解释清楚。提前致谢。 – user1281760