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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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.

744 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

13 Experts available now in Live!

Get 1:1 Help Now