Solved

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

Posted on 2016-08-10
4
32 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

860 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