【问题标题】:Trigger not working when inserting multiple records插入多条记录时触发器不起作用
【发布时间】:2015-04-28 08:34:45
【问题描述】:

当我在表 Pedidos 上插入一条记录时,以下触发器正常工作。

但是,当我插入多条记录时,我收到 512 错误消息。我搜索了有关插入多个记录和触发器的详细信息,但没有找到我的问题的答案。

触发器读取插入的记录并从其他表中查找值以修改表planificaciones中的列situacion的值。

我尝试这样做的方式完全错了吗?我的触发器有什么明显的问题吗?

CREATE TRIGGER TRG_INS_PL_SYNC_STATUS_PLA ON dbo.pedidos after insert as begin if @@ROWCOUNT = 0
    return
set nocount on
declare @v_idpla int,
        @v_situacion nvarchar(12),
        @v_nombre nvarchar(50),
        @v_almacen nvarchar(50), 
        @v_status_pedido nvarchar(4);   

set @v_almacen = (select  almacen_pedido from inserted);
set @v_nombre =(select cliente from inserted);
set @v_status_pedido = (select status_pedido from inserted); 
set @v_situacion = (select top 1 nombre from dbo.StatusPlanificacion 
                    where STATUS_PEDIDO = @v_status_pedido);
set @v_idpla = (select top 1 id from dbo.Planificaciones 
                where dia_entrega >= GETDATE() and situacion <> 'Departed'  
                      and nombre like '%'+@v_almacen +'%'+ @v_nombre);
if(@v_idpla is not null)
    begin
        --select Timespan=SYSDATETIME() from inserted;
        select @@rowcount; 
        UPDATE DBO.Planificaciones
        SET situacion = @v_situacion
        WHERE id = @v_idpla;
    end
end

更新和解决:查看 tanner 的建议,我会在代码和工作上进行下一次更新,但我认为有人会发现这更清晰和有用。在 tanner 的建议中,说光标不是最好的方法,最好的选择是加入。在我的情况下,这个插入永远不会同时超过 50 个插入。

    CREATE TRIGGER TRG_INS_PL_SYNC_STATUS_PLA
    ON dbo.pedidos
    after insert as
    begin

declare @v_idpla int,@v_situacion nvarchar(12),@v_nombre nvarchar(50),@v_almacen nvarchar(50), @v_status_pedido nvarchar(4) 
DECLARE c_cursor CURSOR FAST_FORWARD FOR SELECT ALMACEN_PEDIDO, CLIENTE, STATUS_PEDIDO FROM INSERTED;
OPEN c_cursor
fetch next from c_cursor into @v_almacen,@v_nombre,@v_status_pedido
--declared and open cursor chargin values to variables
while @@fetch_status = 0
begin
    -- set values to variables from anothers tables
    set @v_situacion = (select top 1 nombre from dbo.StatusPlanificacion where STATUS_PEDIDO = @v_status_pedido);
    set @v_idpla = (select top 1 id from dbo.Planificaciones where dia_entrega >= GETDATE() and
        situacion <> 'Departed' and nombre like '%'+@v_almacen +'%'+ @v_nombre);
    --check value not null for assigned variable and do update to the value
    if(@v_idpla is not null)
        begin
            UPDATE DBO.Planificaciones
            SET situacion = @v_situacion
            WHERE id = @v_idpla;
        end 
       --move to the next row of cursor
       fetch next from c_cursor into @v_almacen,@v_nombre,@v_status_pedido
end
CLOSE c_cursor
DEALLOCATE c_cursor
end

【问题讨论】:

  • 您的代码需要重构,因为它错误地假设inserted 将只包含一条记录。在一批中插入多条记录时不会出现这种情况。
  • 感谢 Jaco 的回复,您能否建议我在一个批次中插入多个记录的样本,使用触发器查询另一个表以查找并将值应用于更新语句?
  • 检查 Tanner 链接,谢谢。
  • “在我的情况下,这个插入永远不会超过 50 次插入”——是的,但是有多频繁,同时有多少用户?说真的,您应该真正尝试并学习如何以基于集合的方式而不是以逐行方式解决此类问题。我不能保证@mxix 的建议的正确性,但这是触发器应该如何完成其​​工作的一个例子:尽可能少的步骤。

标签: sql-server triggers


【解决方案1】:

不确定代码是否 100% 正确,但应该给你一个想法..

inserted 是包含该批次所有行的数据集。你只需要考虑基于集合的操作。

CREATE TRIGGER TRG_INS_PL_SYNC_STATUS_PLA
ON dbo.pedidos
    AFTER INSERT
AS
BEGIN
    UPDATE p
        SET 
            situacion =  i.nombre
    FROM DBO.Planificaciones p
    INNER JOIN (
        SELECT
            v_idpla.id
            v_situacion.nombre
        FROM INSERTED I
        CROSS APPLY (
            select top 1 
                SP.nombre 
            from dbo.StatusPlanificacion SP
            where 
                SP.STATUS_PEDIDO = I.STATUS_PEDIDO
        ) v_situacion
        CROSS APPLY (
            select top 1 
                Pla.id 
            from dbo.Planificaciones Pla
            where 
                Pla.dia_entrega >= GETDATE() and
                Pla.situacion <> 'Departed' and 
                Pla.nombre like '%'+I.ALMACEN_PEDIDO +'%'+ I.CLIENTE
        ) v_idpla
    ) I ON
        P.id = I.id
END

【讨论】:

  • Mxix 感谢您的宝贵时间,它工作得很好。只需将“SET situacion = v_situacion.nombre”更改为“SET situacion = i.nombre”。测试和工作!!!
猜你喜欢
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
相关资源
最近更新 更多