gpt4 book ai didi

sql - 插入时,输出未插入但在连接表中的列

转载 作者:行者123 更新时间:2023-12-02 03:51:44 25 4
gpt4 key购买 nike

我目前正在研究某种数据映射。假设我有以下三个表:

临时表

RUNID | DocId | AmountE       7       50C       6       12

Table1

T1ID | DocID | Amount1      5       102      6       203      6       50

Table2

T2ID | RUNID | T1Id1      B       12      C       23      D       3

In table Table1 and Table2 the columns T1ID and T2ID are identity columns that are populated automatically.What I want to do now is to insert the values from TemporaryTable into Table1 and save the value in column RunID from TemporaryTable and the newly generated T1ID to Table2

The resulting table should look like this:

Table1

T1ID | DocID | Amount1      5       102      6       203      6       504      7       505      6       12

Table2

T2ID | RUNID | T1Id1      B       12      C       23      D       34      E       45      C       5

I would like to do so with the help of the output statement. Something like this:

CREATE TABLE #map(T1ID, RUNID)
INSERT INTO Table1(DocId, Amount)
OUTPUT inserted.T1ID, t.RunId INTO #map
SELECT t.DocId, t.Amount
FROM TemporaryTable t

这显然不起作用,因为我无法访问输出语句中的 t.RunId 。这怎么能做到呢?

最佳答案

您可以使用MERGE命令与一些始终为假的条件来模拟您的插入与OUTPUT

中可用的所有列
MERGE Table1 t1
USING TemporaryTable t
ON 1=2
WHEN NOT MATCHED THEN
INSERT (DocId, Amount)
VALUES (t.DocId, t.Amount)
OUTPUT inserted.T1ID, t.RunId
INTO #map ;

关于sql - 插入时,输出未插入但在连接表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45187127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com