1

tbl_repair_visit.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
4

5 回答 5

5

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

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

于 2009-02-03T11:42:11.003 回答
0

我不认为你可以指定。每当对表执行 UPDATE、DELETE 或 INSERT(相应地)时,都会触发触发器 UPDATE、DELETE 和 INSERT。

您可以按照 Mike K. 的建议进行操作,并检查您感兴趣的列是否发生了变化。

于 2009-02-03T11:54:31.713 回答
0

你不能用嵌套触发器选项做点什么吗?
(关闭该选项,以便触发器不会导致其他触发器被触发)

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

于 2009-02-03T12:09:36.877 回答
0

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

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;
于 2009-02-03T12:11:00.243 回答
0
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
于 2009-02-03T13:03:25.630 回答