2012-01-16 23 views
1

我试图从另一个sproc中调用一个存储过程,并将结果放入临时表中,而不先创建临时表。有可能做到这一点,还是有更好的办法?我想使用其结果集有多列和多行的sprocB或functionB。谢谢。是否可以调用sproc中的sproc,并将结果放入#temp表中,而无需先创建临时表?

sproc A 
.. 
begin 
    -- create table #tmp.... -- Try not to create the #tmp table first if possible 
    exec sproc_B ... put results from sproc_B in #tmp 

end 

sproc B 
.. 
@id int 
.. 
begin 
    select table from aTable where id = @id 
end 

Similiar有人问here。 。

+1

可能重复[如何选择* INTO \ [临时表\] FROM \ [存储过程\]](http://stackoverflow.com/questions/653714/how-to-select-into-temp-表存储过程) – 2012-01-16 15:18:16

+0

是的,这就是我需要的。 thx – RetroCoder 2012-01-16 15:29:17

+0

但另一个问题没有提到从一个sproc调用到另一个sproc。 – RetroCoder 2012-01-16 15:31:41

回答

4
create table #tmp.... 

    insert #tmp 
    exec sproc_B 
+0

是否可以在不首先创建临时表的情况下将其添加到临时表中? – RetroCoder 2012-01-16 15:13:33

+1

@RetroCode:否。另一种方法是使用可怕的表格UDF。 – 2012-01-16 15:15:34

+1

@RetroCoder:没有。 INSERT .. EXEC需要一个预先存在的表 – gbn 2012-01-16 15:16:00

2
CREATE TABLE #tmpTable 
(
    COL1 INT, 
    COL2 INT 
) 

INSERT INTO #tmpTable 
Exec spGetResultset 'Params' 
1

是的,但你必须使用之前创建表的语法是:

INSERT INTO YourTable EXEC yourproc上

没有必要说,对这个结构表应该与SP的输出相匹配?

+0

嗨,是的,但我也可以没有先创建临时表,这样做...从tSomeTable选择*到#aNewTempTable – RetroCoder 2012-01-16 15:18:57

+0

没有biggy,我只是改变我的问题,b/c这就是我想要的去做。 – RetroCoder 2012-01-16 15:21:39

相关问题