23

We're using two schemas in our project (dbo + kal).

When we are trying to create a view with the following SQL statement, Visual Studio shows as an error in the error list.

CREATE VIEW [dbo].[RechenketteFuerAbkommenOderLieferantenView]
AS
    SELECT
        r.Id as RechenkettenId,
        r.AbkommenId,
        r.LieferantId,
        rTerm.GueltigVon,
        rTerm.GueltigBis,
        rs.Bezeichnung,
        rs.As400Name
    FROM
        [kal].[Rechenkette] r
    JOIN
        [kal].[RechenketteTerm] rTerm ON rTerm.RechenketteId = r.Id
    JOIN
        [kal].[Basisrechenkette] br ON rTerm.BasisrechenketteId = br.Id
    JOIN
        [kal].[Rechenkettenschema] rs ON rs.Id = br.Id
    WHERE 
        r.RechenkettenTyp = 0

The error message looks like this:

SQL71501: Computed Column: [dbo].[RechenketteFuerAbkommenOderLieferantenView].[AbkommenId] contains an unresolved reference to an object. Either the object does not exist or the reference is ambiguous because it could refer to any of the following objects:
[kal].[Basisrechenkette].[r]::[AbkommenId], [kal].[Rechenkette].[AbkommenId], [kal].[Rechenkette].[r]::[AbkommenId], [kal].[Rechenkettenschema].[r]::[AbkommenId] or [kal].[RechenketteTerm].[r]::[AbkommenId].

Publishing the view and working is just fine, but its quite annoying to see the error message all the time when building our project having all the serious errors get lost in the shuffle of those sql errors.

Do you have any idea, what the problem might be?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jannik
  • 2,310
  • 6
  • 32
  • 61

11 Answers11

7

I just found the solution. Although I can't read your (what appears to be German) enough to know if you're referring to system views, if so, a database reference to master must be provided. Otherwise, adding any other required database references should solve the problem.

This is described here for system views: Resolve reference to object information schema tables

and for other database references.

Additional information is provided here: Resolving ambiguous references in SSDT project for SQL Server

Community
  • 1
  • 1
devinbost
  • 4,658
  • 2
  • 44
  • 57
  • 3
    Thanks for the late answer. I moved to another company, unfortunately I can't test it out anymore. – Jannik Oct 21 '15 at 12:57
5

We have a project that contains a view that references a table valued function in another database. After adding the database reference that is required to resolve the fields used from the remote database, we were still getting this error. I found that the table valued function was defined by using "SELECT * FROM ..." which was old code created by someone not familiar with good coding practices. I replaced the "*" portion with the enumerated fields needed and compiled that function, then re-created the dacpac for that database to capture the resulting schema, and incorporated the new dacpac as the database reference. Woo Hoo! the ambiguous references went away! Seems that SSDT engine cannot (or does not) always have the ability to reach down into the bowels of the referenced dacpac to come back with all the fields. For sure, the projects I work on are normally quite large, so I think it makes sense to give the tools all the help you can when asking them to validate your code.

user6697603
  • 51
  • 1
  • 1
5

I know this is an old question but it was the first one that popped up when searching for the error.

In my case the errors were preventing me from executing the SqlSchemaCompare in Visual Studio 2017. The error however was for a table/index of a table that was not part of the solution any more. A simple clean/rebuild did not help.

A reload of the visual studio solution did the trick.

FlorianB
  • 116
  • 1
  • 4
5

For me I was seeing SQL71501 on a user defined table type. It turned out that the table type's sql file in my solution wasn't set as build. As soon as I changed the build action from None to Build, the error dissapeared.

John Warlow
  • 2,922
  • 1
  • 34
  • 49
3

Although this is an old topic, it is highly ranked on search engines, so I will share the solution that worked for me.

I faced the same error code with a CREATE TYPE statement, which was in a script file in my Visual Studio 2017 SQL Server project, because I couldn't find how to add a user-defined type specifically from the interface.

The solution is that, in Visual Studio, there are many programmability file types, other than the ones you can see through a right-click > Add. Just select New Element and use the search field to find the element you are trying to create.

3

From the last paragraph of the blog post Resolving ambiguous references in SSDT project for SQL Server, which was linked in the answer https://stackoverflow.com/a/33225020/15405769 :

In my case, when I double clicked the file and opened it I found that one of the references to ColumnX was not using the two part name and thus SSDT was unable to determine which table it belonged to and furthermore whether the column existed in the table. Once I added the two part name. Bingo! I was down to no errors!

Amirhossein
  • 1,148
  • 3
  • 15
  • 34
  • In my case just double clicked on the error and it will take me to the exact error on procedure and I noticed that used table column is deleted or renamed but in sp its still old column name. – Saad Awan Jun 10 '21 at 12:57
2

If you build an SSDT project you can get an error which says:

“SQL71502: Function: [XXX].[XXX] has an unresolved reference to object [XXX].[XXX].”

If the code that is failing is trying to use something in the “sys” schema or the “INFORMATION_SCHEMA” schema then you need to add a database reference to the master dacpac:

Add a database reference to master:

Under the project, right-click References. Select Add database reference….

enter image description here

Select System database. Ensure master is selected.

enter image description here

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

(Note this was copied verbatim from the stack overflow question with my screenshots added: https://stackoverflow.com/questions/18096029/unresolved-reference-to-obj… - I will explain more if you get past the tldr but it is quite exciting! )

NOT TLDR: I like this question on stack overflow as it has a common issue that anyone who has a database project that they import into SSDT has faced. It might not affect everyone, but a high percentage of databases will have some piece of code that references something that doesn't exist.

The question has a few little gems in it that I would like to explore in a little more detail because I don't feel that a comment on stack overflow really does them justice.

If we look at the question it starts like this:

enter image description here

Nader Gharibian Fard
  • 6,417
  • 4
  • 12
  • 22
1

In my case, I got this error when I was trying to export the datatier application. The error was related to the link on a database user. To solve the problem, you need to log in to the server with read rights on system users.

Renzo Ciot
  • 3,746
  • 2
  • 25
  • 29
1

In my case I just double click on the error and it will take me to the exact error on procedure and I noticed that table column is deleted or renamed but in SP its still using the old column name.

Saad Awan
  • 566
  • 2
  • 9
  • 23
0

If you're doing this from within Visual Studio, make sure that the file is set to "Build" within the properties.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
0

I've had this numerous times and it really gets me everytime. SQL Build is case sensitive even though your collation isn't. Check the case is correct in agreement with the object and schema names that are referenced!

Shaun Ryan
  • 1,458
  • 12
  • 16