Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

OVERWRITE RECORDS ON TABLE FROM ANOTHER TABLE

Posted on 2008-10-13
3
Medium Priority
?
933 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

715 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