触发不按预期工作

时间:2017-10-02 18:31:04

标签: sql-server

我希望在更改Document时更改Valid表格中的修改日期。

但是,即使只更改其中一个文件的Valid列,具有相同ID的所有文档的修改日期也会发生变化。

因此,当我更改Valid的值时,它会更改ApplicationID=4所有记录的修改日期:

CREATE TRIGGER [Schema].[TGR_Document_ModifiedDate]
ON [Schema].[Document]
AFTER INSERT, UPDATE 
AS
    UPDATE [Schema].[Document]
    SET [ModifiedDate] = SYSDATETIME()
    FROM [Schema].[Document] 
    JOIN inserted i ON [Schema].[Document].[ApplicationID] = i.[ApplicationID] 
                    AND [Schema].[Document].[URI] = i.[URI] 
                    -- AND [AFI].[Document].[Valid] != i.[Valid]

在我的代码中,我只是更改了Valid列的值,而我根本没有改变ModifiedDate

1 个答案:

答案 0 :(得分:1)

你的桌子上有主键吗?如果是,则根据主键加入插入的表,而不是ApplicationID,因为它不是唯一的。我还建议你重写你的触发器

CREATE TRIGGER [Schema].[TGR_Document_ModifiedDate]
ON [Schema].[Document]
AFTER INSERT, UPDATE AS
IF (Update(Valid))
    UPDATE [Schema].[Document]
    SET [ModifiedDate] = SYSDATETIME()
    FROM [Schema].[Document] JOIN inserted i
    ON [Schema].[Document].[PrimaryKeyColumn] = i.[PrimaryKeyColumn] AND
        [Schema].[Document].[URI] = i.[URI] AND

因此,此触发器将检查您是否正在修改Valid列,它将更新Modified Date列。您可以阅读更多here

我试过这个

CREATE TABLE dbo.Document (
ApplicationID INT NOT NULL, 
URI VARCHAR(50) NOT NULL,   
Valid INT, 
CreatedDate DATETIME, 
ModifiedDate DATETIME
)
GO

INSERT INTO dbo.Document
(
    ApplicationID,
    URI,
    Valid,
    CreatedDate,
    ModifiedDate
)
VALUES
(   4,         -- ApplicationID - int
    'someuri',        -- URI - varchar(50)
    0,         -- Valid - int
    '2010-01-01', -- CreatedDate - datetime
    '2010-01-01'  -- ModifiedDate - datetime
)
,
(   4,         -- ApplicationID - int
    'somedifferenturi',        -- URI - varchar(50)
    1,         -- Valid - int
    '2010-01-02', -- CreatedDate - datetime
    '2010-01-02'  -- ModifiedDate - datetime
)
,
(   4,         -- ApplicationID - int
    'xyzuri',        -- URI - varchar(50)
    1,         -- Valid - int
    '2010-01-03', -- CreatedDate - datetime
    '2010-01-03'  -- ModifiedDate - datetime
)
,
(   5,         -- ApplicationID - int
    'uri',        -- URI - varchar(50)
    1,         -- Valid - int
    '2010-01-04', -- CreatedDate - datetime
    '2010-01-04'  -- ModifiedDate - datetime
)
GO
ALTER TABLE dbo.Document ADD CONSTRAINT PK_Document PRIMARY KEY(ApplicationID, URI)
GO

CREATE TRIGGER dbo.[TGR_Document_ModifiedDate]
ON dbo.[Document]
AFTER INSERT, UPDATE AS
IF (Update(Valid))
    UPDATE dbo.[Document]
    SET [ModifiedDate] = SYSDATETIME()
    FROM dbo.[Document] JOIN inserted i
    ON dbo.[Document].ApplicationID = i.ApplicationID AND
        dbo.[Document].[URI] = i.[URI] 
GO
UPDATE dbo.[Document] SET [Valid] = 1 WHERE ApplicationID =4 and Valid =0
GO
SELECT *
FROM dbo.Document

选择的结果如下。

ApplicationID URI                                                Valid       CreatedDate             ModifiedDate
------------- -------------------------------------------------- ----------- ----------------------- -----------------------
4             somedifferenturi                                   1           2010-01-02 00:00:00.000 2010-01-02 00:00:00.000
4             someuri                                            1           2010-01-01 00:00:00.000 2017-10-02 23:37:09.443
4             xyzuri                                             1           2010-01-03 00:00:00.000 2010-01-03 00:00:00.000
5             uri                                                1           2010-01-04 00:00:00.000 2010-01-04 00:00:00.000

因此只更新了一行。