Workaround for calling table-valued function remotely in SQL Server has even more issues

♀尐吖头ヾ 提交于 2019-11-30 08:48:04

Have you tried this variation - basically you push the call to the function to happen locally on the remote box:

EXEC REMOTE_SERVER_NAME.db_name..sp_executesql N'SELECT * 
  FROM dbo.MyTableValuedFunctionName();';

In case you can't solve it by calling an EXEC (because you're calling this function from another function or you're trying to do something like INSERT EXEC but you can't nest it, etc.), here is a workarround that solved this problem for me:

You can create a function in your local server that runs the same query as the remote function (asuming you know the implementation of the remote function) since you can access tables and "static-called-functions" (by openquery) with no problems.

In example, if the remote function is something like this:

CREATE FUNCTION dbo.[remote_function] (@param1 varchar(200))
RETURNS TABLE AS RETURN 
( SELECT col1, col2, col3 FROM [remote_db].[dbo].[remote_table] where col1 = @param1)
GO

You can create a function in your local server:

CREATE FUNCTION dbo.[local_function] (@param1 varchar(200))
RETURNS TABLE AS RETURN 
( SELECT col1, col2, col3 FROM [remote_server].[remote_db].[dbo].[remote_table] where col1 = @param1)
GO

And then just query your new function as you want...

SELECT col1, col2, col3 FROM dbo.local_function(@param1);
GO

It should work with no problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!