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

x
?
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
Medium Priority
?
439 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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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 60

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In this article, we’ll look at how to deploy ProxySQL.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

916 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