您的位置:首页 > 数据库 > SQL

SQL 2005 Tips --事务处理The current transaction cannot be committed and cannot support operations that write to the log file‏

2009-09-23 20:09 911 查看
Error Message:The current transaction cannot be committed and cannot support operations that write to the log file‏

1. Bad Way:

USE tempdb
GO

----(1)
CREATE TABLE TA(ID INT)
CREATE TABLE TB(ID INT)
GO

BEGIN TRY
BEGIN TRAN
INSERT TA SELECT 'TA'
COMMIT TRAN
END TRY

BEGIN CATCH
PRINT 'TranCount: ' + CONVERT(varchar(10),@@TRANCOUNT) + ' State:' + CONVERT(varchar(10),XACT_STATE())
INSERT TB SELECT 'TB'
ROLLBACK TRAN
END CATCH
GO

DROP TABLE TA
DROP TABLE TB
GO
Msg 3930, Level 16, State 1, Line 10
The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction.
这个问题,一般是由于在事务中,事务出现错误被CATCH到以后,在CATCH部分处理时,并没有及时回滚事务 而是在之前,又进行其他的操作,导致隐式事务提交和显式事务不能提交的冲突,从而抛出这样的错误信息。

2. Fixed:
USE tempdb
GO

CREATE TABLE TA(ID INT)
CREATE TABLE TB(ID INT)
GO

BEGIN TRY
BEGIN TRAN

INSERT TA SELECT 'TA'

COMMIT TRAN
END TRY

BEGIN CATCH
ROLLBACK TRAN
--先把事务给回滚,再做其他操作
PRINT 'TranCount: ' + CONVERT(varchar(10),@@TRANCOUNT) + ' State:' + CONVERT(varchar(10),XACT_STATE())
INSERT TB SELECT 'TB'
END CATCH
GO

DROP TABLE TA
DROP TABLE TB
GO

You will get the message:
Msg 245, Level 16, State 1, Line 15
Conversion failed when converting the varchar value 'TB' to data type int.

/*
========================================================
========================================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐