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
----------------------------------------------------------------------
The above is a pseudo code that simulates the process of deposit.