Solved

SQL - Insert and Update Trigger

Posted on 2014-03-20
2
317 Views
Last Modified: 2014-04-04
I need to write a trigger that will copy one field to another when there's an insert or update.  Since this insert/update is being done by the application, which I have no control over.  Is it possible to do an after trigger for just an update?

Example, there's 2 date field (Delivery_Date and Due_Date).  If Delivery_date is populated, then the trigger will also populate Due_Date.  However, if it's an update and Due_date is already populated, then do not update

Here's what I'm thinking of, but does not work.

Create Trigger dbo.Update_date
On dbo.Purchase_line
After Update
Update Purchase_line
Set   Delivery_date  = Due_Date
From Purchase_line PL
Where PL.Order_ID in (Select I.Order_Id from Inserted I Where I.Order_ID = PL.Order_ID  
                                        I.Line_no = PL.Line_no)
           and PL.Due_Date Is Null

The above, I want it to execute after the Insert/Update is done then fire the above to do an update if "Due_Date" is Null only.
0
Comment
Question by:holemania
2 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
Create Trigger dbo.Update_date
On dbo.Purchase_line
After /*Insert*,/ Update --uncomment if you also want to default Due_Date on an Insert
As
Set Nocount On
Update Purchase_line
Set   Delivery_date  = Due_Date
From Purchase_line PL
Inner Join inserted I
    On I.Order_ID = PL.Order_ID AND I.Line_no = PL.Line_no
Where
PL.Due_Date Is Null
Go
0
 

Author Closing Comment

by:holemania
Comment Utility
Thanks that worked nicely.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

10 Experts available now in Live!

Get 1:1 Help Now