Link to home
Start Free TrialLog in
Avatar of gabepcsolutions
gabepcsolutions

asked on

OVERWRITE RECORDS ON TABLE FROM ANOTHER TABLE

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?
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

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

Avatar of gabepcsolutions
gabepcsolutions

ASKER

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]
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial