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.
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:
1 2 3 4 5 6 7 8 |
CREATE proc usp_changepaidcellvalue @cell INT, @paidPerCell INT AS BEGIN tran UPDATE Money SET PaidPerCell = @paidPerCell WHERE Cell = @cell COMMIT tran GO |
So, the way to call the transaction is again through a query in SQL. Like this:
1 |
EXEC usp_changepaidcellvalue 1,99912 |
Now, once the transaction is successful, we may get a value of 99912 for any row, where we have Cell 1. Like this:
That’s all folks! 🙂