Trigger behavior in SQL Server 2000 and SQL Server 2005: any change?

I have the following code for a trigger:

    create trigger trPassDat

    ON men
    FOR INSERT, UPDATE

AS
    declare  @man int;

    select @man = I.man from men U join inserted I on U.man = I.man

    if Not exists (select 'True' from deleted where man = @man)
    BEGIN
        update men set passdate = getdate() where man = (select man from inserted)
        return
    END

-- UPDATE

    if     update(pwd) 
    BEGIN
        update men set passdate = getdate() where man = @man
    END
GO

which should update the password date: unconditionally, if we are dealing with the insertion, but the password date should be changed only if the update really changed the password.

This works in SQL Server 2000, but not in SQL Server 2005. I'm sure I did something stupid, but just in case, someone knows about some changes between SQL Server 2000 and 2005 that could affect the behavior of this trigger? Namely, the update () function?

+3
source share
1 answer

You are caught in the classic coding error of your trigger only for processing updates of one line.

select @man = I.man from men U join inserted I on U.man = I.man

, . - .

update m
    set passdate = getdate()
    from inserted i
        inner join men m
            on i.man = m.man
        left join deleted d
            on i.man = d.man
    where i.pwd <> isnull(d.pwd, '')
+4

Source: https://habr.com/ru/post/1764728/


All Articles