而不是插入子查询返回的值超过1

时间:2013-04-25 15:21:17

标签: sql-server triggers

我遇到下面的触发器问题。我相信问题是当多个行一次添加到数据库时。

它需要适用于插入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

1 个答案:

答案 0 :(得分:0)

是否将union的第一部分更改为此帮助?

SELECT "Status" =
            CASE
                WHEN k.isPersonalised = 1 THEN '0'    --PERSONALISED CD
                ELSE '3'                            -- NON PERSONALISED CD
            END
            FROM KSAT_CDs k
            inner join inserted i on k.ID = substring(i.ESHID, 5, 10)
            where i.ESHID like 'PHZB%'