从SchemaB SQL Server中定义的视图调用schemaA中定义的用户定义函数

时间:2022-05-31 23:58:41

I am having a problem to call a function from my view. They are both in different schemas, so I have something like this:

我有个问题要从我的视图中调用一个函数。它们都有不同的模式,所以我有这样的东西:

View [SchemaA].[ViewName]
    WITH SCHEMABINDING
    AS
        SELECT DISTINCT
             [SchemaB].[functionName](value) as 'variable'
        FROM
            //Several selects with joins

The problem is that I am getting this error: "Cannot schema bind view 'SchemaA.view'. 'SchemaB.functionName' is not schema bound."

问题是我得到了这个错误:“无法使用模式绑定视图' schema .view'。“SchemaB。functionName'不是模式绑定。

I have also tried to include the database name in the call to the function like this:

我还试图在调用函数时包含数据库名如下:

[database].[SchemaB].[functionName]

But it still didn't work. However it returned a different error: "Cannot schema bind view 'SchemaA.view' because name 'SchemaB.functionName' is invalid for schema binding. Names must be in two-part format and an object cannot reference itself."

但它仍然不起作用。但是它返回了一个不同的错误:“无法使用模式绑定视图的SchemaA。”视图SchemaB因为名字。functionName'对于模式绑定无效。名称必须是两部分格式,对象不能引用自己。

Any idea about what I am doing wrong?

知道我做错了什么吗?

Thanks

谢谢

1 个解决方案

#1


2  

Your function should be schema bound (WITH SCHEMABINDING):

您的函数应该是架构绑定(带有SCHEMABINDING):

CREATE FUNCTION SchemaBinded(@INPUT INT)
RETURNS INT WITH SCHEMABINDING
BEGIN
RETURN @INPUT * 2 + 50
END
GO

#1


2  

Your function should be schema bound (WITH SCHEMABINDING):

您的函数应该是架构绑定(带有SCHEMABINDING):

CREATE FUNCTION SchemaBinded(@INPUT INT)
RETURNS INT WITH SCHEMABINDING
BEGIN
RETURN @INPUT * 2 + 50
END
GO