简体   繁体   English

查看存储过程的结果

[英]View results of Stored Procedure

I have a stored procedure that executes a dynamically built string. 我有一个执行动态构建的字符串的存储过程。

It unions several select statements from a constantly changing schema on another server I have no control over (hence the dynamic string). 它将来自另一台无法控制的服务器上不断变化的模式的几个select语句联合起来(因此是动态字符串)。 I want to be able to access the results of this procedure from a view, but this is where I get stuck. 我希望能够从视图中访问此过程的结果,但这是我遇到的问题。

I created a stored procedure (code below) which outputs my results to a table, but I want to be able to perform joins, etc. so it would be really convenient if there was some way to wrap this into a view- I'm thinking a table valued function, but I haven't quite figured out how to get the dynamic SQL into a function... any help is appreciated! 我创建了一个存储过程(下面的代码),它将我的结果输出到一个表,但我希望能够执行连接等,所以如果有一些方法将它包装到视图中会非常方便 - 我是想一个表有价值的功能,但我还没有想出如何将动态SQL变成一个函数...任何帮助表示赞赏!

CREATE PROCEDURE [dbo].[usp_test]
 AS
 SET NOCOUNT ON;
DECLARE @sql VARCHAR(MAX) 

SELECT @sql = ISNULL(@sql+'

','')+'SELECT TOP (900) *
                                   FROM   OPENQUERY(Linked_Server, 
                                                  'SELECT   col1, col2, col3

FROM dbo.'+  tableName+'
               +'
               UNION' FROM  dbo.ls_views

--dbo.ls_views is a view with the pertinent views/table names from the other server.

Set @sql = @sql+ '
Select top (0) ''1'', ''2'',''3'' from sys.tables '           
--last select statement is to end multiple unions... not sure if there is a better way, but this works.

    --PRINT (@sql);
        --EXEC  (@sql);
EXECUTE sp_Executesql @sql
    GO

Can you have SQL Agent execute this periodically and dump the results into another table/set of tables in your db? 您是否可以让SQL代理定期执行此操作并将结果转储到数据库中的另一个表/一组表中?

Then you could use it as a straight join in whatever query you want. 然后你可以在任何你想要的查询中使用它作为直接连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM