Solved

Update records in Table1 from Table2 if records exist or Insert new records from TAble1 into Table2 - SQL Server 2005

Posted on 2008-10-09
3
432 Views
Last Modified: 2012-08-13
Hello Experts,

What is the correct syntax to Update existing records or insert new records into one table from another table?

For Example:I have two tables that are identical (Same exact columns, column definitions and primary keys) -  Call them Table1 and Table2. I want to fill Table2 with records from Table1 periodically. Table2 will hold all of the records and Table1 acts as a temporary table that only has the lates records .

I want to update the existing records in Table2 with the same "changed" records in table1. Also I would like to insert any new records (not already in Table2) into Table2 from Table1.

If records exist in Table2
Update these records in  Table2 from Table1
if records do not exist in Table2
Insert new records from Table1 into Table2

What is the correct syntax to do this?

Thanks!
 
0
Comment
Question by:Saxitalis
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 22683821
UPDATE Table2
SET col1 = t1.col1, col2 = t1.col2, col3 = t1.col3
FROM Table1 t1 INNER JOIN
    Table2 t2 ON t1.ID = t2.ID

INSERT INTO Table2 (ID, col1, col2, col3)
SELECT t1.ID, t1.col1, t1.col2, t1.col3
FROM Table1 t1 LEFT JOIN
    Table2 t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22683836

--If records exist in Table2
--Update these records in  Table2 from Table1
UPDATE t2
SET t2.columnname = t1.columnname
, t2.anothercolumn = t1.anothercolumn
FROM Table2 t2 INNER JOIN Table1 t1
ON t2.PKey = t1.PKey -- primary key matchup
GO
 
--if records do not exist in Table2
--Insert new records from Table1 into Table2
INSERT INTO Table2(columnname, anothercolumn, othercolumn)
SELECT columnname, anothercolumn, othercolumn
FROM Table1 WHERE PKey NOT IN (SELECT PKey FROM Table2)

Open in new window

0
 

Author Closing Comment

by:Saxitalis
ID: 31504897
perfect - Thank you sir!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

828 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