触发器执行期间出错。

时间:2014-02-13 07:38:39

标签: .net sql-server triggers sqlclient

由于我的声誉,我不能在这里添加屏幕截图。这是我得到的错误:

错误来源:.NET SqlClient数据提供程序 错误消息:触发器执行期间出现错误。批处理已中止,用户事务(如果有)已回滚。

这是我的存储过程。

ALTER PROCEDURE [dbo].[uspLogError]
AS
BEGIN
    SET NOCOUNT ON;

-- Output parameter value of 0 indicates that error 
-- information was not logged.

    BEGIN TRY
    -- Return if there is no error information to log.
        IF ERROR_NUMBER() IS NULL
            RETURN;

    -- Return if inside an uncommittable transaction.
    -- Data insertion/modification is not allowed when 
    -- a transaction is in an uncommittable state.
        IF XACT_STATE() = -1
            BEGIN
                PRINT 'Cannot log error since the current transaction is in an uncommittable state. '
                    + 'Rollback the transaction before executing uspLogError in order to successfully log error information.';
                RETURN;
            END;

 DECLARE 
    @ErrorMessage    NVARCHAR(4000),
    @ErrorNumber     INT,
    @ErrorSeverity   INT,
    @ErrorState      INT,
    @ErrorLine       INT,
    @ErrorProcedure  NVARCHAR(200);

-- Assign variables to error-handling functions that 
-- capture information for RAISERROR.
SELECT 
    @ErrorNumber = ERROR_NUMBER(),
    @ErrorSeverity = ERROR_SEVERITY(),
    @ErrorState = ERROR_STATE(),
    @ErrorLine = ERROR_LINE(),
    @ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-');

-- Build the message string that will contain original
-- error information.
SELECT @ErrorMessage = 
    N'Error %d, Level %d, State %d, Procedure %s, Line %d, ' + 
        'Message: '+ ERROR_MESSAGE();

        INSERT  [dbo].[SQLErrorLog]
                ( [UserName] ,
                  [ErrorTime] ,
                  [ErrorNumber] ,
                  [ErrorSeverity] ,
                  [ErrorState] ,
                  [ErrorProcedure] ,
                  [ErrorLine] ,
                  [ErrorMessage]
                )
        VALUES  ( CONVERT(SYSNAME, CURRENT_USER) ,
                  GETDATE() ,
                  @ErrorNumber ,
                  @ErrorSeverity ,
                  @ErrorState ,
                  @ErrorProcedure ,
                  @ErrorLine ,
                  @ErrorMessage
                );


    END TRY
    BEGIN CATCH
        PRINT 'An error occurred in stored procedure uspLogError: ';
        EXECUTE [dbo].[uspPrintError];
        RETURN -1;
    END CATCH

-- Raise an error: msg_str parameter of RAISERROR will contain
-- the original error information.
    RAISERROR 
    (
    @ErrorMessage, 
    @ErrorSeverity, 
    1,               
    @ErrorNumber,    -- parameter: original error number.
    @ErrorSeverity,  -- parameter: original error severity.
    @ErrorState,     -- parameter: original error state.
    @ErrorProcedure, -- parameter: original error procedure name.
    @ErrorLine       -- parameter: original error line number.
    );
END; 

0 个答案:

没有答案