从2个不同的基础创建2个表的触发器

时间:2017-07-19 13:30:51

标签: sql-server database triggers insert

您好我正在处理触发器。此触发器能够将我的表DOUBLON中插入的数据与基数F进行比较,将来自基数T的CLIENT数据进行比较。 如果有类似的TEL,TELPOR,TELPRO,TELDOM,那么它更新F.DOUBLON.doublon =' OUI'否则它会加入“非”#。 我在桌子DOUBLON上的基础F上创建了触发器。

我做了什么:

    CREATE TRIGGER DoublonInserted
    ON  dbo.DOUBLON
    AFTER INSERT
    AS 
    DECLARE @doublon INTEGER
    SELECT @doublon = 0
    IF SELECT count(C.TEL)
    FROM T.dbo.CLIENT AS C , inserted AS I
    where I.TEL = C.TEL
    OR I.TEL = C.TELDOM
    OR I.TEL = C.TELPRO
    OR I.TEL = C.TELPOR
    OR I.TELDOM = C.TEL
    OR I.TELDOM = C.TELDOM
    OR I.TELDOM = C.TELPRO
    OR I.TELDOM = C.TELPOR
    OR I.TELPRO = C.TEL
    OR I.TELPRO = C.TELDOM
    OR I.TELPRO = C.TELPRO
    OR I.TELPRO = C.TELPOR
    OR I.TELPOR = C.TEL
    OR I.TELPOR = C.TELDOM
    OR I.TELPOR = C.TELPRO
    OR I.TELPOR = C.TELPOR) > 1
    BEGIN
    UPDATE dbo.DOUBLON
    SET DOUBLON = 'OUI' --, @doublon = 0 
    FROM T.dbo.CLIENT AS C, inserted AS I
    where I.TEL = C.TEL
    OR I.TEL = C.TELDOM
    OR I.TEL = C.TELPRO
    OR I.TEL = C.TELPOR
    OR I.TELDOM = C.TEL
    OR I.TELDOM = C.TELDOM
    OR I.TELDOM = C.TELPRO
    OR I.TELDOM = C.TELPOR
    OR I.TELPRO = C.TEL
    OR I.TELPRO = C.TELDOM
    OR I.TELPRO = C.TELPRO
    OR I.TELPRO = C.TELPOR
    OR I.TELPOR = C.TEL
    OR I.TELPOR = C.TELDOM
    OR I.TELPOR = C.TELPRO
    OR I.TELPOR = C.TELPOR 
    END
    ELSE
    BEGIN
    UPDATE dbo.DOUBLON
    SET DOUBLON = 'NON' --, @doublon = 0
    FROM T.dbo.CLIENT AS C, inserted AS I
    where I.TEL != C.TEL
    OR I.TEL != C.TELDOM
    OR I.TEL != C.TELPRO
    OR I.TEL != C.TELPOR
    OR I.TELDOM != C.TEL
    OR I.TELDOM != C.TELDOM
    OR I.TELDOM != C.TELPRO 
    OR I.TELDOM != C.TELPOR
    OR I.TELPRO != C.TEL
    OR I.TELPRO != C.TELDOM
    OR I.TELPRO != C.TELPRO
    OR I.TELPRO != C.TELPOR
    OR I.TELPOR != C.TEL
    OR I.TELPOR != C.TELDOM
    OR I.TELPOR != C.TELPRO
    OR I.TELPOR != C.TELPOR
    END

事实上它的工作"不在现实中:

案例工作。 如果我的插页中有任何重复,触发设置' NON'在我的列中插入每行的双倍。 如果我的插入中只有重复,触发器设置' OUI'在我的专栏中,每插入一行都会加倍。

案例不起作用。 如果存在重复且未插入重复,则无效。

我试图重置我的var @doublon,但仍然一样。

我注意到,我的触发器将我测试的最后一个结果放在了所有行的列上。如果最新的插入是重复数据,则所有行都将具有'是' (反之亦然)。

我想帮助解释一下原因或其他方法(使用触发器)。

我无法触摸数据库T(禁止添加列等)!

++ Bases位于同一服务器上++

感谢您的时间和您的帮助!

编辑:

表格客户端

Table CLIENT

表DOUBLON

Table DOUBLON

ID_CLIENT - > INT

我想要的: 在Doublon上设置正确的值

其他一切varchar(50)

3 个答案:

答案 0 :(得分:1)

我想我明白你现在的样子。以下是如何将其创建为触发器而不是触发器。这应该非常接近。

CREATE TRIGGER DoublonInserted 
    ON dbo.DOUBLON
    INSTEAD OF INSERT AS 
BEGIN
    SET NOCOUNT ON;

    INSERT DOUBLON
    (
        ID_CLIENT
        , NOM
        , PRENOM
        , TEL
        , TELDOM
        , TELPRO
        , TELPOR
        , DOUBLON
    )
    select i.ID_CLIENT
        , i.NOM
        , i.PRENOM
        , i.TEL
        , i.TELDOM
        , i.TELPRO
        , i.TELPOR
        , case when 
                I.TEL = C.TEL
                OR I.TEL = C.TELDOM
                OR I.TEL = C.TELPRO
                OR I.TEL = C.TELPOR
                OR I.TELDOM = C.TEL
                OR I.TELDOM = C.TELDOM
                OR I.TELDOM = C.TELPRO
                OR I.TELDOM = C.TELPOR
                OR I.TELPRO = C.TEL
                OR I.TELPRO = C.TELDOM
                OR I.TELPRO = C.TELPRO
                OR I.TELPRO = C.TELPOR
                OR I.TELPOR = C.TEL
                OR I.TELPOR = C.TELDOM
                OR I.TELPOR = C.TELPRO
                OR I.TELPOR = C.TELPOR
            THEN 'OUI'
            ELSE 'NON'
            END
    FROM inserted AS I
    join T.dbo.CLIENT AS C on c.ID_CLIENT = i.ID_CLIENT;
END

细节上仍然很模糊,但我认为你想要这样的东西。

CREATE TRIGGER DoublonInserted 
    ON dbo.DOUBLON
    INSTEAD OF INSERT AS 
BEGIN
    SET NOCOUNT ON;

    INSERT DOUBLON
    (
        ID_CLIENT
        , NOM
        , PRENOM
        , TEL
        , TELDOM
        , TELPRO
        , TELPOR
        , DOUBLON
    )
    select i.ID_CLIENT
        , i.NOM
        , i.PRENOM
        , i.TEL
        , i.TELDOM
        , i.TELPRO
        , i.TELPOR
        ,  CASE 
                when C.TEL IS NULL
                    OR C.TELPRO IS NULL
                    OR C.TELDOM IS NULL
                    OR C.TELPOR IS NULL
                THEN 'NON'
                ELSE 'OUI'
            END
    FROM inserted AS I
    left join T.dbo.CLIENT AS C on I.TEL = C.TEL
                                OR I.TEL = C.TELPRO
                                OR I.TEL = C.TELDOM
                                OR I.TEL = C.TELPOR
END

答案 1 :(得分:0)

你只需要一个"找到一个双"查询:

CREATE TRIGGER DoublonInserted
ON  dbo.DOUBLON
AFTER INSERT
AS BEGIN
IF EXISTS(
    select * from
    FROM T.dbo.CLIENT AS C, inserted AS I
    where I.TEL = C.TEL
    OR I.TEL = C.TELDOM
    OR I.TEL = C.TELPRO
    OR I.TEL = C.TELPOR
    OR I.TELDOM = C.TEL
    OR I.TELDOM = C.TELDOM
    OR I.TELDOM = C.TELPRO
    OR I.TELDOM = C.TELPOR
    OR I.TELPRO = C.TEL
    OR I.TELPRO = C.TELDOM
    OR I.TELPRO = C.TELPRO
    OR I.TELPRO = C.TELPOR
    OR I.TELPOR = C.TEL
    OR I.TELPOR = C.TELDOM
    OR I.TELPOR = C.TELPRO
    OR I.TELPOR = C.TELPOR)
    UPDATE dbo.DOUBLON
    SET DOUBLON = 'OUI'
    WHERE ID_CLIENT = I.ID_CLIENT;
ELSE
    UPDATE dbo.DOUBLON
    SET DOUBLON = 'NON'
    WHERE ID_CLIENT = I.ID_CLIENT;
END

答案 2 :(得分:0)

好吧!这是“答案”:

    CREATE TRIGGER DoublonInserted 
ON dbo.DOUBLON
INSTEAD OF INSERT AS 
    BEGIN

INSERT INTO DOUBLON
(
    ID_CLIENT
    , NOM
    , PRENOM
    , TEL
    , TELDOM
    , TELPRO
    , TELPOR
    , DOUBLON
)
select i.ID_CLIENT
    , i.NOM
    , i.PRENOM
    , i.TEL
    , i.TELDOM
    , i.TELPRO
    , i.TELPOR
    ,  CASE 
            when I.TEL = C.TEL
OR I.TEL = C.TELDOM
OR I.TEL = C.TELPRO
OR I.TEL = C.TELPOR
OR I.TELDOM = C.TEL
OR I.TELDOM = C.TELDOM 
OR I.TELDOM = C.TELPRO
OR I.TELDOM = C.TELPOR
OR I.TELPRO = C.TEL
OR I.TELPRO = C.TELDOM 
OR I.TELPRO = C.TELPRO
OR I.TELPRO = C.TELPOR
OR I.TELPOR = C.TEL
OR I.TELPOR = C.TELDOM 
OR I.TELPOR = C.TELPRO
OR I.TELPOR = C.TELPOR
            THEN 'OUI'
            ELSE 'NON'
        END
FROM inserted AS I
left join T.dbo.CLIENT AS C 
on I.TEL = C.TEL
OR I.TEL = C.TELDOMICIL
OR I.TEL = C.TELPRO
OR I.TEL = C.TELPOR
OR I.TELDOM = C.TEL
OR I.TELDOM = C.TELDOM
OR I.TELDOM = C.TELPRO
OR I.TELDOM = C.TELPOR
OR I.TELPRO = C.TEL
OR I.TELPRO = C.TELDOM
OR I.TELPRO = C.TELPRO
OR I.TELPRO = C.TELPOR
OR I.TELPOR = C.TEL
OR I.TELPOR = C.TELDOM
OR I.TELPOR = C.TELPRO
OR I.TELPOR = C.TELPOR
END

感谢@Sean Lange(你做到了!),@ Bohemian♦以及其他所有人!