SQL about Dblink and multiple update, insert transaction rollback notation

Source: Internet
Author: User
Tags rowcount

In the process of writing stored procedures will inevitably encounter proc and cross-Library calls to other people proc, there is a proc in a number of multiple tables to write and modify the statement. Then we'll use the tran.

If we're going to write the back of every insert,update without writing a try,

IF @ @error <> 0                OR @ @Rowcount = 0                BEGIN                    ROLLBACK TRAN                 END    

To ensure the normal execution of all statements and the number of rows affected.

And in the case of writing a try, we can standardize the wording, using RAISERROR

DECLARE @Ret_Msg VARCHAR( $)DECLARE @Ret_Msg1 VARCHAR( $)DECLARE @nResult INTDECLARE @vcResult VARCHAR( $)BEGINTRYBEGIN   TRAN               UPDATETable_aSetA=1 WHEREB=1               IF @ @Rowcount = 0               BEGIN                    SET @Ret_Msg = 'Execution failed'                    SET @Ret_Msg1 = 'table_a update failed'                    RAISERROR(@Ret_Msg1, -,1)               ENDdatabase. Schema.proc 1,2,@nResultOut,@vcResult outIF @nResult <0               BEGIN                    SET @Ret_Msg = @vcResult                    SET @Ret_Msg1 = 'Dblink call failed' + @vcResult                    RAISERROR(@Ret_Msg1, -,1)               END               SELECT @Ret_Msg   asVcresultCOMMIT TRANENDTRYBEGINCATCHIF @ @tranCount > 0                ROLLBACK TRAN; SELECT  @Ret_Msg  asVcresultENDCatch

When the result is not what we want, RAISERROR throws a mistake and lets the catch handle

SQL about Dblink and multiple update, insert transaction rollback notation

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.