Solved

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

Posted on 2016-08-10
4
44 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
[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
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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