Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-08-10
4
Medium Priority
?
77 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 70

Accepted Solution

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

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

971 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