SQL copy and replace if exists between databases

Hi,

I am trying to copy and replace all the data from a table (old backup of database), into the same table (current version of database) where those records exist in the the current database. So all the new records in that table in the current version of the database, are left untouched.  

Not sure if this done by using a sub-query (WHERE in) or a join?

Your help would be greatly appreciated.
boxerbillAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tpi007Commented:
-- Use EXPORT/IMPORT WIZARD
-- On differents servers, will require linked server to old server
-- One column
SELECT * FROM LinkedServerName.DatabaseName.dbo.TableName
WHERE ColumnName NOT IN (SELECT ColumnName FROM LocalDatabaseName.dbo.TableName)

-- Mulitple Column
SELECT src.* FROM LinkedServerName.DatabaseName.dbo.TableName src
WHERE NOT EXISTS (SELECT * FROM LocalDatabaseName.dbo.TableName dest
                              INNER JOIN LinkedServerName.DatabaseName.dbo.TableName src ON src.ColumnName = dest.ColumnName AND src.AnotherColumnName = dest.AnotherColumnName)

-- On same server
-- One column
SELECT * FROM SourceDatabaseName.dbo.TableName src
WHERE ColumnName NOT IN (SELECT ColumnName FROM TargetDatabaseName.dbo.TableName)

-- Mulitple Column
SELECT src.* FROM TargetDatabaseName.dbo.TableName src
WHERE NOT EXISTS (SELECT * FROM SourceDatabaseName.dbo.TableName dest
                              INNER JOIN SourceDatabaseName.dbo.TableName src ON src.ColumnName = dest.ColumnName AND src.AnotherColumnName = dest.AnotherColumnName)
 
0
boxerbillAuthor Commented:
Thank you for your post tpi007.

I ran the above query and received the following error:

ERROR:  cross-database references are not implemented: "ws_ftp_server.dbo.group_members"

I am running the query as the top level 'admin' user so not sure why this is occuring?
0
boxerbillAuthor Commented:

SELECT src.* FROM ws_ftp_server.dbo.group_members src
WHERE NOT EXISTS (SELECT * FROM ws_ftp_server2.dbo.group_members dest
                              INNER JOIN ws_ftp_server2.dbo.group_members src ON src.group_uid = dest.group_uid AND src.groupid = dest.groupid AND src.userid = dest.userid)

Open in new window

0
DrCabbageCommented:
The message you're getting is because (assuming you are using PostgreSQL) it doesn't support cross-database references, and all the tables involved in the query need to be in the same database. So you can use the solution above with the extra qualifications removed, and use INSERT to actually do it:

INSERT INTO dest SELECT * FROM src
WHERE NOT EXISTS (SELECT * FROM dest
                              INNER JOIN src ON src.group_uid = dest.group_uid AND src.groupid = dest.groupid AND src.userid = dest.userid)

If you start a transaction with BEGIN before you do this you can do some spot checks on the data to be sure you are happy before you do a COMMIT

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
boxerbillAuthor Commented:
Very usful info
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Storage Software

From novice to tech pro — start learning today.