What is a transaction? These are database-specific terms. The lazy explained here: it is to treat multiple things as one thing. That is to say, if everyone is on the same ship, they must live together and over together!
Why should I use transactions? Here is another vulgar example:
I am saving money in the bank, so there are several steps:
1. Hand over the money to the staff; 2. Fill in the work order; 3. Sign the form; 4. Confirm and enter the computer.
If I had handed over the money to the staff, I would have signed 3. The buddy suddenly suffered a heart attack and fell over. Then, my money had not been input into the computer, but I paid the money and signed it for confirmation. There was no other record. Wouldn't I lose my money ??? My hard-earned money! Return it to me !!
As a result, the database produces the term Transaction, that is, either success or failure, and restoring the original state.
Or write the program:
Create Proc sp I deposit (@ M Money, @ iOperator Int)
As
Begin
Declare @ I int
Begin Tran -- activate a transaction
Exec @ I = sp pay @ m, @ iOperator
If @ I <> 0 -- the system Error code @ Error is generally used here. I will not use it here for example. Depends on the actual situation.
Begin
Rollback Tran -- roll back the transaction
RaisError! :( ', 16, 1) with Log -- Record Log
Return-1 -- Return Error Code
End
Exec @ I = sp fill in @ m, @ iOperator
If @ I <> 0
Begin
Rollback Tran -- roll back the transaction
RaisError ?? ', 16, 1) with Log
Return-2
End
Exec @ I = sp signature @ m
If @ I <> 0
Begin
Rollback Tran -- roll back the transaction
RaisError ('depends on me? What bad banks do? I can't write anything for 3 transactions !! I am not saving it !! Do not sign! ', 16, 1) with Log
Return-3
End
Exec @ I = sp enter the computer @ m, @ iOperator
If @ I <> 0
Begin
Rollback Tran -- roll back the transaction
RaisError ('what does it mean? Is the disk space full? Okay, give me the money, and I will go to the bank next to it! ', 16, 1) with Log
Return-4
End
Commit Tran -- submit a transaction
Return 0
End