如果字段实际上已更改,则更改其列之一的值

时间:2015-01-28 09:15:26

标签: sql sql-server tsql database-trigger

我有"要求"的本质。如果用户更改了字段"描述"在这种情况下,它应该自动改变字段的值"稳定性" (下拉列表)。也就是说,如果您必须更改要求的描述,它就会变得不稳定。我写了一个触发器

CREATE TRIGGER [ChangeStabilityRequirement]
ON [dbo].[Requirement] AFTER UPDATE
AS
BEGIN
    if update([Definition])
    begin
        update [Requirement] set Stability = 2 
        where RequirementId in (select RequirementId from inserted)
    end
end 

但问题是当表中的任何更改条目时触发器被激活。需要仅响应列中的更改"定义"。

我的表

CREATE TABLE [dbo].[Requirement] (
    [RequirementId]                   INT            IDENTITY (1, 1) NOT NULL,
    [Rationale]                       NVARCHAR (MAX) NULL,
    [CreatedOn]                       DATE           CONSTRAINT [DF__Requireme__Creat__473C8FC7] DEFAULT (getdate()) NULL,
    [CurentVersion]                   NVARCHAR (MAX) NULL,
    [State]                           INT            NOT NULL,
    [Priority]                        INT            NOT NULL,
    [Type]                            INT            NOT NULL,
    [Source_BusinessRuleId]           INT            NULL,
    [Stability]                       INT            NOT NULL,
    [UserPart]                        NVARCHAR (MAX) NULL,
    [CreatedBy_UserId]                INT            NULL,
    [Responsible_UserId]              INT            NULL,
    [ImplementationVersion]           NVARCHAR (MAX) NULL,
    [ResponsibleId]                   INT            DEFAULT ((0)) NULL,
    [SourceId]                        INT            DEFAULT ((0)) NULL,
    [InterfacePoint_InterfacePointId] INT            NULL,
    [InterfacePointId]                INT            DEFAULT ((0)) NULL,
    [CreatedById]                     INT            DEFAULT ((0)) NULL,
    [Definition]                      NVARCHAR (MAX) DEFAULT ('') NOT NULL,
    CONSTRAINT [PK_dbo.Requirement] PRIMARY KEY CLUSTERED ([RequirementId] ASC),
    CONSTRAINT [FK_dbo.Requirement_dbo.User_CreatedById] FOREIGN KEY ([CreatedById]) REFERENCES [dbo].[User] ([UserId]),
    CONSTRAINT [FK_dbo.Requirement_dbo.User_ResponsibleId] FOREIGN KEY ([ResponsibleId]) REFERENCES [dbo].[User] ([UserId]),
    CONSTRAINT [FK_dbo.Requirement_dbo.BusinessRule_SourceId] FOREIGN KEY ([SourceId]) REFERENCES [dbo].[BusinessRule] ([BusinessRuleId])
);

更新表格 更新发生在Web应用程序界面中

这是来自SQL SERVER配置文件的查询

exec [dbo].[Requirement_Update] 
@RequirementId=32,
@Definition=N'Реализовать возможность выбора значения "Без пени" в поле "Тип начисления пени". Диалоговое окно АР-12',
@Rationale=N'В соответствии с изменением бизнес-правила',
@CreatedOn='2014-12-18 00:00:00',
@CurentVersion=N'2.1',
@ImplementationVersion=N'2.2',
@State=0,
@Priority=1,
@Stability=1,
@Type=0

1 个答案:

答案 0 :(得分:0)

好的,如果只在更新定义时需要更新要求,请将触发器更改为:

    CREATE TRIGGER [ChangeStabilityRequirement]
    ON [dbo].[Requirement] AFTER UPDATE
    AS
    BEGIN
        if update([Definition]) AND NOT update([Rationale]) AND NOT update([CurentVersion]) 
         --AND NOT UPDATE(.....list here all possible columns that may be updated 
        begin
            update [Requirement] set Stability = 2 
            where RequirementId in (select RequirementId from inserted)
        end
    end