Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting only new and modified records?

Status
Not open for further replies.

andrewradvansky

Programmer
Mar 15, 2004
2
US
I have two tables in SQL Server. One contains existing data (Tbl1) and one is used for inserts (Tbl2) from another database. I should note that both tables contain data from the same source, so some records will be duplicates. What I would like to do is insert from Tbl2 any records that are new or modified, into Tbl1. So instead of dropping the entire Tbl1 and inserting all records from Tbl2, I would just be adding new records or updating existing records that have been changed. I have a DocID field that can be used to identify unique records. This would be used during our testing process. Can anyone help?
 
Are you asking for something like

INSERT INTO Tbl1
SELECT * FROM Tbl2
WHERE DocID NOT IN
(SELECT DocID FROM Tbl1)

 
That would insert the missing records, but not the updated records.

First, do an Update query on bl1 using an inner join between the tables, then run the query jarlh mentioned.

Or, delete all the records from tbl1 that match your primary key in tbl2, then append all tbl1's records into tbl1 with an insert clause that has no condition.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top