Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL - Insert and Update Trigger

Posted on 2014-03-20
2
Medium Priority
?
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 39942757
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
ID: 39978196
Thanks that worked nicely.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

715 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