繁体   English   中英

SQL触发器

[英]SQL Trigger

如果更新时tbl_repair_visit.TENANTSATISFACTION = 'Poor' ,我将运行附加的触发器。

如果我们更改工程师名称,我遇到的问题是“工程师”列得到更新,如果TENANTSATISFACTION = 'Poor' ,触发器将再次运行

我如何将其设置为仅在TENANTSATISFACTION = 'Poor'列已更新并且所有其他列TENANTSATISFACTION = 'Poor'更新的情况下运行

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(TENANTSATISFACTIONACTION)开始....结束

我认为您无法指定。 每当在表上执行UPDATE,DELETE或INSERT(分别为)时,都会触发触发器UPDATE,DELETE和INSERT。

您可以按照Mike K.的建议进行操作,并检查您感兴趣的列是否更改。

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

或者,也许您可​​以创建INSTEAD OF触发器,而不是AFTER触发器。 偏离路线,然后在INSTEAD OF触发器中,还必须编写UPDATE(或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 Programmer
(
id int not null identity(1,1),
name varchar(100) not null,
status varchar(20)
);


insert into Programmer(name,status) values('Pampers', 'Rich');




create trigger TrigUpdOnProgrammer on Programmer
for update
as

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

        -- put the fields to detect here...
        and inserted.status = deleted.status -- detect if changed
        -- ...detections
        where inserted.status is null) 

       -- if changes detected on status, then check if it is Poor.  
       -- don't worry, there's no performance degradation.  SQL AND is short-circuited
       and exists(select * from inserted where status = 'Poor')
    begin    
       print 'status changed';
    end
    else begin
        print 'changes ignored';
    end;

go

-- execute the following on succession, then check the output

update Programmer set status = 'Poor' where id = 1; -- changes detected

update Programmer set status = 'Poor' where id = 1; -- changes ignored

update Programmer set status = 'Rich' where id = 1; -- changes ignored

update Programmer set status = 'Poor' where id = 1; -- changes detected

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM