Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Please help me to fix the logic with where clause

like image 607
Akhil Avatar asked Jun 06 '26 16:06

Akhil


1 Answers

Although this question has an accepted answer, I think the following query would be more concise instead of using derived table.

DELETE FROM tbl1 
OUTPUT GETDATE(), DELETED.*
INTO AR_tbl1
WHERE Amt >= 40
like image 90
Adnan Sharif Avatar answered Jun 09 '26 06:06

Adnan Sharif