sql-server - 当sql server 2005中的try catch捕获错误时如何防止事务回滚

标签 sql-server sql-server-2005 try-catch sqltransaction

我有一个包含记录的表 Table1

Table1
-------------------------------
ID  F1
-------------------------------
01  1
02  OK
03  52
04  53
05  Null
------------------------------

这里我想将F1 Varchar的数据类型更改为Decimal(3,0); 然后我创建一个具有新结构的新表。

我想通过将 varchar 显式转换为十进制,将旧表的每个记录插入新表中。 所以我在 try catch block 中编写这个命令。如果生成错误,那么在 catch block 中我可以用默认值填充它。 但这整个工作是在开始事务和提交事务 block 之间执行的。 因此,当 catch block 中捕获错误时,无法执行进一步的工作,会发生错误

The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction.

即使在 try catch block 中发生错误,如何防止此事务回滚。

最佳答案

你不能。您必须始终check the XACT_STATE and act accordingly :

Uncommittable Transactions and XACT_STATE
If an error generated in a TRY block causes the state of the current transaction to be invalidated, the transaction is classified as an uncommittable transaction. An error that ordinarily ends a transaction outside a TRY block causes a transaction to enter an uncommittable state when the error occurs inside a TRY block. An uncommittable transaction can only perform read operations or a ROLLBACK TRANSACTION. The transaction cannot execute any Transact-SQL statements that would generate a write operation or a COMMIT TRANSACTION. The XACT_STATE function returns a value of -1 if a transaction has been classified as an uncommittable transaction. When a batch finishes, the Database Engine rolls back any active uncommittable transactions. If no error message was sent when the transaction entered an uncommittable state, when the batch finishes, an error message will be sent to the client application. This indicates that an uncommittable transaction was detected and rolled back.

关于sql-server - 当sql server 2005中的try catch捕获错误时如何防止事务回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14056142/

相关文章:

sql - 在 SQL 2005 中的 WHERE 子句中使用命名的自定义列?

sql-server - 在 SQL Server 2005 中,是否可以为每个用户设置事务隔离级别?

sql-server - Json 到 SQL Server 2008 中的表

SQL 将列转换为逗号分隔的行

sql-server-2005 - "Type DATE is not a defined system type."

java - 在java中获取从 'try'到 'catch'的输入

javascript - 上传后图片src不变

java - Java中的while-try-catch

sql-server - 按连接表上的性能问题排序

mysql - 在 Mule JDBC 流程中使用两个不同数据库时出现问题