sqlserver 異常 事務

ForTechnology發表於2013-03-14

sqlserver 2005

USE AdventureWorks;
GO

BEGIN TRY
   
-- Generate a divide-by-zero error.
    SELECT 1/0;
END TRY
BEGIN CATCH
   
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;
GO 

 

USE AdventureWorks;
GO
BEGIN TRANSACTION;

BEGIN TRY
   
-- Generate a constraint violation error.
    DELETE FROM Production.Product
       
WHERE ProductID = 980;
END TRY
BEGIN CATCH
   
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;

   
IF @@TRANCOUNT > 0
       
ROLLBACK TRANSACTION;
END CATCH;

IF @@TRANCOUNT > 0
   
COMMIT TRANSACTION;
GO

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25897606/viewspace-756166/,如需轉載,請註明出處,否則將追究法律責任。

相關文章