# Database Discussions > MS SQL Server 7/MS SQL Server 2000 >  How do you reference a table in another (SQL) database in TSQL

## gurmi

Good afternoon one and all,

 I am trying to write a query that will use tables in more than one SQL database (on the same server). How do refer to the table? I have tried dbasename.tablename.fieldname but I don&#39;t think that&#39;s working.

TIA for any and all help

Gurmi

----------


## Jun

The only thing you are missing is the DB owner. You use

Either: DBname.dbo.Tablename.field

OR: DBname.actualDBOwner.Tablename.field

OR: DBname..Tablename.field


Jun

------------
gurmi at 1/9/01 11:13:46 AM


Good afternoon one and all,

 I am trying to write a query that will use tables in more than one SQL database (on the same server). How do refer to the table? I have tried dbasename.tablename.fieldname but I don&#39;t think that&#39;s working.

TIA for any and all help

Gurmi

----------


## Peter Roy

try this...

login to database A and then type...

eg.
    select * from B.dbo.xxx

where B is the other database and dbo is the owner (dbo is usually the default owner)




------------
gurmi at 1/9/01 11:13:46 AM


Good afternoon one and all,

 I am trying to write a query that will use tables in more than one SQL database (on the same server). How do refer to the table? I have tried dbasename.tablename.fieldname but I don&#39;t think that&#39;s working.

TIA for any and all help

Gurmi

----------


## axel

Hi,

I agree with the other answers. 

But there is more standard with use <databasename>

execute(&#39;use B select * from dbo.xxx&#39 :Wink: 
instead of
select * from B.dbo.xxx

Here is the same but if you use other SQL instructions (enable, disable triggers) using &#39;use <db>&#39; is the only one way !

Best regards
Axel


------------
Peter Roy at 1/9/01 11:26:37 AM


try this...

login to database A and then type...

eg.
    select * from B.dbo.xxx

where B is the other database and dbo is the owner (dbo is usually the default owner)




------------
gurmi at 1/9/01 11:13:46 AM


Good afternoon one and all,

 I am trying to write a query that will use tables in more than one SQL database (on the same server). How do refer to the table? I have tried dbasename.tablename.fieldname but I don&#39;t think that&#39;s working.

TIA for any and all help

Gurmi

----------

