Solved

SQL Server Trigger to copy specific records from one table to another

Posted on 2016-08-10
4
31 Views
Last Modified: 2016-08-10
In short, when the “Customer” column is update in TABLE1, I need to write a trigger to copy *most* (about12 columns) of that record information from TABLE1 into TABLE2. However, the columns names are not all an exact match.

The trigger need to fire only on data update, so when going from data to a NULL value, the trigger will NOT fire.

Also, when inserting that record into TABLE2, I also need to insert that customer e-mail address which is found in TABLE3.eMail where the relation is TABLE1.Customer=TABLE3.Customer

Thank you all! Please be so kind to refrain from lecturing about the data design since this is a simplified version of a much more complex  design :-)
0
Comment
Question by:Cobra967
  • 2
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 41750720
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
CREATE TRIGGER Customer__TRG_UPD
ON dbo.Customer
AFTER UPDATE
AS
SET NOCOUNT ON;
INSERT INTO dbo.TABLE2 (
    col1, col2 /*, col3, ...*/,
    eMail
    )
SELECT
    i.col1, i.colb /*, i.col3, ...*/,
    t3.eMail
FROM inserted i
LEFT OUTER JOIN dbo.TABLE3 t3 ON t3.Customer = i.Customer
WHERE i.col1 IS NOT NULL OR i.colb IS NOT NULL /* OR ...*/
GO
0
 

Author Comment

by:Cobra967
ID: 41750785
Thank you Scott with "ON dbo.Customer" did you mean dbo.TABLE1?

Also, what is the intent of the clause :  WHERE i.col1 IS NOT NULL OR i.colb IS NOT NULL /* OR ...*/ ?

Thanks for your help!
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41750790
Yes.

Your original q said:

The trigger need to fire only on data update, so when going from data to a NULL value, the trigger will NOT fire

I wasn't sure what that meant, so I included the NULLs check in case the app sometimes sets columns back to NULL??
0
 

Author Closing Comment

by:Cobra967
ID: 41750966
I got it to work! Thank you for your help Scott.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

809 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