我知道这可能是重复的问题。参考answer in this question,它使用触发器中的游标来处理多行插入,这可能会降低性能。应该有Docs here的解决方案,但我不知道如何让它工作。多行插入后是否还有其他方法可以触发?
以下是我的触发器示例:
splitted = Str.split('.')
# splitted[0] will be 'i like cows'
# splitted[1] will be 'I also like camels'
以下是insert语句的示例:
ALTER TRIGGER [dbo].[UpdateTaskStatus]
ON [dbo].[STG_CANCEL_TASK]
AFTER INSERT
AS
BEGIN
DECLARE @NO_HC VARCHAR (20) = NULL;
DECLARE @taskId VARCHAR (80) = NULL;
DECLARE @START_DATE DATETIME = NULL;
SELECT @NO_HC = RTRIM(LTRIM(INSERTED.NO_HC)) FROM INSERTED;
SELECT @START_DATE = START_DTM
FROM MSS.dbo.TR_TASK
WHERE NO_HC = @NO_HC
SET @taskId = (SELECT TASK_ID
FROM MSS.dbo.TR_TASK
WHERE NO_HC = @NO_HC)
IF (@START_DATE IS NOT NULL)
BEGIN
UPDATE MCS.dbo.STG_CANCEL_TASK
SET RESULT = 'Error',
TASK_ID = @taskId
WHERE NO_HC = @NO_HC
END
ELSE
BEGIN
UPDATE MCS.dbo.STG_CANCEL_TASK
SET RESULT = 'Success',
TASK_ID = @taskId
WHERE NO_HC = @NO_HC
END
END;
更新:目前,我将触发器修改为类似的内容,现在可以使用了。我不确定这是否正确。
INSERT INTO STG_CANCEL_TASK (NO_HC)
VALUES ('2018032801'),
('2018032802'),
('2018032803'),
('2018032804'),
('2018032805')
答案 0 :(得分:2)
下面的内容将有助于
UPDATE M
SET m.result = CASE
WHEN i.result IS NULL
THEN 'error'
ELSE 'success'
END
FROM inserted i
JOIN MCS.dbo.STG_CANCEL_TASK M ON m.task_id-i.task_id
AND m.NO_HC = RTRIM(LTRIM(i.NO_HC))
AND m.START_DTM = i.START_DTM