简体   繁体   中英

Ensure Successful INSERT before triggering DELETE

Am currently working on archiving a database, I have come up with a simple approach. However, executing the script can run into errors. Leading to a case where the insert is not successfully executed but the delete. Which means I delete records from production before inserting them into archive. Is there a way to ensure delete statement would not be executed unless insert is run successfully?

INSERT INTO [archive].[dbo].[Table]
SELECT *
FROM [Production].[dbo].[Table]
WHERE TimeStamp < DATEADD(year,-2,SYSDATETIME()) 

DELETE FROM [Production].[dbo].[table]
WHERE TimeStamp < DATEADD(year,-2,SYSDATETIME())

As an alternative to an explict transaction, one can specify an OUTPUT clause on the DELETE to perform the operation as a single autocommit transaction. This will ensure all-or-none behavior.

DELETE [Production].[dbo].[Table]
OUTPUT DELETED.*   
INTO [archive].[dbo].[Table]
WHERE TimeStamp < DATEADD(year,-2,SYSDATETIME());

Also, consider an explict column list instead of * .

Typically, you would create a transaction, making the operation into a single update.

BEGIN TRAN

BEGIN TRY
    INSERT INTO [archive].[dbo].[Table] 
    SELECT * FROM [Production].[dbo].[Table] WHERE TimeStamp < 
             DATEADD(year,-2,SYSDATETIME())

    DELETE FROM [Production].[dbo].[table] 
    WHERE TimeStamp < DATEADD(year,-2,SYSDATETIME())

    COMMIT
END TRY
BEGIN CATCH
   ROLLBACK
END CATCH

Basically, the code says.

  • BEGIN TRAN - Start a group of commands that either all complete or none complete
  • BEGIN TRY - Commands to try
  • COMMIT - If I reach here, everything worked OK
  • BEGIN CATCH
  • ROLLBACK If an error occurs, roll back the commands (basically ignore them)

You should probably add some status to indicate success or failure, and maybe capture the error in the BEGIN CATCH block, but this should give you enough to get started

A second approach is to modify your DELETE statement a bit

DELETE FROM [Production].[dbo].[table] 
    WHERE TimeStamp IN (SELECT TimeStamp FROM [archive].[dbo].[Table])

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM