Error Handling – TRY…CATCH

Error Handling block can be used to catch errors and commit or rollback the transaction accordingly as below:

Example: Error Handling with Transaction in SQL Server

BEGIN TRY

BEGIN TRANSACTION Trans_One
Insert into Customers (CustName,City,State,Country) Values ('Loganathan','Chennai','TN','India')
Insert into Orders Values (@@IDENTITY,'SANYO TV',1)
COMMIT TRANSACTION Trans_One

END TRY

BEGIN CATCH

ROLLBACK TRANSACTION Trans_One

END CATCH

Below is the data updated after the above Insert statements
Output:

Try…Catch example

SQL Server Transaction

Multiple SQL queries in a group in SQL Server may need to execute and out of the SQL Queries in the group, some of the queries may fail. This could create data update issues and data consistency issues as a partial update could make a series of transactions incomplete. So, there is an SQL Statement called TRANSACTION with COMMIT and ROLLBACK, which can be used to handle incomplete or partial execution of queries in a Transaction. In this article, we will discuss the different Transaction commands with examples.

This article explores SQL Server Transactions, including syntax, error handling, and best practices. The model illustrates the key role played in data integrity, which is applied to financial transactions and inventory management. Whether you’re a developer or an administrator, understanding these principles is essential to building flexible database applications.

Similar Reads

TRANSACTION in SQL Server

A TRANSACTION in SQL Server is a single unit of work that contains one or more SQL Statements to be executed at once. The TRANSACTION statement ensures that all statements within a single transaction either succeed or fail together to maintain data consistency. If there are multiple SQL queries for Insert, Update, or Delete without a TRANSACTION statement, if one of the queries fails then the other queries get executed since each SQL statement is an independent Transaction. This could lead to errors for a targeted transaction with incomplete data updates....

Example of SQL Server Transaction

Example: Using Begin Transaction and Commit Transaction...

IF…ELSE Statements inside the TRANSACTION block

The COMMIT and ROLLBACK can be used inside conditional statements like IF…ELSE to commit or rollback the changes from all the SQL queries based on a specific condition....

SAVE TRANSACTION

The save transaction statement can be used with a name identifier to rollback a transaction from a particular point based on some specific logic....

Named Transaction in SQL Server

Each BEGIN TRANSACTION can be given a name and it can be referenced in COMMIT and ROLLBACK Statements. This will be useful when there are multiple TRANSACTION statements or nested transaction statements in a single Transaction code block....

Named Transaction example

Nested Transactions...

Nested Transactions

TRANSACTION statements can be nested to separate multiple units of code and to only COMMIT or ROLLBACK specific TRANSACTION...

Error Handling – TRY…CATCH

Error Handling block can be used to catch errors and commit or rollback the transaction accordingly as below:...

Common Use Cases for TRANSACTION

1. Transferring funds between Bank Accounts...

Conclusion

The TRANSACTION statements are a great way to avoid any partial updates to the database tables which are related and needs to implement ACID characteristics of all data. It is important to use transaction statements with care and as needed to maintain data consistency and integration. By implementing proper transaction management strategies, developers and administrators can enhance the performance and reliability of their SQL Server databases....