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
435 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
[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
3 Comments
 
LVL 93

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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

636 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