SQL Tran save point

Source: Internet
Author: User
-- 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.