Search code examples
sql-servervisual-studiosql-server-data-toolsvisual-studio-2019sql-server-data-project

Is there a way to use the actual database name as the Database Variable Name in a Referenced Database of a SQL Server Database Project?


It seems like when you add another database as a reference to a SQL Server Database Project, the way to reference that database in the query code is via the Database Variable Name which is the notation $(ReferencedDatabaseName):

Referenced Database - Database Variable Name

So a query in the primary database would then reference an object from the referenced database in the following way:

SELECT Field1, Field2
FROM [$(ReferencedDatabaseName)].SchemaName.ObjectName

Is there a way to just directly reference the database by it's actual name, e.g.:

SELECT Field1, Field2
FROM ReferencedDatabaseName.SchemaName.ObjectName

If not, does that mean if I import an existing database that references objects in other databases into my solution, I need to replace all the referenced database names with this database variable name notation?


Solution

  • The literal database name could be used. The "Database variable" field has to be omitted when Database Reference is created.

    DB_create

    There is a bug which causes Database Variable Name to be empty thereafter.

    Bug

    All you need to do is to reload solution.

    Fixed_name

    Now the database can be referenced by it's actual name.

    Query_example