下面的触发器有问题。我认为问题在于一次将多行添加到数据库中。
它需要适用于插入的 n* 行。
代码的意思是在插入时检查 ESHID 列与另一个表的 ID 在 4 个字符后开始
因此,如果有匹配项并且 isPersonalised 为 0,那么我需要将状态设置为 3,否则它应该保持与原始 FulfilmentStatus 相同。
ALTER TRIGGER [dbo].[setStatus]
ON [dbo].[ESH_OrderLine]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO ESH_OrderLine(
OrderID,
ESHID,
ESHVersion,
GBPPrice,
Currency,
FulfilmentStatus,
FulfilmentDate
)SELECT
OrderID,
ESHID,
ESHVersion,
GBPPrice,
Currency,
(
SELECT "Status" =
CASE
WHEN isPersonalised = 1 THEN '0' --PERSONALISED CD
ELSE '3' -- NON PERSONALISED CD
END
FROM KSAT_CDs
WHERE ID= SUBSTRING( (
SELECT ESHID FROM INSERTED
WHERE ESHID like 'PHZB%'
),5,10)
UNION
SELECT "Status" =
CASE
WHEN ESHID = 'PNP' THEN '0' --POSTAGE AND PACKING
ELSE '4' --DOWNLOAD
END
FROM INSERTED
WHERE ESHID NOT LIKE 'PHZB%'
) as FulfilmentStatus,
FulfilmentDate
FROM
INSERTED
END