Sql 服务器。当前事务无法提交,无法支持写入日志文件的操作

问题描述 投票:0回答:2
begin tran
begin try
  select case when 1=0 then 0.0 else '' end --this will not work
end try
begin catch
--error has occured. But it doesnt matter. We want to continue anyway
end catch

select 1 --do something else

commit --unfortunatelly this producess error "The current transaction cannot be committed and cannot support operations that write to the log file"

我可以做什么来提交交易? 我知道,

select case when 1=0 then 0.0 else '' end
不正确。这就是为什么它位于 try/catch 块中(在实际情况下,这是由管理员定义的查询)。但我想提交其余的操作。

编辑:// 如果“不正确的查询”是例如,代码可以按我想要的方式工作

select 1/0

begin tran
begin try
  select 1/0-- this will not work
end try
begin catch
--error has occured. But it doesnt matter. We want to continue anyway
end catch

select 1--do something else
commit --commit is done without any errors
sql-server t-sql transactions
2个回答
0
投票

来自此页面

“如果 TRY 块中生成的错误导致当前事务的状态无效,则该事务被归类为不可提交的事务。通常在 TRY 块之外结束事务的错误会导致事务在以下情况下进入不可提交状态:错误发生在 TRY 块内。不可提交的事务只能执行读操作或 ROLLBACK TRANSACTION。”

来自此页面

“SQL Server 可以容忍事务中的某些错误,而无需将其标记为不可提交。例如 SELECT 1/0 会导致错误,但不会强制事务进入不可提交状态。”


0
投票

您可以尝试在提交或退出之前检查事务状态..

begin tran
begin try
    select case when 1=0 then 0.0 else '' end --this will not work
end try
begin catch
--error has occured. But it doesnt matter. We want to continue anyway
end catch

select 1 --do something else

IF (XACT_STATE()) = -1
    rollback tran
IF (XACT_STATE()) = 1
    commit tran
© www.soinside.com 2019 - 2024. All rights reserved.