SQL SERVER错误处理程序,尝试在insert trigger中捕获

时间:2014-09-17 10:53:51

标签: sql-server error-handling triggers try-catch

我有一个Errorlog表,一个存储过程,用于在表中插入错误详细信息和插入触发器。但是,当我在插入触发器内调用存储过程时,不会捕获错误。相反,它在下面给出了这个错误,

The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction.

这是我的错误日志表

CREATE TABLE ErrorLog
 (
     [Error_Number] INT NOT NULL,
     [Error_LINE] INT NOT NULL,
     [Error_Location] sysname NOT NULL,
     [Error_Message] VARCHAR(MAX),
     [SPID] INT, 
    [Program_Name] VARCHAR(255),
     [Client_Address] VARCHAR(255),
     [Authentication] VARCHAR(50),
     [Error_User_Application] VARCHAR(100),
     [Error_Date] datetime NULL
         CONSTRAINT dfltErrorLog_error_date DEFAULT (GETDATE()),
     [Error_User_System] sysname NOT NULL
         CONSTRAINT dfltErrorLog_error_user_system DEFAULT (SUSER_SNAME())
 )
 GO

存储过程,

CREATE PROCEDURE [ErrorLog_proc]
 (
     @Error_Number INT = NULL,
     @Error_LINE INT = NULL,
     @Error_Location sysname = NULL,
     @Error_Message VARCHAR(4000) = NULL,
     @UserID INT = NULL
 ) 
AS
 BEGIN
     BEGIN TRY

        INSERT INTO ErrorLog
         (
             [Error_Number]
             ,[Error_LINE]
             ,[Error_Location]
             ,[Error_Message]
             ,[SPID]
             ,[Program_Name]
             ,[Client_Address]
             ,[Authentication]
             ,[Error_User_System]
             ,[Error_User_Application]
         )
         SELECT 
            [Error_Number]              = ISNULL(@Error_Number,ERROR_NUMBER())  
            ,[eRROR_LINE]               = ISNULL(@Error_LINE,ERROR_LINE())  
            ,[Error_Location]           = ISNULL(@Error_Location,ERROR_MESSAGE())
             ,[Error_Message]            = ISNULL(@Error_Message,ERROR_MESSAGE())
             ,[SPID]                     = @@SPID -- SESSION_id/connection_number
             ,[Program_Name]             = ses.program_name
             ,[Client_Address]           = con.client_net_address
             ,[Authentication]           = con.auth_scheme           
            ,[Error_User_System]         = SUSER_SNAME()
             ,[Error_User_Application]   = @UserID

        FROM sys.dm_exec_sessions ses
         LEFT JOIN sys.dm_exec_connections con
             ON con.session_id = ses.session_id
         WHERE ses.session_id = @@SPID

    END TRY
     BEGIN CATCH
                  INSERT INTO ErrorLog
         (
             ERROR_NUMBER
             ,ERROR_LOCATION
             ,ERROR_MESSAGE
         )
         VALUES 
        (
             -100
             ,OBJECT_NAME(@@PROCID)
             ,'Error Log Procedure Errored out'
         )
     END CATCH

END
 GO

名为test_error_handler的表的触发器代码,有2列,ID和名称。

Create trigger tr_test_error_handler
 on test_error_handler
 for insert as
begin

BEGIN TRY
         if exists(select * from inserted where name='rim')
         raiserror('test error', 16,1) 
         rollback 
END TRY
BEGIN CATCH
        EXEC [ErrorLog_proc]
END CATCH

end 

请注意,如果我自己运行以下代码,errorlog表会被填充,但当代码在触发器体中时,这不会发生!!!!

BEGIN TRY
         if exists(select * from test_error_handler where name='rim')
         raiserror(50002, 16,1) 
         rollback 
END TRY
BEGIN CATCH
        EXEC [ErrorLog_Ins_Error_Dtl]
END CATCH

0 个答案:

没有答案