Try Catch in SQL with Transaction.
BEGIN TRY
BEGIN TRANSACTION
--Lots of T-SQL Code here
COMMIT
END TRY
BEGIN CATCH
ROLLBACK
END CATCH
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO tbTable_deleted(Id)
SELECT Id FROM tbTable
DELETE
a
FROM
tbTable a
INNER JOIN tbTable_deleted t ON t.Id = a.Id
COMMIT
PRINT 'Records successfully copied over and removed.'
END TRY
BEGIN CATCH
ROLLBACK
SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH
Sources:
https://docs.microsoft.com/en-us/sql/t-sql/functions/error-message-transact-sql?view=sql-server-2017
Comments