From the course: Complete Guide to Advanced SQL Server

Unlock the full course today

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

Create a transaction

Create a transaction

To explore how transactions work in SQL Server, let's return to the Colors table that we've worked with previously. In a prior movie, we created a stored procedure called uspInsertColor to help us enter new color names into that table. Now, if you've been following along, then you'll already have this stored procedure, but if not, you can run Lines 6 through 16 in order to create it now. Okay. Let's turn our attention to transactions. In order to wrap a batch of commands into a transaction, we need to start the transaction at the beginning and end the transaction at the end. Starting a transaction is easy. Just run the BEGIN TRANSACTION command, or you can abbreviate it to just simply BEGIN TRAN. You can provide a name for the transaction, but that's optional. Naming your transactions can help you later on when it's time to close the transaction, so that it's easy to see which lines correspond to which transaction name. I'm naming this transaction "FirstTransaction." If you do name…

Contents