SQL database transactions

Source: Internet
Author: User

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

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.