使用内部联接构造更新触发器

时间:2015-02-01 14:52:10

标签: sql triggers inner-join

我不确定这一切是否正确 我想知道建筑和内部联接在哪里

USE x;
GO
IF EXISTS (SELECT typ1
    FROM db.y
    WHERE typ1 ='wartosc' and typ2 = 'wartosc')

DROP TRIGGER triggername;
GO
CREATE TRIGGER triggername
ON [db.jakiasbaza][y.typ]
AFTER UPDATE
AS
declare @zmienna1   
declare @zmienna2 varchar(20)
IF (UPDATE (zmienna1))
    SET zmienna1= 'cos1'
ELSEIF
    SET zmienna1= 'cos2'
END IF;
BEGIN
UPDATE x.typ1
SET @zmienna1 = 'jakaswartosc'
WHERE x INNER JOIN y
    ON x.typ1 = y.typ1
    WHERE (x.typ1 = 'wartosc'and x.typ2 = 'wartosc' 
END;
GO

THX

2 个答案:

答案 0 :(得分:0)

您的代码看起来像SQL Server。在您的情况下,使用连接进行更新的正确语法如下:

UPDATE x
    SET zmienna1 = (CASE WHEN UPDATE(zmienna1) THEN 'jakaswartosc' ELSE zmienna1 END),
        zmienna2 = (CASE WHEN UPDATE(zmienna1) THEN zmienna2 ELSE 'jakaswartosc' END)
    FROM x INNER JOIN
         y
         ON x.typ1 = y.typ1
    WHERE x.typ1 = 'wartosc' and x.typ2 = 'wartosc' ;

请注意,您不能使用变量指定目标列(或任何其他列) - 因此使用case语句。

或者,您可以有两个更新语句:

IF (UPDATE (zmienna1))
    UPDATE x
        SET cos1 = 'jakaswartosc'
        FROM x INNER JOIN
             y
             ON x.typ1 = y.typ1
        WHERE x.typ1 = 'wartosc' and x.typ2 = 'wartosc' ;
ELSE 
    UPDATE x
        SET cos2 = 'jakaswartosc'
        FROM x INNER JOIN
             y
             ON x.typ1 = y.typ1
        WHERE x.typ1 = 'wartosc' and x.typ2 = 'wartosc' ;

答案 1 :(得分:0)

所以它看起来像这样, 但如果我想添加更多变量 比如宣布@ zmienna2,@ zmienna3 并将其设置为specyfic值 我可以这样:

USE x;
GO
IF EXISTS (SELECT typ1
    FROM db.y
    WHERE typ1 ='wartosc' and typ2 = 'wartosc')

DROP TRIGGER triggername;
GO
CREATE TRIGGER triggername
ON [db.sampledbname][y.typ]
AFTER UPDATE
AS
declare @zmienna1 varchar(20)   
declare @zmienna2 varchar(20)
declare @zmienna3 varchar(20)
IF (UPDATE (zmienna1))
    UPDATE x
        SET cos1 = 'valuex'
        FROM x INNER JOIN y
             ON x.type1 = y.type1
        WHERE x.type1 = 'something1' and x.type2 = 'something2' ;
ELSE 
    UPDATE x
        SET cos2 = 'valuey'
        FROM x INNER JOIN y
             ON x.type1 = y.type1
        WHERE x.type1 = 'something1' and x.type2 = 'something2' ;
END IF;
BEGIN
SET @zmienna2 = 'value1'
SET @zmienna3 = 'value2'
END;
GO