1

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?

J.D.
  • 954
  • 6
  • 22
  • 1
    How can you do SELECT * FROM databaseName? You can select only from tables/views/functions/derived tables – Dmitrij Kultasev Nov 19 '19 at 06:39
  • Sorry, I wrote the initial question in haste but just fixed it. I want to select against an object of said referenced database but by using the actual database name itself in the query, not the database variable name notation. – J.D. Nov 19 '19 at 13:50

2 Answers2

1

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

Vadim Shkaberda
  • 2,807
  • 19
  • 35
  • 1
    Thank you for this, despite my question being old, it's appreciated to know the solution. – J.D. Dec 13 '21 at 14:05
-1

Yes, that is the only way, other than creating Synonyms for your referenced objects. Even in that case, you would still have to use the variable in the synonym.

If you are worried about a heavy lift of replacing all of the database names with variable references, do a ctrl+shift+h and replace all [database]. with [$(databasereference)].

RobC
  • 22,977
  • 20
  • 73
  • 80
Kyle J V
  • 563
  • 5
  • 21