Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have been using an SQL statement that will allow me to use tables from the same database for comparison. Here is the code:

Merge into table1 as T
using [table] as S
on T.[Last Name] = S.[Last Name] and T.[First Name] = S.[First Name]

When Matched then 
Update Set T.[Companynamne] = S.[Companyname];

DELETE T1 FROM [table] T1 JOIN [table1] T2 ON T1.[Last Name] = T2.[Last Name] AND T1.[First Name] = T2.[First name];


I tried to use the same code to compare another table from another database, I kept getting an out of object error.

I would like to use this same code but with modifications, what would I need to change to get the information from the second database as S in the SQL formula and T1 as the delete statement for the same thing?

What I have tried:

I couldn't figure out how to change it to recognize that it's for comparing 2 tables from 2 databases.
Posted
Updated 10-Apr-17 7:14am

1 solution

Simply add the name of the databases in front of the table name - example:
SQL
[database1].[dbo].[table_A]

and
SQL
[database2].[dbo].[table_B]


If the databases are in two different servers, just add the SQL server instance name the same way:

SQL
[Server1].[database1].[dbo].[table_A]

and
SQL
[Server2].[database2].[dbo].[table_B]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900