SQL Server – Simple Transaction in SQL Server

So, after starting with SQL Server custom views, SQL Server Triggers, SQL Server Procedures and Functions, now it is time to pay some attention to SQL Transaction as well.

sqlserver

What are transactions? Microsoft says the following:

 A transaction is a single unit of work. If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database. If a transaction encounters errors and must be canceled or rolled back, then all of the data modifications are erased.

So, in our nice example table called Money, we have 3 columns – Id, Cell and PaidPerCell. The idea for the table came from this article, but I am still using it. Thus, let’s say that we simply should update a value in one cell, but we want to d it with a transaction, in order to be sure that nothing wrong is going to happen, if the electricity goes off 🙂

Thus, we should create the following transaction:

So, the way to call the transaction is again through a query in SQL. Like this:

Now, once the transaction is successful, we may get a value of 99912 for any row, where we have Cell 1. Like this:

paidPerCell

That’s all folks! 🙂

Tagged with: , , ,