Joint transactions of Stored Procedures

Source: Internet
Author: User
Dear friends! Remember the previous blogs about stored procedures! Next, let us have a deeper understanding and use it as a joint transaction, so that it can give full play to its advantages. First, I will briefly introduce the transaction DatabaseTransaction, which refers to a series of operations performed as a single logical unit of work. Transaction Processing ensures that

Dear friends! Remember the previous blogs about stored procedures! Next, let us have a deeper understanding and use it as a joint transaction, so that it can give full play to its advantages. First of all, here I will briefly introduce the Transaction: Database Transaction refers to a series of operations performed as a single logical unit of work. Transaction Processing ensures that

Dear friends! Remember the previous blogs about stored procedures! Next, let us have a deeper understanding and use it as a joint transaction, so that it can give full play to its advantages.

First, I will briefly introduce the transaction here:

Database Transaction refers to a series of operations performed as a single logical unit of work. Transaction Processing ensures that data-oriented resources are not updated permanently unless all operations in the transaction unit are successfully completed. I have introduced transactions in the front, so I will not talk about them here. Welcome to the following link:

Http://blog.csdn.net/liu_yujie2011com/article/details/9004816

Next, we will analyze the application of transactions and stored procedures in data center cooperation: When registering student information, we should consider three tables: Student table, card table, and recharge table. Insert the newly registered information into these tables, and the problem occurs. Is it a table insert? Or insert simultaneously? The answer is, of course, simultaneous insertion. But the problem arises again. What if I insert three pieces of information at the same time? At this time, the transaction will come in handy. If an error occurs during data insertion, the transaction will be rolled back immediately and an error message will be prompted. Let's get into practice!

The stored procedure and transaction code:

Alter procedure [dbo]. [PROC_StudentRegister] @ studentID varchar (20), @ Name varchar (20), @ sex varchar (20), @ department varchar (20), @ grade varchar (20 ), @ Classroom varchar (20), @ explain varchar (20), @ userID varchar (20), @ recharge money, @ rechargeDate date, @ rechargeTime (7 ), @ isCheck varchar (20), @ cardID varchar (20), @ Balance money, @ registerDate date, @ registerTime time (7), @ cardFix varchar (20 ), @ state varchar (20) AS Begin -- transaction SET NoCOUNT ON; SET XACT_ABORT ON; begin Tran OK -- start a transaction -- add the student table insert into T_StudentInfo (studentID, Name, sex, department, grade, Classroom, explain, userID) values (@ studentID, @ Name, @ sex, @ department, @ grade, @ Classroom, @ explain, @ userID) -- insert into T_CardInfo (cardID, Balance, registerDate, registerTime, cardFix, state, studentID, isCheck, userID) values (@ cardID, @ Balance, @ registerDate, @ registerTime, @ cardFix, @ state, @ studentID, @ isCheck, @ userID) -- add the recharge table insert into T_RechargeInfo (cardID, recharge, rechargeDate, rechargeTime, userID, isCheck) values (@ cardID, @ recharge, @ rechargeDate, @ rechargeTime, @ userID, @ isCheck) if @ ERROR = 0 -- if the database operation is correct, commit Tran OK -- submit the transaction else -- wrong begin -- if the ERROR rollback Tran -- roll back the transaction endEnd
The above is to add transactions in the database, and the other is to add transactions in the code. I will not introduce them now! Study and try again in the future!

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.