Database Transaction example (cainiao Edition)

Source: Internet
Author: User

Begintrans: Start transaction
Committrans: commit a transaction
Rollbacktrans: roll back the transaction
The transaction is atomic, either not executed or fully executed. Once the transaction is successfully executed, it is permanently saved. The following is an example of a transfer transaction application.

1. Create a database

-----------------------------------------------------------------------

If not exists (select * From sysobjects where name = 'bank ')
Begin
Create Table bank
(
Bankid int identity (1, 1) primary key,
Username varchar (50) not null,
Rmbnum float not null
)
End

------------------------------------------------------------------------

 

2. Insert data

------------------------------------------------------------------------

Insert into Bank (username, rmbnum) values ('zhang san', 10000)
Insert into Bank (username, rmbnum) values ('lily', 10000)
Insert into Bank (username, rmbnum) values ('王', 10000)
Insert into Bank (username, rmbnum) values ('Children, 10000)

-------------------------------------------------------------------------

 

3. Limit the account deposit not less than 0

-------------------------------------------------------------------------

Alter table bank add constraint ck_bank_rmbnum check (rmbnum> 0)

-------------------------------------------------------------------------

 

4. Execute the transfer (normal version) to transfer 200 million of Michael Jacob's account to Mr. Li's account (Mr. Li successfully added the money because Michael Jacob's account balance was insufficient and he failed to deduct the money. So the bank will not lose 20000 ?)

--------------------------------------------------------------------------

Declare @ howmuch float
Set @ howmuch = 20000
Update Bank set rmbnum = rmbnum-@ howmuch where username = 'zhangsan'
Update Bank set rmbnum = rmbnum + @ howmuch where username = 'Li si'

---------------------------------------------------------------------------

5. Execute the transfer (transaction Version)

Select * From bank

-- View the balance before transfer

1 Zhang San 10000
2 Li Si 10000
3. Wang Wu 10000
4 pediatric 10000

 

Execute business

----------------------------------------------------------------------------

Begin TransAction
Declare @ errno int
Declare @ num int
Set @ errno = 0
Set @ num = 100000
-- Remove Wang Wu's account @ num
Update Bank set rmbnum = rmbnum-@ num where username = 'wang wu'
Set @ errno = @ errno + @ error -- add the error number generated during execution
-- Add @ num to the pediatric account
Update bank set rmbnum = rmbnum + @ num where username = 'pediatric'
Set @ errno = @ errno + @ error

-- Determines whether the transaction is committed or canceled based on whether errors are generated.
If @ errno> 0
Begin
Print ('transaction processing failed, roll back the transaction! ')
Rollback TransAction
End
Else
Begin
Print ('transaction processed successfully, commit transaction! ')
Commit TransAction
End

------------------------------------------------------------------------------

Running result

(One row is affected)

Transaction Processing failed. roll back the transaction!

Select * From bank

1 Zhang San 10000
2 Li Si 10000
3. Wang Wu 10000
4 pediatric 10000

The query database data has not changed (Transfer failed, and the Update statement has not been executed)

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.