【问题标题】:Database Reference in SSDT project with UDFs and ViewsSSDT 项目中的数据库参考与 UDF 和视图
【发布时间】:2015-05-15 22:37:35
【问题描述】:

遇到一个奇怪的问题。假设在一个空的解决方案 Bart 和 Homer 中有两个数据库项目。 Bart 已添加为 Homer 的数据库引用。

Bart 项目定义了一个函数:

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

然后 Homer 项目定义了一个表:

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

还有一个观点:

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

尝试构建时,出现以下错误:

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]. 

我应该如何在视图中正确引用 Bart UDF?

【问题讨论】:

    标签: sql-server-data-tools


    【解决方案1】:

    当您添加数据库引用时,默认情况下它会设置一个数据库变量以在您的 TSQL 脚本中使用。这允许您的项目针对多个环境,其中引用的数据库可能具有不同的名称。

    给出这个默认值的正确用法是:

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

    我已验证这适用于您发送的代码 sn-p。请注意,如果您确实想直接使用数据库名称,您可以在添加引用时简单地删除数据库变量值。然后 Bart.dbo.Message() 将为您按预期工作。

    下面显示的是“添加数据库引用”对话框,其中显示了相关的数据库变量和示例用法。根据数据库变量文本框中是否有任何文本,您将看到用法变化。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    相关资源
    最近更新 更多