Solved

SQL - Insert and Update Trigger

Posted on 2014-03-20
2
329 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 69

Accepted Solution

by:
Scott Pletcher earned 500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

622 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