Satyendra Routela
Satyendra Routela

Reputation: 77

Query having two table from different database each without db prefix

i have one table (tb1) in db1 and one tb2 in db2.

if i join two table as follow

select *from db1.tb1 join db2.tb2 on tb1.id=tb2.id

if db1 is default database then i can write it as following

select *from tb1 join db2.tb2 on tb1.id=tb2.id

now, what i want is to not use the prefix for tb2 i.e. (db2)

Upvotes: 1

Views: 85

Answers (1)

PSK
PSK

Reputation: 17943

If you don't give prefix as DB2, then SQL Server will try to find the table in connected database only and your query will fail.

You can try creating synonyms for the table.

CREATE SYNONYM tbl2 FOR db2.dbo.tb2

This way you can hide the database.

Upvotes: 2

Related Questions