Solved

HOW TO MODIFIE THE LAST MODIFIED ENTRY WITH A TRIGGER ?

Posted on 2009-05-20
5
499 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now