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