How can I do a join on tables of 2 different db
in same domain
like
SET DBname = \"sample\"
SELECT *
FROM Table1
INNER JOIN DBname.Tab
You have to use dynamic sql:
@DBname = 'sample';
declare @sql nvarchar(max);
set @sql = '
SELECT *
FROM Table1 INNER JOIN
@DBname.table2
On Table1.key = Table2.Key';
set @sql = replace(@sql, '@DBname', @DBname);
exec sp_executesql @sql;
Databases in general -- and SQL Server in particular -- do not allow parameters to represent database names, schema names, table names, or column names.