Solved

OVERWRITE RECORDS ON TABLE FROM ANOTHER TABLE

Posted on 2008-10-13
3
924 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
  • 2
3 Comments
 
LVL 59

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 59

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

11 Experts available now in Live!

Get 1:1 Help Now