LastUpdatedDate and LastUpdatedUser

  • Hi,

    Have a table with LastUpdatedDate and LastUpdatedUser columns at the end.

    If row been updated these columns in that row must populated by Getdate() and current_user values.

    Users can update table directly from Access linked tables.

    Is the way to "stamp" (trigger) update of the LastUpdatedDate and LastUpdatedUser fields when row is updated .

    Can it be done without any changes on Access side.

    Thank you.

    Leon

  • Use timestamp in pre-trigger is a way.

  • Yep

     TRIGGER TR_U_TblName ON TblName

     FOR UPDATE

    IF @@ROWCOUNT = 0

     RETURN

    UPDATE TblName

    Set LastUpdatedDate = Getdate(), LastUpdatedUser = Current_user

    FROM Inserted I

    WHERE TblName.Pk = I.PK

    Now, there is a catch!  this works if nested triggers are disabled


    * Noel

  • Thank you very much.

    Going to try it tomorrow morning.

    Have a good night.

    Leon

  • one thing to add, if you will have multiple rows updated at once you will need to change this line:

    WHERE TblName.Pk = I.PK

    to WHERE TblName.Pk in (select PK from inserted)

    HTH

    ------------

    Ray Higdon MCSE, MCDBA, CCNA

  • In addition to the above posts I would also place a DEFAULT of GETDATE() on the LastUpdateDate and a DEFAULT of CURRENT_USER on the LastUpdateUser columns.

    Another thing is, you write that users can update the table directly via Access. Do you mean they open the table and hack in the grid? If so, it might be better to create a view and exclude those 'internals columns' from the view.

     

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • I don't think that is correct

    You can check that the Trigger perform on ALL AFFECTED ROWS with

    tblName.pk = I.pk

    (That is Joining the Inserted Table with the Destination so the "In" is not necessary!)

     

     


    * Noel

  • NoelD, You're right, read it too fast.

    ------------

    Ray Higdon MCSE, MCDBA, CCNA

  • Thank you.

    It trigger working fine.

     It works even nested triggers set enabled!?

    Thanks

     

  • I said Nested Triggers  I should have said Recursive Triggers which by default is disabled

    but even  if you enable it it still works because is acting on the same set of records until it reaches level 32 of nesting   and you don't want that right?


    * Noel

  • You bet.

    ".. until it reaches level 32 ..."

    It sounds like playing computer game: "SQL server advanture"

    It has 32 levels, each is more difficalt.

    ( Now I trying to pass level 22 - "performance and tuning".  Interested, but can not pass ))

     

    Thanks.

    Leon

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply