更新触发器语句

时间:2012-12-22 04:34:17

标签: sql-server-2005

如何在触发器中使用以下查询,当self_id列填充下一条记录时会自动触发????

UPDATE  tree_str
SET     L1_Left = ml.self_id,
    L1_Centre = mc.self_id,
    L1_Right = mr.self_id
FROM    tree_str t LEFT JOIN
    member ml   ON  t.self_id = ml.parent_id
                AND ml.position = 'LEFT' LEFT JOIN
    member mc   ON  t.self_id = mc.parent_id
                AND mc.position = 'CENTER' LEFT JOIN
    member mr   ON  t.self_id = mr.parent_id
                AND mr.position = 'RIGHT'

我使用下面的触发器,但它不起作用.......

CREATE TRIGGER autoupdate ON [dbo].[tree_str]
FOR UPDATE
AS BEGIN

UPDATE  tree_str
SET     L1_Left = ml.self_id,
    L1_Centre = mc.self_id,
    L1_Right = mr.self_id
FROM    tree_str t LEFT JOIN
    member ml   ON  t.self_id = ml.parent_id
                AND ml.position = 'LEFT' LEFT JOIN
    member mc   ON  t.self_id = mc.parent_id
                AND mc.position = 'CENTER' LEFT JOIN
    member mr   ON  t.self_id = mr.parent_id
                AND mr.position = 'RIGHT'
END                    

1 个答案:

答案 0 :(得分:0)

请尝试以下触发语法:

CREATE TRIGGER autoupdate ON dbo.tree_str
AFTER UPDATE
AS BEGIN
    UPDATE  tree_str
    SET L1_Left = ml.self_id, L1_Centre = mc.self_id,  L1_Right = mr.self_id
    FROM tree_str t LEFT JOIN
    member ml   ON  t.self_id = ml.parent_id AND ml.position = 'LEFT' 
    LEFT JOIN member mc   ON  t.self_id = mc.parent_id AND mc.position = 'CENTER' 
    LEFT JOIN member mr   ON  t.self_id = mr.parent_id
    AND mr.position = 'RIGHT'

END

一个明显的变化是'更新后'而不是'FOR UPDATE'。