I have two stored procedures.
Proc1
executes a SQL statement and results a data table as a result set.I need to use the datatable in a select SQL that resides in
proc2
as a datasource. like,SELECT Col1 FROM (Exec Proc1)
How can I do it? This syntax here shows error in SQL Server.
marc_s
9,0626 gold badges46 silver badges52 bronze badges
asked Apr 13, 2015 at 9:17
-
If it only returns data without modifying it, maybe that stored procedure should be a function.spaghettidba– spaghettidba2015年04月13日 10:41:46 +00:00Commented Apr 13, 2015 at 10:41
1 Answer 1
you could store your result in a Table Variable
DECLARE @TableVar TABLE (col1 varchar,col2 int,...)
insert into @TableVar
exec (Proc1)
select Col1 from @TableVar
answered Apr 13, 2015 at 9:53
-
1Please keep in mind that table variables lack statistics and thus might throw the query optimizer to very wrong a path.vonPryz– vonPryz2015年04月13日 10:00:56 +00:00Commented Apr 13, 2015 at 10:00
-
Stijn Thanks for the idea, Is there any other possibilities without using #temp table or table variable?lourdh– lourdh2015年04月13日 11:12:31 +00:00Commented Apr 13, 2015 at 11:12
-
1You cannot use the results of a stored proc directly - you need to store that into an in-memory or temporary table, this a duplicate of your question :) stackoverflow.com/questions/2847327/…Stijn Wynants– Stijn Wynants2015年04月13日 11:26:55 +00:00Commented Apr 13, 2015 at 11:26
lang-sql