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
426 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

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

815 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

9 Experts available now in Live!

Get 1:1 Help Now