1.存储过程代码。 (数据库是 AdventureWorks) 存储过程用来将错误信息插入到指定的表中。 在别的地方可以调用存储过程插入错误信息(下面部分代码 生成一个表,如果表已经存在就会插入错误信息到表中) 知识点: try catch,output参数;begin try..end try;begin catch….end catch; --存储过程output, begin try,begin...
USE tempdb; GO CREATE PROCEDURE Called @Msg2 VARCHAR(100) OUTPUT AS SET NOCOUNT ON; BEGIN TRY SET @Msg2 = '"Called" SP' SELECT 1/0; END TRY BEGIN CATCH SET @Msg2 = '--Divide by Zero Caught in "Called" SP'; THROW 50000,@Msg2,1; SELECT ERROR_NUMBER() AS ErrorNumber ,ERROR_...
未捕获错误,并且控制将构造传出TRY...CATCH到下一个更高的级别。 在SELECT存储过程内运行该语句会导致错误发生在低于TRY块的级别。 该错误由TRY...CATCH构造处理。 SQL -- Verify that the stored procedure does not exist.IF OBJECT_ID(N'usp_ExampleProc', N'P') IS NOT NULLDROPPROCEDUREusp_ExampleProc...
PRINT 'An error occurred in stored procedure uspLogError: '; EXECUTE YourPrintErrorProcedure;---打印错误信息的存储过程 RETURN -1; END CATCH END; CREATE PROCEDURE YourPrintErrorProcedure AS BEGIN SET NOCOUNT ON; -- Print error information. PRINT 'Error ' + CONVERT(varchar(50), ERROR_NUMBER())...
If the value returned is an empty handle, and you do not have aTryCatchin place, your code must bail out. 如果返回值是一个空句柄,并且你没有正确的设置TryCatch,你的代码就会崩溃。 4. Returns the name of the stored procedure or trigger where an error occurred that caused the CATCH block ...
ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred. Error information is retrieved by using these functions from anywhere in the scope of the CATCH block of a TRY…CATCH construct. The error functions will return NULL if called outside the scope of...
This is our Stored Procedure. This is the CATCH block within our Stored Procedure: Error Line #5 of procedure #myProc Now our MAIN sql code block continues. ¹取消注释存储过程的CATCH块中的“附加错误行”将产生: This is our MAIN procedure. ...
錯誤不會攔截,而且控制權會從建構傳出TRY...CATCH至下一個較高層級。 在SELECT預存程式內執行 語句會導致錯誤發生在低於 區塊的TRY層級。 錯誤是由TRY...CATCH建構所處理。 SQL -- Verify that the stored procedure does not exist.IF OBJECT_ID(N'usp_ExampleProc', N'P') IS NOT NULLDROP...
在TRY.CATCH构造中,事务可以进入一种状态:事务保持打开但无法提交。 www.ecd123.com 6. In the CATCH block of a TRY. . . CATCH construct, the stored procedure is called and information about the error is returned. 在TRY…CATCH构造的CATCH块中,调用了该存储过程并返回有关错误的信息。 msdn2.micros...
BEGIN CATCH PRINT 'An error occurred in stored procedure uspLogError: '; EXECUTE YourPrintErrorProcedure;---打印错误信息的存储过程 RETURN -1; END CATCH END; CREATE PROCEDURE YourPrintErrorProcedure AS BEGIN SET NOCOUNT ON; -- Print error information...