unresolved reference to object [INFORMATION_SCHEMA].[TABLES]

SqlSql ServerTsqlSql Server-Data-Tools

Sql Problem Overview


I've created a UDF that accesses the [INFORMATION_SCHEMA].[TABLES] view:

CREATE FUNCTION [dbo].[CountTables]
(
	@name sysname
)
RETURNS INT
AS
BEGIN
	RETURN
	(
		SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @name
	);
END

Within Visual Studio, the schema and name for the view are both marked with a warning:

>SQL71502: Function: [dbo].[CountTables] has an unresolved reference to object [INFORMATION_SCHEMA].[TABLES].

I can still publish the database project without any problems, and the UDF does seem to run correctly. IntelliSense populates the name of the view for me, so it doesn't seem to have a problem with it.

I also tried changing the implementation to use sys.objects instead of this view, but I was given the same warning for this view as well.

How can I resolve this warning?

Sql Solutions


Solution 1 - Sql

Add a database reference to master:

  1. Under the project, right-click References.
  2. Select Add database reference....
  3. Select System database.
  4. Ensure master is selected.
  5. Press OK.

Note that it might take a while for VS to update.

Solution 2 - Sql

In our project, we already have a reference to master, but we had this issue. Here was the error we got:

SQL71502: Procedure: [Schema].[StoredProc1] has an unresolved reference to object [Schema].[Table1].[Property1].

To resolve the reference error, on the table sql file, right click properties and verify the BuildSettings are set to Build.

Changing it build fixed it.

Solution 3 - Sql

what Sam said is the best way for doing this.
However, if you have a scenario that you need to deploy the dacpac from a machine that doesn't have that reference in that specific location, you may get into trouble. Another way is to open your .project file and make sure the following tag has the value of false for the build configuration you are trying to run.

<TreatTSqlWarningsAsErrors>false</TreatTSqlWarningsAsErrors>

This way you don't need to add a reference to your project.

Solution 4 - Sql

I'm using VS 2019, And even after adding the master db reference still got this issue. Resolved this by Changing the target platform of the DB project as shown in the image below. I had to remove and add back the master db again after this change. enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamView Question on Stackoverflow
Solution 1 - SqlSamView Answer on Stackoverflow
Solution 2 - SqlRussell DView Answer on Stackoverflow
Solution 3 - SqltecfieldView Answer on Stackoverflow
Solution 4 - SqlTharindu JayasingheView Answer on Stackoverflow