From the course: Complete Guide to Advanced SQL Server

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Manage transaction control

Manage transaction control

We've seen a number of techniques for managing the flow of a program using error handling with TRY-CATCH blocks. Now, it's time to combine that with what we learned in the previous chapter about transactions. If you remember, we saw that transactions need to be closed by either finalizing the data with commit transaction or undoing the changes with rollback transaction. But in that chapter, we had to manually decide which route to take and either send the commit command or the rollback command ourselves. The last piece of that puzzle is to combine all of this knowledge and have the TRY-CATCH blocks control when a commit is sent and when a rollback is sent. So let's go back to the prior transaction example. When we last saw the Warehouse.Colors table, we had just inserted a row called Burnished Bronze. We also wrote this transaction on Lines 11 through 16. This transaction is used to insert two new colors and then commit the change to the table all at once. But we found that if you try…

Contents