有没有办法插入输出的输出

时间:2015-10-29 12:22:46

标签: sql

我试图找到一种方法来获取已经输出的数据的输出,然后重新插入(输出/插入x 2的种类)

以下是我将使用的代码,有人可以告诉我有没有办法解决这个问题?

INSERT INTO table1
(
Field1, Field2, Field3
)
OUTPUT
inserted.values1, inserted.values2, insertedvalues3
INTO table2
(
Field1, Field2, Field3
)

OUTPUT 
inserted.values1, inserted.values2, insertedvalues3 -- from table2
INTO table3
(
Field1, Field2, Field3
)

SELECT values1, values2, values3
FROM MasterTable

1 个答案:

答案 0 :(得分:0)

创建一个临时表,然后 - OUTPUT INTO - 进入该临时表。 然后通过从临时表中选择,将值插入table2和table3。

CREATE TABLE table1(Field1 INT, Field2 int, Field3 int);
CREATE TABLE table2(Field1 INT, Field2 int, Field3 int);
CREATE TABLE table3(Field1 INT, Field2 int, Field3 int);

-- Our temporary table
CREATE TABLE #results(Field1 INT, Field2 int, Field3 int);

INSERT INTO table1(Field1, Field2, Field3)
    OUTPUT inserted.Field1, inserted.Field2, inserted.Field3
    INTO #results(Field1, Field2, Field3)
VALUES (1, 2, 3)

-- Now insert the "inserted" values into the two target tables
INSERT INTO dbo.table2( Field1, Field2, Field3 )
SELECT Field1, Field2, Field3 FROM #results;

INSERT INTO dbo.table3( Field1, Field2, Field3 )
SELECT Field1, Field2, Field3 FROM #results;