SQL触发器

SQL触发器,sql,triggers,Sql,Triggers,如果更新时tbl\u repair\u visit.tenantsatisation='Poor',则会运行附加的触发器 我遇到的问题是,如果我们更改工程师名称,工程师列将得到更新,如果tenantsatisation='Poor' 如果更新了tenantsatisation='Poor'列,并且所有其他列上的ignor都更新了,我如何将此设置为仅运行 ALTER TRIGGER [dbo].[tr_CustomerSatisfactionAlertRepair] ON [dbo].[

如果更新时
tbl\u repair\u visit.tenantsatisation='Poor'
,则会运行附加的触发器

我遇到的问题是,如果我们更改工程师名称,工程师列将得到更新,如果
tenantsatisation='Poor'

如果更新了
tenantsatisation='Poor'
列,并且所有其他列上的ignor都更新了,我如何将此设置为仅运行

ALTER TRIGGER [dbo].[tr_CustomerSatisfactionAlertRepair] 
    ON [dbo].[tbl_repair_visit] 
AFTER UPDATE
AS 
BEGIN
    SET NOCOUNT ON;
    INSERT alertmessagedata (TypeID, Contract, Address, ORDERID, 
                ENGINEERS, Sent, DateAdded)
    SELECT '5', tbl_property.contract, tbl_property.fulladdress, 
            tbl_repair_visit.orderid, tbl_repair_visit.engineer, 
            0, GETDATE()
    FROM TBL_REPAIR_VISIT 
    INNER JOIN
        INSERTED X ON TBL_REPAIR_VISIT.VISITID = X.VISITID 
    INNER JOIN 
        TBL_PROPERTY ON TBL_REPAIR_VISIT.PROPREF = TBL_PROPERTY.PROPREF
    WHERE tbl_repair_visit.TENANTSATISFACTION = 'Poor'
END

在更新触发器中,可以检查列是否正在更新:

如果更新(租户满意度) 开始 ....
结束

在更新触发器中,您可以检查列是否正在更新:

如果更新(租户满意度) 开始 ....
结束

我想你不能具体说明。每当对表执行UPDATE、DELETE或INSERT(体面地)时,就会触发UPDATE、DELETE和INSERT触发器


你可以按照Mike K.的建议去做,检查你感兴趣的专栏是否改变了

我认为你不能具体说明这一点。每当对表执行UPDATE、DELETE或INSERT(体面地)时,就会触发UPDATE、DELETE和INSERT触发器


你可以按照Mike K.的建议去做,检查你感兴趣的专栏是否改变了

您不能使用嵌套触发器选项吗?
(将该选项设置为off,以便触发器不会导致触发其他触发器)

或者,您可以创建一个替代触发器,而不是后触发器。
当然,在INSTEAD OF触发器中,您还必须在附加逻辑旁边编写UPDATE(或insert)语句,该语句应该执行实际的UPDATE(或insert)或insert(插入)到表中。

您不能使用嵌套触发器选项吗?
(将该选项设置为off,以便触发器不会导致触发其他触发器)

或者,您可以创建一个替代触发器,而不是后触发器。
当然,在INSTEAD OF触发器中,您还必须在附加逻辑旁边编写UPDATE(或insert)语句,该语句应该执行实际的更新或插入到表中。

如果连接的表(即insert)的值为空,则从旧版本左连接记录的新版本,这意味着要检测更改的字段已更改

create table family
(
id int not null identity(1,1),
name varchar(100) not null,
age int not null
);


insert into family(name,age) values('Michael', 32);
insert into family(name,age) values('Matthew', 23);



create trigger TrigUpdOnFamily on family
for update
as

    if exists
        (
        select * from deleted 
        left join inserted on inserted.id = deleted.id    

        -- put the fields to detect here...
        and inserted.age = deleted.age
            -- ...detections
        where inserted.age is null) begin    

        -- detect important fields
        print 'age change';

    end
    else begin
        -- ignore non-important fields
        print 'nothing change';
    end;

go


-- possible SqlCommand from .NET
update family set name = 'Michael', age = 20 where id = 1;    

update family set name = 'Mateo', age = 23 where id = 2;

左键从旧版本连接记录的新版本,如果连接的表(即插入的)具有空值,则表示要检测更改的字段已更改

create table family
(
id int not null identity(1,1),
name varchar(100) not null,
age int not null
);


insert into family(name,age) values('Michael', 32);
insert into family(name,age) values('Matthew', 23);



create trigger TrigUpdOnFamily on family
for update
as

    if exists
        (
        select * from deleted 
        left join inserted on inserted.id = deleted.id    

        -- put the fields to detect here...
        and inserted.age = deleted.age
            -- ...detections
        where inserted.age is null) begin    

        -- detect important fields
        print 'age change';

    end
    else begin
        -- ignore non-important fields
        print 'nothing change';
    end;

go


-- possible SqlCommand from .NET
update family set name = 'Michael', age = 20 where id = 1;    

update family set name = 'Mateo', age = 23 where id = 2;

触发器中BL的不良后果的一个很好的例子。既然这可以更改为执行预期的操作,那么这是否更像是“当实现与设计不匹配时的不良后果”?我不明白为什么触发器在这种情况下是坏的,因为它可以正确编码…@MartGriff:如果更新(租户满意度)失败,如果之前的值很差,那么即使租户满意度没有真正改变,它仍然会标记为UPDATEd。如果更新将触发多个插入到alertmessagedata中。想象一下用户双击基于web的程序上的“提交”按钮。触发器存储在Microsoft SQL中的什么位置,因为我在“可编程数据库触发器”文件夹中找不到它们。触发器中BL的不良后果的一个很好的例子。由于这可以更改为执行预期的操作,这难道不是一个更严重的问题吗“当实现与设计不匹配时会产生不良后果”?我不明白为什么触发器在这种情况下是不好的,因为它可以正确编码…@MartGriff:IF-UPDATE(租户满意度)如果前一个值很差,则将失败,即使租户满意度没有真正更改,它仍将标记为已更新。如果更新,则将触发多个插入到alertmessagedata中。设想用户双击基于web的程序上的“提交”按钮。触发器存储在Microsoft SQL中的何处,因为我在文件夹中找不到它们可编程数据库触发器。