2017-02-14 22 views
1

我有2个程序P1和P2格式的每个给出的结果如下面动态表使用过程输出

exec p1----> #col1 #col2 
       P-1 52 
       P-2 25 

EXEC p2----> #col1 #col2 
       P-1 20 
       P-2 2 
       P-3 5 

我想调用另一过程,这些过程将执行两个过程,创建临时表和插入两个表的结果,如下所示:

#col1 #col2 #col3 
P-1 52 20 
P-2 25 2 
P-3 NULL 5 

我是新来使用临时表的任何帮助,将不胜感激。 此代码必须在SQL服务器中运行。 问候,

+0

[将存储过程的结果插入临时表中的可能副本](http://stackoverflow.com/questions/653714/insert-results-of-a-stored-procedure-into-a-temporary-表) –

+0

嗨我试过,但抛出错误。 – rahul

回答

0

我想这是你想要的。尝试一次,它可以帮助你

按照您的程序输出遵循以下查询

create table #tempp (col1 varchar(50),col2 bigint) 

insert into #temp 
exec p1 

create table #tempp1 (col1 varchar(50),col2 bigint) 

insert into #tempp1 
exec p2 

select t1.col1,t.col2,t1.col2 from #tempp t 
full join #tempp1 t1 on t.col1=t1.col1 

我已经创建的样本数据,就像你的程序输出数据和我的数据库试图

   select * into #tempp from (
       select 'p-1' as col1,52 col2 
       union all 
       select 'p-2',25 
      ) as a 

       select * into #tempp1 from (
       select 'p-1' as col1,20 col2 
       union all 
       select 'p-2',2 
       union all 
       select 'p-3',5 
      ) as a 

select t1.col1,t.col2,t1.col2 from #tempp t 
full join #tempp1 t1 on t.col1=t1.col1 

你的输出看起来像

+------+------+------+ 
| col1 | col2 | col2 | 
+------+------+------+ 
| p-1 | 52 | 20 | 
| p-2 | 25 | 2 | 
| p-3 | NULL | 5 | 
+------+------+------+