Analysis of SQL stored procedures and transaction processing

Source: Internet
Author: User

In database programming, transactions are frequently used. net platform, transaction processing is very easy to use, but in the stored procedures of SQL Server database, how to use transactions to complete data batch operations?

The solution is as follows:
Copy codeThe Code is as follows:
Create proc RegisterUser
(@ UsrName varchar (30), @ usrPasswd varchar (30), @ age int, @ PhoneNum varchar (20), @ Address varchar (50 ))
AS
BEGIN

Begin tran -- display the definition and start a transaction
SET XACT_ABORT ON -- indicates rolling back immediately when an error occurs.

Insert into user (userName, userPasswd) values (@ usrName, @ usrPasswd)

IF @ error <> 0 -- an error occurs.
BEGIN
ROLLBACK TRANSACTION
RETURN 0
END
ELSE
BEGIN
COMMIT TRANSACTION
RETURN 1 -- execution successful
END

END

Concise enough!

Related Article

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.