Using transactions in SQL Server stored procedures

Source: Internet
Author: User

Stored Procedure format:

CREATE PROCEDUREyourprocedure asBEGIN    SETNOCOUNT on; BEGINTRY---------------------Start catching exceptionsBEINTRAN------------------Start a transaction        UPDATEASETA.names=B.names fromT1 asAINNER JOINT2 asB ona.ID=b.idUPDATEASETA.names=B.names fromT1 asAINNER JOINT2 asB onA.test=b.testCOMMIT TRAN -------Commit Transactions    ENDTRY-----------End Catch Exception    BEGINCatch------------have been arrested for unusual        IF @ @TRANCOUNT > 0---------------Judge If there is any business        BEGIN            ROLLBACK TRAN----------rolling back a transaction        END         EXECYourlogerrorprocedure-----------Log error messages when the stored procedure is executed, customize    ENDCatch--------End Exception HandlingEND

Ii. Common functions for capturing errors

1, Error_number () returns the error number.

2, Error_severity () returns the severity level.

3, Error_state () returns the error status number.

4, Error_procedure () returns the name of the stored procedure or trigger where the error occurred.

5, Error_line () returns the line number that caused the error.

6, Error_message () returns the full text of the error message. The text can include values provided by any replaceable parameter, such as length, object name, or time.

Using transactions in SQL Server stored procedures

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.