xpt
xpt

Reputation: 22994

tsql: select view from different database

Is it possible to select view defined in different database in MS SQL Server?

All my searching results point to defining view to use data from different database, but haven't found if it possible to select view from another database yet.

Upvotes: 2

Views: 5477

Answers (2)

GuidoG
GuidoG

Reputation: 12014

suppose you want to do a select on database DBOther than it would be :

select * from DBOther..TableName

Also check if the table or view is on the dbo schema, if not you should add the schema also : Please notice I use only one dot now after the database name

select * from DBOther.dbo.ViewName

Upvotes: 4

Josan
Josan

Reputation: 722

Make sure the Database is in the Linked Server if they are not on the same server.

Then you can access the table or view on that database via:

SELECT * FROM [AnotherServerName].[DB].[dbo].[Table]

If on same server:

SELECT * FROM [DB].[dbo].[Table]

Upvotes: 3

Related Questions