ozz
ozz

Reputation: 183

Extract the name of a Database which is used in a view (refering other Database)

i dynamicaly create Views which refering other Databases (mainly to import data from there)

use [RezepteDB]

in this DB i create a view like this

CREATE VIEW [dbo].bla as select * from ZutatenDB.dbo.Bla

Later i need the name of the Database, on which is the view based.

For this example 'ZutatenDB'.

How can i done this? a small function to pass the name of the view and return the name of the database.

Upvotes: 1

Views: 60

Answers (1)

gotqn
gotqn

Reputation: 43636

One technique is to use sys.dm_sql_referenced_entities:

SELECT *
FROM sys.dm_sql_referenced_entities ('dbo.bla', 'OBJECT');

You can see the referenced server, database, schema and objects.

Upvotes: 1

Related Questions