Database Reference in SSDT project with UDFs and Views
Asked Answered
G

1

1

Running into a weird issue. Assuming there are two database projects in one empty solution, Bart and Homer. Bart has been added as a Database Reference to Homer.

Bart project defines a function:

CREATE FUNCTION [dbo].[Message]()
RETURNS NVARCHAR(255)
AS
BEGIN
    RETURN 'I am a value returned from another database'
END

Then the Homer project defines a table:

CREATE TABLE [dbo].[Messages]
(
     [Id] INT NOT NULL PRIMARY KEY
)

and a view:

CREATE VIEW [dbo].[MessagesV]
    AS SELECT Id, Bart.dbo.Message() AS [Message]
    FROM dbo.Messages

When trying to build, I am getting these errors:

Error   2   SQL71501: Computed Column: [dbo].[MessagesV].[Message] 
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: [Bart].[dbo].[Message] or 
[dbo].[Messages].[Bart]::[dbo].[Message].   

Error   1   SQL71501: View: [dbo].[MessagesV] 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: 
[Bart].[dbo].[Message] or [dbo].[Messages].[Bart]::[dbo].[Message]. 

How should I reference Bart UDFs correctly in a view?

Gainly answered 15/5, 2015 at 22:37 Comment(0)
N
5

When you add a database reference, by default it sets a database variable to use in your TSQL scripts. This allows your project to target multiple environments, where the referenced DB may have a different name.

The correct usage given this default would be:

CREATE VIEW [dbo].[MessagesV]
AS SELECT Id, [$(Bart)].dbo.Message() AS [Message]
FROM dbo.Messages

I've verified this works for the code snippet you sent. Note that if you do want to use the Database Name directly you can simply delete the Database Variable value when adding the reference. Then Bart.dbo.Message() will work as expected for you.

Shown below is the Add Database Reference dialog with the relevant Database Variable and Example Usage shown. You will see the usage change depending on whether there is any text in the database variable text box.

enter image description here

Nick answered 16/5, 2015 at 19:3 Comment(1)
I'm having the same issue. But I don't use database references at all. It's just a single, standalone database. See: #57712171Cree

© 2022 - 2024 — McMap. All rights reserved.