How to get the result sets from tsql's execute?

蹲街弑〆低调 提交于 2019-12-12 01:54:58

问题


I try to remotely query table-valued function as it is offered in this SO answer.

But I stumbled over how to get the returned result sets to further work with them in sql code...

Calling UDF remotely is not supported by SQL Server and openquery cannot have parameters - only static string.

declare @query nvarchar(max) = 'select * into #workingDays from openquery(LNKDSRV, ''select * from DB.dbo.fxn_getWorkingDays('''''
    + cast(@date1 as nvarchar(max))
    + ''''',''''' 
    + cast(@date2 as nvarchar(max))
    + ''''')'')';
exec sys.sp_executesql @query;

When #workinDays is later queried there is a error 'invalid object name'.


回答1:


You have to define your table before sp_executesql to be available in the session:

Create table #tbl  
declare @query nvarchar(max) = 'insert into #tbl select * from....
exec sys.sp_executesql @query
select * from #tbl

Another option is to use global temp table ##tbl



来源:https://stackoverflow.com/questions/27527864/how-to-get-the-result-sets-from-tsqls-execute

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