我有一个表,当记录更新时触发触发器。当我使用 SSMS 18.4 使用 DML 语句更新记录时,由于错误'A trigger returned a resultset and the server option 'disallow results from triggers' is true.'
,事务将回滚
当我从 SSMS 11.03 触发相同的语句时,将存储更新而不会引发上述错误消息。
以下是触发代码:
DECLARE @iCntrMTL varchar(20),
@iCntrLOD varchar(20),
@iCntrWeight int,
@iCntrID int,
@iCntrEmpty bit,
@iCntrDestination varchar(150),
@iCntrDestBOLLOD varchar(20),
@iCntrEmptyWeight int,
@iCntrToFill bit,
@iCntrUseForLoading bit,
@pCntrMTL varchar(20),
@pCntrLOD varchar(20),
@pCntrWeight int,
@pCntrID int,
@pCntrEmpty bit,
@pCntrDestination varchar(150),
@pCntrDestBOLLOD varchar(20),
@pCntrEmptyWeight int,
@pCntrToFill bit,
@pCntrUseForLoading bit,
@AuditText varchar(2500)
select * into #inserted from inserted
if update(cntrweight) or update(cntrMTL) or update(cntrlod) or update(CntrDestination) or update(CntrDestBOLLOD) or update(cntrempty)
or update(cntruseforloading) or update(cntrtofill) or update(cntremptyweight)
BEGIN
select @iCntrid = cntrid,
@iCntrMTL = cntrmtl,
@icntrlod = cntrlod,
@iCntrWeight = CntrWeight,
@iCntrDestination = CntrDestination,
@iCntrDestBOLLOD = CntrDestBOLLOD,
@iCntrEmpty = CntrEmpty,
@iCntrUseForLoading = CntrUseForLoading,
@iCntrToFill = CntrToFill,
@iCntrEmptyWeight = CntrEmptyWeight
from inserted
select @pCntrid = cntrid,
@pCntrMTL = cntrmtl,
@pcntrlod = cntrlod,
@pCntrWeight = CntrWeight,
@pCntrDestination = CntrDestination,
@pCntrDestBOLLOD = CntrDestBOLLOD,
@pCntrEmpty = CntrEmpty,
@pCntrUseForLoading = CntrUseForLoading,
@pCntrToFill = CntrToFill,
@pCntrEmptyWeight = CntrEmptyWeight
from deleted
--select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '#inserted'
--select * from #inserted
if @pCntrMTL != @iCntrMTL
set @AuditText = isnull(@AuditText,'') + 'Prev.CntrMTL=' + isnull(@pCntrMTL,'') + ' -- New.CntrMTL= ' + isnull(@iCntrMTL ,'') + ' || '
--Set @AuditText = 'Prev.CntrMTL=' + isnull(@pCntrMTL,'') + ' -- New.CntrMTL= ' + isnull(@iCntrMTL ,'') + ' || '
if @pCntrLOD != @iCntrLOD
Set @AuditText = isnull(@AuditText,'') + 'Prev.CntrLOD=' + isnull(@pCntrLOD,'') + ' -- New.CntrLOD= ' + isnull(@iCntrLOD,'') + ' || '
if @pCntrWeight != @iCntrWeight
Set @AuditText = isnull(@AuditText,'') + 'Prev.CntrWeight=' + convert(varchar(10),isnull(@pCntrWeight,'')) + ' -- New.CntrWeight= ' + convert(varchar(10),isnull(@iCntrWeight,'')) + ' || '
if @pCntrDestination != @iCntrDestination
Set @AuditText = isnull(@AuditText,'') + 'Prev.CntrDestination=' + isnull(@pCntrDestination,'') + ' -- New.CntrDestination= ' + isnull(@iCntrDestination,'') + ' || '
if @pCntrDestBOLLOD != @iCntrDestBOLLOD
Set @AuditText = ISNULL(@AuditText,'') + 'Prev.CntrDestBOLLOD=' + isnull(@pCntrDestBOLLOD,'') + ' -- New.CntrDestBOLLOD= ' + isnull(@iCntrDestBOLLOD,'') + ' || '
if @pCntrEmpty != @iCntrEmpty
Set @AuditText = ISNULL(@AuditText,'') + 'Prev.CntrEmpty=' + cast(@pCntrEmpty as char(1)) + ' -- New.CntrEmpty= ' + cast(@iCntrEmpty as char(1)) + ' || '
if @pCntrUseForLoading != @iCntrUseForLoading
Set @AuditText = ISNULL(@AuditText,'') + 'Prev.CntrUseForLoading=' + cast(@pCntrUseForLoading as char(1)) + ' -- New.CntrUseForLoading= ' + cast(@iCntrUseForLoading as char(1)) + ' || '
if @pCntrToFill != @iCntrToFill
Set @AuditText = ISNULL(@AuditText,'') + 'Prev.CntrToFill=' + cast(@pCntrToFill as char(1)) + ' -- New.CntrToFill= ' + cast(@iCntrToFill as char(1)) + ' || '
if @pCntrEmptyWeight != @iCntrEmptyWeight
Set @AuditText = ISNULL(@AuditText,'') + 'Prev.CntrEmptyWeight=' + convert(varchar(10),isnull(@pCntrEmptyWeight,'')) + ' -- New.CntrEmptyWeight= ' + convert(varchar(10),isnull(@iCntrEmptyWeight,'')) + ' || '
IF @AuditText is not null
INSERT INTO ContainerMutaties (CmCntrID,CmTimeStamp, CmMutatie, cmcmcName) values (@iCntrid, getdate(), @AuditText, 'CHG')
END
结束
如果我禁用语句select * into #inserted from inserted
,这不是必需的,它仍然会给出相同的错误。
有人可以解释为什么在 SSMS 18.4 中而不是在 SSMS 11.03 中引发错误,以及根据错误消息从触发器返回哪个语句?我会select * into #inserted from inserted
,但是当它从触发器中删除时,它仍然会给出相同的错误,因此它一定是其他错误。
我想我找到了根本原因。我在SSMS中启用了"包括实际执行计划"选项。关闭此选项后,更新语句在 SSMS 18.4 中运行时没有问题。在SSMS 11.03中启用它时,它将以相同的原因失败.....对我来说,这很奇怪,但这可能是由于我缺乏一些深入的SQL知识