Sql server 2000中监测错误,只能通过监测全局遍历 @@ERROR.由于@@ERROR会被下一个数据库操作所覆盖. 所以在每次操作完后必须立即监测. SQL SERVER 2005中异常处理: TRY...CATCH是SQL Server 2005提供的更具有可读性的语法.每个开发人员都熟悉这种写法.SQL Server 2005仍然支持@@ERROR这种用法.
BEGIN TRY { sql_statement | statement_block } END TRY BEGIN CATCH { sql_statement | statement_block } END CATCH ,和普通语言的异常处理用法差不多,但要注意的是,SQL SERVER只捕捉那些不是严重的异常,当比如数据库不能连接等 这类异常时,是不能捕捉的 一个例子: BEGIN TRY DECLARE @X INT -- Divid...
未捕获错误,并且控制将构造传出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...
您可以從區塊範圍內 CATCH 的任何位置使用這些函式來擷取錯誤資訊。 例如,下列指令碼顯示包含錯誤處理函數的預存程序。 在 CATCH建構的 TRY...CATCH區塊中,會呼叫預存程序,並傳回錯誤的相關資訊。 SQL 複製 -- Verify that the stored procedure does not already exist. IF OBJECT_ID('usp_GetErrorInfo', ...
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...
在存储过程内运行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; GO-- Create a stored procedure that will cause an--...
Transact-SQL 代码中的错误可使用 TRY…CATCH 构造处理,此功能类似于 Microsoft Visual C++ 和 Microsoft Visual C# 语言的异常处理功能。TRY…CATCH 构造包括两部分:一个 TRY 块和一个 CATCH 块。如果在 TRY 块内的 Transact-SQL 语句中检测到错误条件,则控制将被传递到 CATCH 块(可在此块中处理此错误)。
If the stored procedure contains a TRY...CATCH construct, the error transfers control to the CATCH block in the stored procedure. When the CATCH block code finishes, control is passed back to the statement immediately after the EXECUTE statement that called the stored procedure.GOTO...
If the stored procedure contains a TRY...CATCH construct, the error transfers control to the CATCH block in the stored procedure. When the CATCH block code finishes, control is passed back to the statement immediately after the EXECUTE statement that called the stored procedure.GOTO...
VALUES (4, ‘FOUR’); — 执行成功后,需要提交事务. COMMIT; END TRY BEGIN CATCH...