[英]Using OUTPUT clause to insert value not in INSERTED
I am writing a stored procedure to process a table belonging to an application and insert values into a table belonging to the same application (so I cannot amend either table). 我正在编写一个存储过程来处理属于应用程序的表,并将值插入属于同一应用程序的表中(因此我无法修改任何一个表)。
I have to process only new records and remember which records have been processed, for this I have created a third simple table. 我必须只处理新记录并记住已经处理了哪些记录,为此我创建了第三个简单表。
Tables are below, many columns removed to leave only important details. 表格如下,删除了许多列以仅留下重要细节。
Source Table 来源表
CREATE TABLE [dbo].[DETAIL](
[DET_NET] [float] NULL,
[DET_VAT] [float] NULL,
[DET_VATCODE] [varchar](4) NULL,
[DET_GROSS] [float] NULL,
[DET_DATE] [datetime] NULL,
[DET_PRIMARY] [float] NOT NULL
)
Target Table 目标表
CREATE TABLE [dbo].[TRN_TEMP](
[TRN_TRAN_DATE] [datetime] NULL,
[TRN_DESCRIPTION] [varchar](20) NULL,
[TRN_PRIMARY] [int] NULL,
[TRN_AMT] [float] NULL
)
Tracking Table 跟踪表
CREATE TABLE REGISTER(
LINE_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL,
DET_PRIMARY_LINK FLOAT NOT NULL,
INS_DATE DATETIME NOT NULL
)
I am trying to insert into the Target table values from the source table, but also insert the primary key of the source table into the tracking table. 我试图从源表插入Target表值,但也将源表的主键插入到跟踪表中。
INSERT INTO TRN_TEMP (TRN_TRAN_DATE, TRN_DESCRIPTION, TRN_AMT)
OUTPUT D.DET_PRIMARY, GETDATE() INTO REGISTER (DET_PRIMARY_LINK, INS_DATE)
SELECT D.DET_DATE, 'SOMETEXT', SUM(D.DET_NET)
FROM DETAIL D
LEFT JOIN REGISTER R ON D.DET_PRIMARY = R.DET_PRIMARY_LINK
WHERE <MY CONDITIONS> AND R.LINE_ID IS NULL -- TO REMOVE LINES ALREADY PROCESSED
GROUP BY D.DET_DATE
I can't see a problem with the text above but I get an error "The multi part identifier 'D.DET_PRIMARY' could not be bound.". 我看不出上面文本的问题,但是我收到错误“无法绑定多部分标识符'D.DET_PRIMARY'。” I've tried both
D.DET_DETAIL
and DETAIL.DET_DETAIL
and the error is the same. 我已经尝试了
D.DET_DETAIL
和DETAIL.DET_DETAIL
,错误是一样的。
Is it not possible to use values from the source table in the OUTPUT
clause when using a group or do I have an error in the formatting? 使用组时是否无法使用
OUTPUT
子句中的源表中的值,或者格式化中是否有错误? If it's not possible is there another way of keeping track of which lines I have processed? 如果不可能有另一种方法来跟踪我处理的线路?
Use MERGE
instead of INSERT
: 使用
MERGE
而不是INSERT
:
MERGE
INTO trn_temp d
USING (
SELECT D.DET_DATE, 'SOMETEXT' AS sometext, SUM(D.DET_NET) AS the_sum
...
) s
ON (1 = 0)
WHEN NOT MATCHED THEN
INSERT (TRN_TRAN_DATE, TRN_DESCRIPTION, TRN_AMT)
VALUES (det_date, sometext, the_sum)
OUTPUT s.*
Update: 更新:
To work around the GROUP BY
problem, use this: 要解决
GROUP BY
问题,请使用以下命令:
DECLARE @tmp TABLE
(
det_primary INT NOT NULL PRIMARY KEY
)
MERGE
INTO register r
USING detail d
ON (r.det_primary_link = d.det_primary)
WHEN NOT MATCHED THEN
INSERT (det_primary_link, ins_date)
VALUES (det_primary, GETDATE())
OUTPUT d.det_primary
INTO @tmp;
INSERT
INTO trn_temp (trn_tran_date, trn_description, trn_amt)
OUTPUT INSERTED.*
SELECT det_date, 'sometext', SUM(det_net)
FROM @tmp t
JOIN detail d
ON d.det_primary = t.det_primary
GROUP BY
det_date
I know this question is nearly a year old, but your insert can work as you have presented it if you select the proper column name from the OUTPUT 我知道这个问题已经有将近一年的历史了,但是如果你从OUTPUT中选择了合适的列名,你的插入就可以正常工作了。
The output clause gives us one of two possible virtual tables, inserted or deleted, depending on the operation, an update operation gives both. output子句为我们提供了两个可能的虚拟表之一,插入或删除,具体取决于操作,更新操作同时给出。 Since you don't have a field name DET_DATE in your TRN_TEMP table that you just inserted into, it is invalid in the output statement.
由于您刚刚插入的TRN_TEMP表中没有字段名称DET_DATE,因此在输出语句中它无效。
INSERT INTO TRN_TEMP (TRN_TRAN_DATE, TRN_DESCRIPTION, TRN_AMT)
OUTPUT INSERTED.TRN_TRAN_DATE, GETDATE()
INTO REGISTER (DET_PRIMARY_LINK, INS_DATE)
SELECT D.DET_DATE, 'SOMETEXT', SUM(D.DET_NET)
FROM DETAIL D
LEFT JOIN REGISTER R ON D.DET_PRIMARY = R.DET_PRIMARY_LINK
WHERE <MY CONDITIONS> AND R.LINE_ID IS NULL -- TO REMOVE LINES ALREADY PROCESSED
GROUP BY D.DET_DATE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.