Search code examples
sql-server-2014ssdt-2017

SSDT: Unresolved reference to object


I have an existing SQL Server 2014 database and I want to add it to source control (SSDT in Visual Studio 2017).

I have a database project with lot's of views and stored procedures. MyDatabase is current database.

Every view and stored procedure is written in the following way:

create view MyView 
as
    select
        Id
    from MyDatabase..MyTable

".." means the default schema name here (dbo). And it works in SQL Server. But SSDT considers such a construct as an error:

View MyView has an unresolved reference to MyDatabase.dbo.MyTable.

So SSDT knows perfectly well, that database is MyDatabase and skipped schema name is dbo.

But I can't build my project with such errors. I can't also rewrite MyDatabase..MyTable to MyDatabase.dbo.MyTable.

So is there any way to solve this problem SSDT?


Solution

  • The 3-part name could be replaced as [$(DatabaseName)]..MyTable:

    select Id from MyDatabase..MyTable
    =>
    select Id from [$(DatabaseName)]..MyTable
    

    Using local 3-part names in programmability objects

    While VSTS:DB does not support local 3 part names it does support the use of variables and literals to resolve references to external databases. The $(DatabaseName) variable is an ambient variable that will have its value replaced at the time of deployment. This variable gets its value from the project properties deployment tab. Since $(DatabaseName) is always replaced at deployment with the target database name and references through variables are resolved you may use a variable in your local 3-part names.

    Our guidance is to not use local 3-part names as it introduces an unnecessary layer of abstraction and dependency on the database name