我已经测试了该触发器,但它仅适用于一个插入行。
触发器因多行插入而失败,我没有找到修复触发器的语法。
有什么建议可以解决这个问题吗?
感谢让我注意到问题的 stackoverflow 用户。
USE [MY_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tr_update_father]
ON [dbo].[PROD_IVR_CALL]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @tnumber nvarchar(50), @id_inserted int, @id_prod_file int;
select
@id_inserted = ins.id,
@tnumber = ins.tnumber ,
@id_prod_file = pf.ID_PROD_FILE
from inserted ins
inner join prod_failure pf on (ins.ID_PROD_FAILURE = pf.ID);
update prod_ivr_call
set id_father = sq.ID
from
(select min(pic.id) as ID
from prod_ivr_call pic
inner join prod_failure pf on (pic.ID_PROD_FAILURE = pf.ID)
where pic.tnumber = @tnumber
and pic.is_requested = 0
and pf.ID_PROD_FILE = @id_prod_file
group by pic.tnumber ) sq
END