开发者

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 is supposed to update the password date: unconditionally if we deal with an insert, but password date should be changed only if an update has really changed the password.

This works in SQL Server 2000 but does not in SQL Server 2005. I am quite sure to have made something st开发者_如何学编程upid, but, just in case, someone is aware about some change between SQL Server 2000 and 2005 that could possibly affect the behaviour of this trigger? Namely, the update() function?


You've fallen into the classic error of coding your trigger to only handle single row updates.

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

assumes you'll only update one row at a time. Instead, try something like this.

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, '')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜