Best practice to auto-update a datetime field?

Last post 07-19-2005 1:04 PM by jfmenard. 23 replies.
Page 1 of 2 (24 items) 1 2 Next >
Active Topics My Discussions Unanswered Sort Posts: Previous Next
  • 07-07-2005 7:46 AM

    Best practice to auto-update a datetime field?

    I use an UpdateDT datetime field on all tables I want to track the last modification date. This field is updated by a trigger in my Sql 2000 database:

    CREATE TRIGGER Trig_MyTable_Update ON dbo.MyTable FOR INSERT, UPDATE
    AS
    IF @@ROWCOUNT=0 RETURN
    UPDATE U SET UpdateDT = GETDATE()
    FROM MyTable U INNER JOIN INSERTED I ON U.Id = I.Id

    This looked to me as a simple way of accomplishing what I needed. However this trigger is source of deadlocks. I made a simple .NET application that start 2 threads that each repetedly update the same row in MyTable. It takes a few seconds and a deadlock appears.

    My understanding is:
    - Thread-1: Call Update on a row.
    - Thread-2: Call Update on same row, but waits that Thread-1 finishes.
    - Thread-1: Trigger gets called to update same row, waits that Thread-2 finishes.
    - Sql Server detects deadlock and terminate one of the queries (let say Thread-2).
    - Thread-2: Aborts and raise an error.
    - Thread-1: Completes trigger Update statement.
    - Thread-1: Commits main Update and terminates.

    I successfully solved this issue by using a pair of INSTEAD OF triggers like this (only UPDATE shown below):

    CREATE TRIGGER Trig_MyTable_Update ON dbo.MyTable INSTEAD OF UPDATE
    AS
    IF @@ROWCOUNT=0 RETURN
    UPDATE U SET
    U.Field1 = I.Field1,
    U.Field2 = I.Field2,
    U.Field3 = I.Field3,
    U.Field4 = I.Field4,
    U.Field5 = I.Field5,
    U.Field6 = I.Field6,
    U.Field7 = I.Field7,
    U.Field8 = I.Field8,
    U.Field9 = I.Field9,
    U.UpdateDT = GETDATE()
    FROM MyTable U INNER JOIN INSERTED I ON U.Id = I.Id

    But this becomes just too complicated for a simple timestamping thing:
    - Other (AFTER) triggers will think that all columns are updated when querying the UPDATED(field) function.
    - If we change the table structure, we must not forget to update the INSTEAD OF trigger which complicates maintenance.

    Is there something I missed?

    Thanks,

    J-F
  • 07-07-2005 7:51 AM In reply to

    Re: Best practice to auto-update a datetime field?

    How about using default (getDate()) instead? I guess all you want is to set date of last change.
  • 07-07-2005 7:56 AM In reply to

    Re: Best practice to auto-update a datetime field?

    quote:
    Originally posted by mmarovic

    How about using default (getDate()) instead? I guess all you want is to set date of last change.



    Precisely, I want the date of "last change" not "first insert".
  • 07-07-2005 8:01 AM In reply to

    Re: Best practice to auto-update a datetime field?

    Then, I don't think there is a option other than the Update Trigger,
    or else you have to handled this by the front end script code
    Contributing Editor, Writer & Forums Moderator http://www.SQL-Server-Performance.Com

    Visit my Blog at http://www.dbfriend.net/


    View Dinesh Asanka's profile on LinkedIn
  • 07-07-2005 8:12 AM In reply to

    Re: Best practice to auto-update a datetime field?

    quote:
    Originally posted by dineshasanka

    Then, I don't think there is a option other than the Update Trigger,
    or else you have to handled this by the front end script code


    No, we want to keep track of rows that where altered using SQL tools like Enterprise Manager.

    I guess the main question is:

    > Is it normal that an AFTER trigger should never update its source table?

    If this is true, then I know a lot of DBA that do this mistake on a daily basis, like I did...
  • 07-07-2005 8:59 AM In reply to

    Re: Best practice to auto-update a datetime field?

    I see. I prefer performing all modifications using stored procedures. In that case I would just add updateDT = getDate() in update procedures. Performs much better then trigger. If it is not the option then you have to use the trigger afaik.
  • 07-07-2005 2:25 PM In reply to

    Re: Best practice to auto-update a datetime field?

    The trigger script that you posted is OK in itself - is it the complete trigger, or could it be there's nothing else in the trigger causing the deadlock?
  • 07-07-2005 4:45 PM In reply to

    Re: Best practice to auto-update a datetime field?

    quote:
    Originally posted by Adriaan

    The trigger script that you posted is OK in itself - is it the complete trigger, or could it be there's nothing else in the trigger causing the deadlock?



    Finally someone who understand my concerns! Yes this simple trigger:
    CREATE TRIGGER Trig_MyTable_Update ON dbo.MyTable FOR INSERT, UPDATE
    AS
    IF @@ROWCOUNT=0 RETURN
    UPDATE U SET UpdateDT = GETDATE()
    FROM MyTable U INNER JOIN INSERTED I ON U.Id = I.Id

    is the source of all my deadlocks! I couldn't beleive it when it occured so I made a simple application with many threads that did:
    UPDATE MyTable SET Field1 = 123 WHERE Id=1

    many times in parallel and the deadlock appears after a few seconds!

    Can someone have a technical explanation for this?

    Thanks,
  • 07-08-2005 2:59 AM In reply to

    Re: Best practice to auto-update a datetime field?

    As Adriaan already asked, is that the only trigger on that table? I hope so, otherwise I would have to admit that I don't understand.
  • 07-08-2005 8:15 AM In reply to

    Re: Best practice to auto-update a datetime field?

    quote:
    Originally posted by mmarovic

    As Adriaan already asked, is that the only trigger on that table? I hope so, otherwise I would have to admit that I don't understand.



    Yes. It is the ONLY trigger on that table.
  • 07-08-2005 8:21 AM In reply to

    Re: Best practice to auto-update a datetime field?

    Strange. Consider this just a WAG, but what happens when you change

    IF @@ROWCOUNT=0 RETURN
    ...

    to

    IF @@ROWCOUNT>0
    ...


    --
    Frank Kalis
    Microsoft SQL Server MVP
    http://www.insidesql.de
    Ich unterstütze PASS Deutschland e.V. http://www.sqlpass.de)
    --
    Frank Kalis
    Microsoft SQL Server MVP
    Contributing Editor, Writer & Forum Moderator http://www.sql-server-performance.com
    Webmaster: http://www.insidesql.org
    View Frank Kalis's profile on LinkedIn
  • 07-08-2005 9:27 AM In reply to

    Re: Best practice to auto-update a datetime field?

    quote:
    Originally posted by FrankKalis

    Strange. Consider this just a WAG, but what happens when you change
    IF @@ROWCOUNT=0 RETURN
    to
    IF @@ROWCOUNT>0



    Same thing:

    SQL : UPDATE MyTable SET Field1=@Field1 WHERE (Id=@Value1)
    (Transaction (Process ID 56) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.)
  • 07-09-2005 2:27 PM In reply to

    Re: Best practice to auto-update a datetime field?

    quote:
    Originally posted by jfmenard

    quote:
    Originally posted by FrankKalis

    Strange. Consider this just a WAG, but what happens when you change
    IF @@ROWCOUNT=0 RETURN
    to
    IF @@ROWCOUNT>0



    Same thing:

    SQL : UPDATE MyTable SET Field1=@Field1 WHERE (Id=@Value1)
    (Transaction (Process ID 56) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.)



    [xx(]
    But this is interesting. If you want me to, I'll ask this in the private MVP newsgroups. Contact me via PM here in the forum. [:)]

    --
    Frank Kalis
    Microsoft SQL Server MVP
    http://www.insidesql.de
    Ich unterstütze PASS Deutschland e.V. http://www.sqlpass.de)
    --
    Frank Kalis
    Microsoft SQL Server MVP
    Contributing Editor, Writer & Forum Moderator http://www.sql-server-performance.com
    Webmaster: http://www.insidesql.org
    View Frank Kalis's profile on LinkedIn
  • 07-11-2005 4:12 AM In reply to

    Re: Best practice to auto-update a datetime field?

    Btw, what is the output of trace flag 1204 as described in BOL under "troubleshooting deadlocks"?

    --
    Frank Kalis
    Microsoft SQL Server MVP
    http://www.insidesql.de
    Ich unterstütze PASS Deutschland e.V. http://www.sqlpass.de)
    --
    Frank Kalis
    Microsoft SQL Server MVP
    Contributing Editor, Writer & Forum Moderator http://www.sql-server-performance.com
    Webmaster: http://www.insidesql.org
    View Frank Kalis's profile on LinkedIn
  • 07-11-2005 8:04 AM In reply to

    Re: Best practice to auto-update a datetime field?

    quote:
    Originally posted by FrankKalis
    [xx(]
    But this is interesting. If you want me to, I'll ask this in the private MVP newsgroups. Contact me via PM here in the forum. [:)]


    I have not found any link to send you a private email, so here is my email address:
    jean-francois.menard [at-sign] traf-park.com

    Thanks,
Page 1 of 2 (24 items) 1 2 Next >
Active Topics   My Discussions    Unanswered Posts


© 2010 Jude O'Kelly All Rights Reserved.