Mid
T-SQL
SQL & Databases
Explain transactions and write a TRY/CATCH transaction template.
Short answer: BEGIN TRAN; work; COMMIT. On error, ROLLBACK inside CATCH. Check @@TRANCOUNT / XACT_STATE(). Mention ACID briefly.
Sample solution
T-SQL
BEGIN TRY
BEGIN TRAN;
-- DML here
COMMIT TRAN;
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0 ROLLBACK TRAN;
THROW;
END CATCH;
THROW (not old-style RAISERROR alone) preserves error details in modern T-SQL.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png