Solved

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

Posted on 2016-08-10
4
24 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:
ScottPletcher 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:ScottPletcher
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

758 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

14 Experts available now in Live!

Get 1:1 Help Now