-- Typical transaction programming example
-- Begin transaction is the start transaction, commit transaction is the commit transaction, and rollback transaction is the rollback transaction
-- In this example, insert a record first. If an error occurs, roll back the transaction, that is, cancel the transaction, and directly return (return). If yes, commit commits the transaction.
-- The Return above can return an integer. If the return value is 0, there is no error during execution. If the return value is a negative number,
-- This return can also be used in the stored procedure. You can use exec @ return_status = pro_name to obtain this value.
Use pubs
Go
Begin Tran mytran
Insert into stores (stor_id, stor_name)
Values ('20140901', 'My books ')
Go
Insert into discounts (discounttype, stor_id, discount)
Values ('clearance sale ', '123', 9999)
If @ error <> 0
Begin
Rollback Tran mytran
Print 'insert discount record error'
Return
End
Commit Tran mytran
-- Example of saving points for Transaction Processing
-- After a transaction is saved, the transaction can be rolled back to the specified storage point, so that no operation can be used.
Use pubs
Go
Select * from stores
Begin transaction testsavetran
Insert into stores (stor_id, stor_name)
Values ('000000', 'W. z.d Book ')
Save transaction before_insert_data2
Go
Insert into stores (stor_id, stor_name)
Values ('20140901', 'foreat books ')
Go
Rollback transaction before_insert_data2
Select * from stores