Solved

HOW TO MODIFIE THE LAST MODIFIED ENTRY WITH A TRIGGER ?

Posted on 2009-05-20
5
501 Views
Last Modified: 2012-06-21
Hi,
I need to change the value of a column of a table  when another column has been modified.
what is the good syntax ?

Thanks !
CREATE TRIGGER [MODIF_DEVIS_COMMENTAIRE_INTERNE] ON [dbo].[DEVIS] 
FOR UPDATE
AS
IF UPDATE(COMMENTAIRE_INTERNE)
BEGIN
	UPADTE DEVIS SET COMMENTAIRE_INTERNE SET DATE_MODIFICATION = GETDATE  WHERE /*  HOW CAN I RETRIEVE THE PK OF THE UPDATED ENTRY ???    */
END

Open in new window

0
Comment
Question by:bruno_boccara
  • 3
5 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 24434395
CREATE TRIGGER [MODIF_DEVIS_COMMENTAIRE_INTERNE] ON [dbo].[DEVIS]

FOR UPDATE

AS

IF UPDATE(COMMENTAIRE_INTERNE)

BEGIN
    UPDATE  d
    SET          COMMENTAIRE_INTERNE = <The value you want to use goes here>,
          DATE_MODIFICATION = GETDATE()
    From    DEVIS d
          Inner Join Inserted i On d.<YourPrimaryKeyGoesHere> = i.<YourPrimaryKeyGoesHere>

END
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24434400
you can find the primary key from inserted or deleted tables; to be frank, i dont really prefer TRIGGERS here, you can probably writr the code to perform the update in the same procedure where you do ur update on the other columsn
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24434403
Let's try that again:

CREATE TRIGGER [MODIF_DEVIS_COMMENTAIRE_INTERNE] ON [dbo].[DEVIS]

FOR UPDATE

AS

IF UPDATED(COMMENTAIRE_INTERNE)

BEGIN
    UPDATE  d
    SET          COMMENTAIRE_INTERNE = <The value you want to use goes here>,
          DATE_MODIFICATION = GETDATE()
    From    DEVIS d
          Inner Join Inserted i On d.<YourPrimaryKeyGoesHere> = i.<YourPrimaryKeyGoesHere>

END
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24434437
It was right the first time. :(

Sorry for the multiple posts.
0
 

Author Closing Comment

by:bruno_boccara
ID: 31583620
Perfect !!!!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question