Sql-server – SQL Server : execute stored procedure from another stored procedure

sql serverstored-procedures

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.

Best Answer

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