简体   繁体   English

将源列添加到 output 子句并插入到 select

[英]Add source columns to output clause with insert into select

My c# code calls a stored procedure and passes a tvp as an argument to the storerd procedure.我的 c# 代码调用存储过程并将 tvp 作为参数传递给 storerd 过程。 I need to insert the received data into several database tables.我需要将接收到的数据插入到几个数据库表中。

TVP schema:
TVP_ID int PRIMARY KEY IDENTITY, Col1 int, Col2 int, Col3 int.

First, I need to insert Col1 to Table1:首先,我需要将 Col1 插入到 Table1:

Table1 schema:
Table1_ID int PRIMARY KEY IDENTITY, Col1 int

Than, I need to insert all columns into Table2.然后,我需要将所有列插入到 Table2 中。

Table2 schema:
TVP_ID int, Col1 int, Col2 int, Col3 int, Table1_ID int

where TVP_ID and Table1_ID are primary keys of TVP and Table1 tables.其中 TVP_ID 和 Table1_ID 是 TVP 和 Table1 表的主键。

How can I do that?我怎样才能做到这一点?

Thanks!谢谢!

Edit: My problem is that when I have Table1 (after data was inserted to col1) the connection between TVP and Table1 is no longer exists.编辑:我的问题是,当我有 Table1 时(在将数据插入 col1 之后),TVP 和 Table1 之间的连接不再存在。 Key of TVP is TVP_ID, key of Table1 is Table1_ID, and I lost the connection between them. TVP的Key是TVP_ID,Table1的Key是Table1_ID,我失去了它们之间的联系。

I insert multiple rows, so once I have added multiple rows to Table1 how can I insert multiple rows to table2?我插入多行,所以一旦我将多行添加到 Table1,我如何才能将多行插入到 table2?

When you do an insert into MSSQL, all data inserted is temporarily stored.当您插入 MSSQL 时,所有插入的数据都会被临时存储。 You can acces the data by using an OUTPUT clause.您可以使用 OUTPUT 子句访问数据。 In this output clause you have two ways of accessing data.在此 output 子句中,您有两种访问数据的方法。 Inserted.* (or specified column) and Deleted.已插入。*(或指定的列)和已删除。 . . This way you can capture the same data that was either inserted or deleted, and route it to another table.通过这种方式,您可以捕获插入或删除的相同数据,并将其路由到另一个表。 With an Insert statement, you can only use OUTPUT inserted.同一条Insert语句,只能用OUTPUT插入。 , with Delete only OUTPUT deleted.* and with update both (an update is after all nothing but a delete, and an insert of the same row with 1 or more cells changed) , 仅删除 OUTPUT deleted.* 并同时更新两者(更新毕竟只是删除,并且插入同一行并更改了 1 个或多个单元格)

The syntax is as follows:语法如下:

Insert Into table1(table1_ID, col1)
Output inserted.tabl1_id, inserted.col1 into table2
from .... (or Values()? )

After the insert you can do an OUTPUT inserted.col1, inserted.col2 etc INTO table2.插入后,您可以执行 OUTPUT inserted.col1、inserted.col2 等 INTO table2。 For update and delete you can use the same syntax (OUTPUT deleted.* into <2nd table>)对于更新和删除,您可以使用相同的语法(OUTPUT deleted.* into <2nd table>)

If you would have to copy the data to multiple tables, you can also store it in a table variable or a temp table.如果您必须将数据复制到多个表,您也可以将其存储在表变量或临时表中。 Note, you cant store it anything but a table, even if you know it will return just a single row.请注意,您只能将它存储在一个表中,即使您知道它只会返回一行。 This is because while you might know it will always return 1 row, SQL doesnt and wont allow this to be a problem.这是因为虽然您可能知道它总是会返回 1 行,但 SQL 不会也不会允许这成为一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM