正确使用事务提交数据并回滚

CREATE TABLE test1(
    fid VARCHAR(2) NULL    
)



BEGIN TRY
    
BEGIN 

 TRAN
INSERT INTO test1(fid) VALUES('1')
INSERT INTO test1(fid) VALUES('222')
INSERT INTO test1(fid) VALUES('3')
COMMIT TRANSACTION
END TRY
BEGIN CATCH

IF @@TRANCOUNT>0
 ROLLBACK TRANSACTION
    /* 
        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

 

posted on 2014-07-02 09:44  ~紫鱼~  阅读(555)  评论(0编辑  收藏  举报