我有这个触发器,它在回滚时返回错误,错误是“事务在触发器中结束。批处理已经中止。”
create trigger trigger1 on payments
for insert
as
set nocount on ;
begin
if(select COUNT(*) from customers c , inserted i where c.customer_id = i.customer_id) = 0
begin
rollback tran;
print 'Customer not found'
end
else
print 'ok'
end
答案 0 :(得分:0)
根据评论尝试从触发器中提出错误而不是回滚(OR)回滚然后抛出/引发错误并从触发器返回,如下所示
create trigger trigger1 on payments
for insert
as
set nocount on;
begin
if(select COUNT(*) from customers c join inserted i
on c.customer_id = i.customer_id) = 0
begin
declare @errmsg VARCHAR(MAX), @sev AS INT, @state AS INT
SELECT @errmsg = ERROR_MESSAGE(), @sev = ERROR_SEVERITY(),
@state = ERROR_STATE()
print 'Customer not found'
rollback transaction
raiserror(@errmsg,@sev,@state);
return
end
else
print 'ok'
end