MSSQL-更新语句基于SSMS版本的不同结果

问题描述 投票:-1回答:1

我有一个表,当记录更新时会触发触发器。当我使用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

END

如果我禁用语句select * into #inserted from inserted,则不必这样做,它仍然会给出相同的错误。

有人能解释为什么在SSMS 18.4中而不是在SSMS 11.03中引发错误,并且根据错误消息从触发器返回哪个语句?我会把select * into #inserted from inserted放在一边,但是当将其从触发器中删除时,它仍然会给出相同的错误,因此必须是其他错误。

我有一个表,当记录更新时会触发触发器。当我使用SSMS 18.4使用DML语句更新记录时,由于错误'事务返回了...

sql-server ssms
1个回答
1
投票

我想我找到了根本原因。我在SSMS中启用了“包括实际执行计划”选项。关闭此选项后,更新语句在SSMS 18.4中运行没有问题。在SSMS 11.03中启用它时,它会因相同的原因而失败.....对我来说,这很奇怪,但这可能是由于我缺乏一些深入的SQL知识]

© www.soinside.com 2019 - 2024. All rights reserved.