Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用过程输出的动态表_Sql_Sql Server_Stored Procedures_Dynamic Queries - Fatal编程技术网

Sql 使用过程输出的动态表

Sql 使用过程输出的动态表,sql,sql-server,stored-procedures,dynamic-queries,Sql,Sql Server,Stored Procedures,Dynamic Queries,我有两个程序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 我想在另一个过程中调用这些过程,该过程将执行两个过程,创建一个临时表并插入两个表的结果,如下所示: #co

我有两个程序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 server中运行。
您好,

我想这正是您想要的。试一下可能会对您有所帮助

根据您的程序输出以下查询

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 |
+------+------+------+

我想这就是你想要的。试一下也许会对你有帮助

根据您的程序输出以下查询

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 |
+------+------+------+

可能重复的Hi我试过了,但是抛出错误。可能重复的Hi我试过了,但是抛出错误。