1

I have two stored procedures.

  1. Proc1 executes a SQL statement and results a data table as a result set.
  2. 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
1
  • If it only returns data without modifying it, maybe that stored procedure should be a function. Commented Apr 13, 2015 at 10:41

1 Answer 1

2

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
3
  • 1
    Please keep in mind that table variables lack statistics and thus might throw the query optimizer to very wrong a path. Commented Apr 13, 2015 at 10:00
  • Stijn Thanks for the idea, Is there any other possibilities without using #temp table or table variable? Commented Apr 13, 2015 at 11:12
  • 1
    You 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/… Commented Apr 13, 2015 at 11:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.