如果存储过程引发错误,如何维护错误日志?

时间:2015-07-16 09:09:06

标签: sql sql-server database sql-server-2008 sql-server-2012

我们有一个场景,如果存储过程抛出错误,那么当存储过程将错误引入TRY CATCH块中的错误日志表时,我们需要记录错误相关数据,参数和时间。任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我会创建一个用于记录错误详细信息的表格.....

CREATE TABLE Error_Log 
(
  ProcName      SYSNAME
 ,LoggedAt      DATETIME
 ,ErrorMessage  NVARCHAR(4000)
 ,ErrorLine     NVARCHAR(4000)
 ,ErrorNumber   INT
 ,ErrorSeverity INT
 ,ErrorState    INT
 ,ParamDetails  NVARCHAR(MAX) 
)
GO

然后你记录错误详情是的,你是正确的TRY & CATCH块是你需要的东西......

CREATE PROCEDURE dbo.myTestProc
  @Var1 INT 
 ,@Var2 INT
 ,@Var3 VARCHAR(10)
AS
BEGIN
  SET NOCOUNT ON;

BEGIN TRY


  BEGIN TRANSACTION;
    /* Do your stuff here*/
    /* if Every thing goes fine commit transaction*/
  COMMIT TRANSACTION;
   /* else the contorl will jump to catch block*/
END TRY



BEGIN CATCH
  IF @@TRANCOUNT <> 0
    ROLLBACK TRANSACTION; --<-- If any open trans rollback

  /* Now here log error details */

  DECLARE @ParamDetails NVARCHAR(MAX);

  SET @ParamDetails = ' @Var1 = ' + CAST(ISNULL(@Var1, 'NULL') AS NVARCHAR(MAX))
                    + ',@Var2 = ' + CAST(ISNULL(@Var2, 'NULL') AS NVARCHAR(MAX))
                    + ',@Var3 = ' + CAST(ISNULL(@Var3, 'NULL') AS NVARCHAR(MAX))

  INSERT INTO Error_Log (ProcName , LoggedAt , ErrorMessage , ErrorLine 
                          ,ErrorNumber ,ErrorSeverity, ErrorState , ParamDetails)
  VALUES (ERROR_PROCEDURE() , GETDATE() , ERROR_MESSAGE() , ERROR_LINE(), ERROR_NUMBER() 
        , ERROR_SEVERITY() , ERROR_STATE(), @ParamDetails)

END CATCH

END

答案 1 :(得分:0)

您可以使用以下代码实现此目的: 错误日志表

    CREATE TABLE [ErrorLog]
    (
        ErrorMessage    NVARCHAR(512)
    ,   ErrorSeverity   INT
    ,   ErrorNumber     INT
    ,   ErrorState      INT
    ,   ErrorProcedure  SYSNAME
    ,   ProcParameters  VARCHAR(50)
    ,   ErrorLine       INT
    ,   ErrorTime       DATETIME
    )

程序:

    CREATE PROCEDURE [DivideByZero]
    (
        @First  INT
    ,   @Last   INT
    )
    AS
    BEGIN
        BEGIN TRY
            SELECT  (@First / @Last)
        END TRY
        BEGIN CATCH
                SELECT  ERROR_MESSAGE()
                    ,   ERROR_SEVERITY()
                    ,   ERROR_NUMBER()
                    ,   ERROR_STATE()
                    ,   ERROR_PROCEDURE()
                    ,   CONVERT(VARCHAR, @First) + ',' + 
                            CONVERT(VARCHAR, @Last)
                    ,   ERROR_LINE()    
                    ,   CURRENT_TIMESTAMP
        END CATCH
    END

执行过程以创建错误

    EXEC [DivideByZero] 1, 0

从错误日志表中选择

    SELECT * FROM ErrorLog

您应该获得以下输出:

    ErrorMessage                        ErrorSeverity   ErrorNumber ErrorState  ErrorProcedure  ProcParameters  ErrorLine   ErrorTime
    -----------------------------------------------------------------------------------------------------------------------------------------------
    Divide by zero error encountered.   16              8134        1           DivideByZero    1,0             10          2011-09-11 15:15:10.803