我已经看到其他文章谈论使用OPENQUERY调用远程服务器上的函数,但是我无法使它在下面的简单函数中起作用,该函数返回一个数值。 (我无法更改此函数,所以我被其参数和返回值所困扰):
DECLARE @ProjectDepartment NUMERIC(12,0);
DECLARE @Department NUMERIC(12,0);
SET @Department = 104;
SET @ProjectDepartment = db.schema.GetSharing(@Department, 'DeliveryPlace');
我尝试过:
SELECT
@ProjectDepartment = @ProjectDepartment
FROM
OPENQUERY(server,
'SELECT @ProjectDepartment
FROM db.schema.GetSharing(@Department, "DeliveryPlace")');
和
EXECUTE @ProjectDepartment = server.db.schema.sp_executesql
N'SELECT db.schema.GetSharing(@Department, "DeliveryPlace")',
N'@Department NUMERIC(12,0)',
@Department = 104;
和
DECLARE @sql NVARCHAR(4000);
SET @sql = 'SELECT' +
CAST(@ProjectDepartment AS NVARCHAR(12)) +
'FROM OPENQUERY(server,
''SELECT ' + CAST(@ProjectDepartment AS NVARCHAR(12)) +
' = db.schema.GetSharing(' + CAST(@Department AS NVARCHAR(12)) + ', "DeliveryPlace")'')';
EXEC(@sql);
我正在使用SQL Server Management Studio 17.9.1。
已添加:
我将其添加到测试例程中:
Use db
GO
DECLARE @ProjectDepartment NUMERIC(12,0);
DECLARE @Department NUMERIC(12,0);
SET @Department = 104;
EXEC @ProjectDepartment = server.db.schema.sp_executesql
N'SELECT GetSharing(@Department, "DeliveryPlace")',
N'@Department NUMERIC(12,0)',
@Department=@Department;
我收到此错误:
Could not find stored procedure 'db.schema.sp_executesql'.
当我在实际函数中使用此代码时,仍然出现此错误:
Remote function calls are not allowed within a function.