删除后的T-SQL触发器

时间:2014-09-23 18:17:59

标签: sql sql-server tsql

需要在从表中删除行后运行触发器。 此触发器应删除另一个表中的行,其中该表上的字段的值与另一个表中已删除行的某列的值相匹配。

1 个答案:

答案 0 :(得分:1)

如果从删除中触发删除:

create trigger trg_mainTable
on dbo.mainTable
after delete
as
begin
    delete 
    from dbo.relatedTable
    where someColumn in 
    (
        select someColumn
        from deleted
    )
end
go

如果从更新中触发删除:

create trigger trg_mainTable
on dbo.mainTable
after update
as
begin
    if update(someColumn)
    begin
        delete 
        from dbo.relatedTable
        where someColumn in 
        (
            select someColumn
            from deleted
        )
    end
end
go