How to get results of stored procedure #1 into a temporary table in stored procedure #2

折月煮酒 提交于 2019-12-10 21:24:50

问题


I am trying to combine the results of several stored procedures into a single temporary table. The results of the various stored procedures have the same column structure. Essentially, I would like to UNION ALL the results of the various stored procedures. A significant fact: each of the stored procedures creates a temporary table to store its data and the results each returns are based on a select against the temporary table:

create proc SP1    
as
 .
 .  <snip>
 .
 select * from #tmp   -- a temporary table

Noting that select * from OPENQUERY(server, 'exec SP1') does not work if the select in SP1 is against a temporary table (see this question for details), is there another way for a different stored proc, SP2, to get the results of executing SP1 into a temporary table?

  create proc SP2 as
  -- put results of executing SP1 into a temporary table:
  .
  .
  .

NOTE: SP1 cannot be modified (e.g. to store its results in a temporary table with session scope).


回答1:


Create your temporary table such that it fits the results of your stored procedures.

Assuming your temp. table is called "#MySuperTempTable", you would do something like this...

INSERT INTO #MySuperTempTable (Column1, Column2)
EXEC SP1

That should do the trick.




回答2:


INSERT INTO #MySuperTempTable 
EXEC SP1

Following above code style but sqlserver is not compialing instructions.



来源:https://stackoverflow.com/questions/14294311/how-to-get-results-of-stored-procedure-1-into-a-temporary-table-in-stored-proce

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