Solved

OVERWRITE RECORDS ON TABLE FROM ANOTHER TABLE

Posted on 2008-10-13
3
925 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
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.…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

19 Experts available now in Live!

Get 1:1 Help Now