简体   繁体   English

仅当列值更改时,T-SQL触发器

[英]T-SQL Trigger only when column value changes

I'm using SQL Server 2008 and have the following trigger: 我正在使用SQL Server 2008,并且具有以下触发器:

USE [gatordata]
GO
/****** Object:  Trigger [dbo].[TriggerUserAssigned] Script Date:2/20/2016 3:42:05 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[TriggerUserAssigned]
ON [dbo].[User]
AFTER UPDATE
AS
IF UPDATE([AssignedToUser]) 
BEGIN
declare @Id int;
declare @AssignedToUser nvarchar(50);
declare @TimeStamp datetime;
declare @Hour bit;
declare @RushService bit;
declare @Status nvarchar(50);
select @Id=i.Id from inserted i;
select @AssignedToUser=i.AssignedToUser from inserted i;
select @Hour=i.[Hour] from inserted i;
select @RushService=i.[RushService] from inserted i;
select @Status=i.[Status] from inserted i;

INSERT INTO [Messages]  
(RequestID,AssignedToUser,[TimeStamp],[Hour],RushService,[Status],MessageText) VALUES (@Id,@AssignedToUser,GETDATE(),@Hour,@RushService,@Status,'New Critical Request')
END

This is working fine except I need to modify it so it only triggers the insert to the "Messages" table when the column "AssignedToUser" value actually changes to a new value. 除我需要修改它外,此方法工作正常,因此只有在“ AssignedToUser”列的值实际更改为新值时,它才触发对“ Messages”表的插入。 If the value of "AssignedToUser" remains the same when the row is updated the trigger should not fire. 如果在更新行时“ AssignedToUser”的值保持不变,则不应触发该触发器。 Thanks in advance SO friends... 在此先感谢各位朋友...

Assuming that you have a primary key on Users , this is not difficult. 假设您在Users上具有主键,这并不困难。 Let me assume that primary key is RequestId . 让我假设主键是RequestId

Then: 然后:

insert into messages( . . . )
    select i.id, i.assignedtouser, i.hour, i.rushservice, i.status
    from inserted i join
         deleted d
         on i.requestid = d.requestid
    where i.assignedtouser <> d.assignedtouser;

Note: You should never assume that inserted and deleted have only one row, as your original code does. 注意: 永远不要像原始代码那样假定inserteddeleted行只有一行。 This will simply fail when someone attempts to update multiple rows. 当有人尝试update多行时,这只会失败。

You should also explicitly include the column list in an insert statement. 您还应该在insert语句中显式包括列列表。 So the . . . 所以. . . . . . is for the list of columns in the messages table. 用于messages表中的列列表。

I think this should do it : 我认为应该这样做:

USE [gatordata]
GO
/****** Object:  Trigger [dbo].[TriggerUserAssigned] Script Date:2/20/2016 3:42:05 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[TriggerUserAssigned]
ON [dbo].[User]
AFTER UPDATE
AS
    INSERT  [Messages](
            [RequestID],        [AssignedToUser],   [TimeStamp],            [Hour],
            [RushService],      [Status],           [MessageText]) 
    SELECT 
            i.[Id],             i.[AssignedToUser], GETDATE(),              i.[Hour],
            i.[RushService],    i.[Status],         'New Critical Request'
    FROM    [inserted]  i
    JOIN    [deleted]   d       ON d.[Id] = i.[Id] 
    WHERE   d.[AssignedToUser] <> i.[AssignedToUser];
GO

By joining the inserted table to the deleted table, you should be able to compare the values in that column to determine whether it has changed. 通过将插入的表连接到已删除的表,您应该能够比较该列中的值以确定它是否已更改。 The added benefit is you can select straight from the inserted table instead of having to assign everything to variables. 附加的好处是您可以直接从插入的表中进行选择,而不必将所有内容分配给变量。

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

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