Solved

OVERWRITE RECORDS ON TABLE FROM ANOTHER TABLE

Posted on 2008-10-13
3
931 Views
Last Modified: 2012-05-05
I need to replace a number of records from database1.table to database2.table , I dont want to copy them from table to table because I dont want duplicates, I want to replace records.

The ideas is restore information from a backup database to the current database, what would be the best way to write the query?
0
Comment
Question by:gabepcsolutions
[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
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22708538
Something like this should be what you want.  Replacing the column and table names as appropriate.  ID was used to represent the link between the tables to establish same records.  If there are more fields involved in the link just add to the ON statement and add WHERE clause as needed.

If you only want to update from t1 if a valid value is there, you can use:

t2.colname = isnull(t1.colname, t2.colname)

This will leave value as is if column is null in table1.
UPDATE t2
SET t2.col1 = t1.col1
, t2.col2 = t1.col2
, t2.col3 = t1.col3
, t2.col4 = t1.col4
, t2.col5 = t1.col5
FROM Database2.dbo.Table t2 
INNER JOIN Database1.dbo.Table t1
ON t2.ID = t1.ID

Open in new window

0
 

Author Comment

by:gabepcsolutions
ID: 22717098
Actually, what I need is to select all records on CLIENT LIST table that match a CODEID and replace the entire record.

For example, I did this in the past to replace the entire table.

SELECT *
INTO [PIProDev].[dbo].[CLIENT LIST]
FROM [PIPro].[dbo].[CLIENT LIST]
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22720864
You can insert NEW records like this:

INSERT INTO PIProDev.dbo.[Client List]
SELECT * FROM PIPro.dbo.[Client List]
WHERE /* some condition is met */

However, keeps sounding like to me you have an EXISTING record and want to update.  You will either have to do what I suggested and hand write out each column value to replace.  Or alternatively, you will have to use the criteria to find and delete those records from the PIProDev version of table and use INSERT INTO statement above as if these were new records.

Hope that helps and I apologize for and in advance as I am out of pocket a bit this week and so my responses may be a bit delayed.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

624 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