Beginners of software development are not very familiar with stored procedures and transactions. Today, we will give you a simple example of a transaction, so that you can understand it first. Then we will explain the stored procedure to you.
Start transaction: Begin transaction
Commit transaction: commit transaction
Rollback transaction: rollback transaction
Example:
Begin transaction
Declare @ errorsum int -- defines local variables
Set @ errorsum = 0 -- initialize temporary variables
Update Bank set currentmoney = currentMoney-1000 where customername = 'zhang san'
Set @ errorsum = @ errorsum + @ error -- indicates whether an error exists.
Update Bank set currentmoney = currentmoney + 1000 where customername = 'Li si'
Set @ errorsum = @ errorsum + @ error -- indicates whether an error exists.
If @ errorsum <> 0 -- if an error occurs
Begin
Rollback transaction
End
Else
Begin
Commit transaction
End
Go
Start transaction: Begin transaction
Commit transaction: commit transaction
Rollback transaction: rollback transaction
Example:
Begin transaction
Declare @ errorsum int -- defines local variables
Set @ errorsum = 0 -- initialize temporary variables
Update Bank set currentmoney = currentMoney-1000 where customername = 'zhang san'
Set @ errorsum = @ errorsum + @ error -- indicates whether an error exists.
Update Bank set currentmoney = currentmoney + 1000 where customername = 'Li si'
Set @ errorsum = @ errorsum + @ error -- indicates whether an error exists.
If @ errorsum <> 0 -- if an error occurs
Begin
Rollback transaction
End
Else
Begin
Commit transaction
End
Go