0

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

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 Profile 的查询

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
4

1 回答 1

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 
于 2015-01-28T12:44:26.203 回答