[英]How do I insert multiple columns into a table using OUTPUT in SQL Server?
Scroll down to the OUTPUT part. 向下滚动到OUTPUT部分。 It's giving me a red underline.
这给了我一个红色的下划线。 I want to insert into the
InsertedCreditDebitAdjustmentIDs
table, the ID inserted into @CreditDebitAdjustment
along with the InvoiceNum. 我想将插入到
@CreditDebitAdjustment
的ID与@CreditDebitAdjustment
一起InsertedCreditDebitAdjustmentIDs
表中。 How do I go about doing this? 我该怎么做呢?
DECLARE @InsertedCreditDebitAdjustmentIDs TABLE
(
ID INT,
InvoiceNumber VARCHAR(50)
)
INSERT INTO @CreditDebitAdjustment ( col1, col2, ...)
SELECT @ImportedFileID AS '@ImportedFileID',
tbl.col.value('(Purpose/text())[1]', 'VARCHAR(500)') AS Purpose,
etc.
FROM @XML.nodes('/CreditDebitAdjustments/CreditDebitAdjustment') AS tbl (col)
OUTPUT INSERTED.CreditDebitAdjustmentID, etc. INTO @InsertedCreditDebitAdjustmentIDs
put your OUTPUT clause
right after you INSERT INTO
statement something lie this.... 在
INSERT INTO
语句之后立即放置OUTPUT clause
INSERT INTO TableName (Column1, Column2, Column3.....)
OUTPUT inserted.Column1 , inserted.Column2 INTO @TableName
SELECT Column1, Column2, Column3.....
FROM Table_name
On a side note I would suggest you to keep XML Shredding
and your INSERT
operations in separate statements. 另外,我建议您将
XML Shredding
和INSERT
操作保留在单独的语句中。 any hanky panky in your XML can leave you with a lot of cleaning to do :) XML中的任何麻烦问题都可以给您带来很多麻烦:)
do the shredding separately insert results into a temp table, once happy then insert into your target tables. 进行切碎时,将结果分别插入到临时表中,一旦满意,则将其插入到目标表中。
It goes: 它去了:
TARGET (insert/update etc.)
OUTPUT (direct or into another table)
SOURCE (select/values etc.)
So you just need to switch the order: 因此,您只需要切换顺序:
INSERT dbo.tablename(column1, column2, ...)
OUTPUT INSERTED.col1, INSERTED.col2, ... [INTO ...]
SELECT @var1, col1, etc. FROM elsewhere...
See the OUTPUT Clause (Transact-SQL) documentation for examples. 有关示例,请参见OUTPUT子句(Transact-SQL)文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.