遇到一个奇怪的问题。假设一个空解决方案中有两个数据库项目: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?
添加数据库引用时,默认情况下它会设置要在 TSQL 脚本中使用的数据库变量。这允许您的项目面向多个环境,其中引用的数据库可能具有不同的名称。
给定此默认值的正确用法是:
CREATE VIEW [dbo].[MessagesV]
AS SELECT Id, [$(Bart)].dbo.Message() AS [Message]
FROM dbo.Messages
我已经验证这适用于您发送的代码片段。请注意,如果您确实想直接使用数据库名称,则只需在添加引用时删除数据库变量值即可。然后 Bart.dbo.Message() 将按预期工作。
下面显示的是“添加数据库引用”对话框,其中显示了相关的数据库变量和示例用法。您将看到用法发生变化,具体取决于数据库变量文本框中是否有任何文本。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)