[英]Insert into from deleted output clause
I am archiving my table data to archive tables. 我正在将表数据归档到归档表。 For this I am just inserting the records to the archive table based on some condition and in the next statement I delete the records with the same condition in the main table.
为此,我只是根据某种条件将记录插入到存档表中,并在下一条语句中删除主表中具有相同条件的记录。
But to improve performance it was suggested to use a single statement with an output clause. 但是为了提高性能,建议使用带有输出子句的单个语句。
Code : 代码:
INSERT INTO AR_tbl1
SELECT GETDATE(), D.*
FROM
(DELETE FROM tbl1
WHERE Amt >= 40
OUTPUT DELETED.*) D
But this is not working. 但这是行不通的。 If I comment the where clause it works.
如果我评论where子句,它会起作用。 Please help me to fix the logic with
where
clause 请帮助我修复
where
子句的逻辑
I finished. 我完成了。 thanks Martin.
谢谢马丁。 The problem lies in the order.
问题出在顺序上。 Output clause needs to add before the where clause.
输出子句需要在where子句之前添加。
New Code : 新代码:
INSERT INTO AR_tbl1
SELECT D.*
FROM
(DELETE FROM tbl1
OUTPUT DELETED.*
WHERE Amt >= 40) D
Although this question has an accepted answer, I think the following query would be more concise instead of using derived table
. 尽管此问题的答案已被接受,但我认为以下查询比使用
derived table
更为简洁。
DELETE FROM tbl1
OUTPUT GETDATE(), DELETED.*
INTO AR_tbl1
WHERE Amt >= 40
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.