sql tsql__try_catch.sql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql tsql__try_catch.sql相关的知识,希望对你有一定的参考价值。

BEGIN TRY
	BEGIN TRANSACTION



	COMMIT TRANSACTION;
	PRINT CHAR(10) + '*** DONE ***';
END TRY
BEGIN CATCH

    IF (@@TRANCOUNT > 0)
    	ROLLBACK TRANSACTION;

    PRINT CHAR(10) + '*** UPDATE FAILED ***';

    SELECT
         ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage;
END CATCH




BEGIN TRY
    BEGIN TRANSACTION



    COMMIT TRANSACTION;
    PRINT CHAR(10) + '*** DONE ***';
END TRY
BEGIN CATCH

    IF (@@TRANCOUNT > 0)
        ROLLBACK TRANSACTION;

    PRINT CHAR(10) + '*** UPDATE FAILED ***';

    PRINT CHAR(10);
    PRINT 'Error Number: ' + CAST(ERROR_NUMBER() AS nvarchar);
    PRINT 'Error Severity: ' + CAST(ERROR_SEVERITY() AS nvarchar);
    PRINT 'Error State: ' + CAST (ERROR_STATE() AS nvarchar);
    PRINT 'Error Procedure: ' + CAST(ERROR_PROCEDURE AS varchar);
    PRINT 'Error Line: ' + CAST(ERROR_LINE() AS nvarchar);
    PRINT 'Error Message: ' + CAST(ERROR_MESSAGE() AS nvarchar);
END CATCH



PRINT CHAR(10);
PRINT 'Error Number: ' + CAST(ERROR_NUMBER() AS varchar);
PRINT 'Error Severity: ' + CAST(ERROR_SEVERITY() AS varchar);
PRINT 'Error State: ' + CAST (ERROR_STATE() AS varchar);
PRINT 'Error Procedure: ' + CAST(ERROR_PROCEDURE AS varchar);
PRINT 'Error Line: ' + CAST(ERROR_LINE() AS varchar);
PRINT 'Error Message: ' + CAST(ERROR_MESSAGE() AS varchar);

以上是关于sql tsql__try_catch.sql的主要内容,如果未能解决你的问题,请参考以下文章

sql tsql__get_schema.sql

sql tsql__template_create_database.sql

sql tsql__SQLCMD_mode_check.sql

sql tsql__get_lock_history.sql

sql tsql__kill_idle_session.sql

sql tsql__kill_db_connections.sql