Solved

HOW TO MODIFIE THE LAST MODIFIED ENTRY WITH A TRIGGER ?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…

895 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

11 Experts available now in Live!

Get 1:1 Help Now