CREATE PROCEDURE YourLogErrorProcedure @ErrorLogID [int] = 0 OUTPUT -- contains the ErrorLogID of the row inserted AS -- by uspLogError in the ErrorLog table BEGIN SET NOCOUNT ON; -- Output parameter value of 0 indicates that error -- information was not logged SET @ErrorLogID = 0;...
/en-us/sql/t-sql/language-elements/throw-transact-sql?view=sql-server-2017 However, If you really don't need a custom message, I prefer to just: THROW; That way you get the entire original error message in the caller and use the standard ERROR_* functions. prettyprint 复制 ALTER PRO...
the END CATCH statement. If the END CATCH statement is the last statement in a stored procedure or trigger, control is returned to the code that invoked the stored procedure or trigger. Transact-SQL statements in the TRY block following the statement that generates an error will not be ...
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())...
3. Error handling in legacy code Before the introduction of theBEGIN TRY/BEGIN CATCHblocks in SQL Server 2005,GOTOwas often used for error handling. Even though theTRY...CATCHstructure is now the preferred method for handling exceptions, you may encounter legacy codebases whereGOTOis still used...
EXEC YourLogErrorProcedure---执⾏存储过程将错误信息记录在表当中 END CATCH---结束异常处理 END ---记录操作错信息的存储过程--- CREATE PROCEDURE YourLogErrorProcedure
Server 2005 now supports a more programmable error trapping convention in T-SQL code. This error handling is known as the TRY/CATCH block. The TRY/CATCH
Now let's modify the body of the code and use TRY/CATCH. (For this example, you'll need to run the code in a version of SQL Server 2005.) When you use TRY/CATCH, you separate your action code from error handling code. You put code for an action in a TRY block and place your...
,和普通语言的异常处理用法差不多,但要注意的是,SQL SERVER只捕捉那些不是严重的异常,当比如数据库不能连接等 这类异常时,是不能捕捉的 一个例子: BEGIN TRY DECLARE @X INT -- Divide by zero to generate Error SET @X = 1/0 PRINT 'Command after error in TRY block' ...
ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred. ERROR_LINE() returns the line number inside the routine that caused the error. ERROR_MESSAGE() returns the complete text of the error message. The text includes the values supplied for any substitut...