Exec stored procedure into dynamic temp table

Asked at 2013-11-29 07:03:14Z
  • 5 Subscribers

To my knowledge; what I want to do is not possible in sql, but it is worth asking you guys.

Lets say I have a stored procedure abc that returns columns Id and Value. This stored procedure is mainly being used by other departments for functional reasons and I will only use it every now and again for data checks.

So using it as part of my stored procedure:

   ID      INT,
   Value   DECIMAL(12,2)

   EXEC OtherDb.DataProd.abc

Oky so this will work perfectly for now, but what if they change the structure of their stored procedure?

Adding or removing a column from their stored procedure will break my code, so is there a way of making my code more flexible.

My last desperate attempt went something like this:

WITH tempTable AS
    EXEC OtherDb.DataProd.abc
SELECT ID, Value FROM tempTable

Which obviously failed miserably.

3 answers in total

Raj Posted at 2013-11-29 07:36:51Z
SELECT * INTO #TempTable 
     'EXEC OtherDb.DataProd.abc')

SELECT * FROM #TempTable
Tim Melton Posted at 2015-04-22 16:29:45Z

Insert into a temp table. I know this works in 2008 and above, not sure about 2005. Your temp table columns must match your Stored Proc columns.

create table #mytable (custid int,company varchar(50),contactname varchar(50)
                , phone varchar(50),address1 varchar(50)
                , address2 varchar(50),city varchar(50)
                ,st varchar(2),zip varchar(20))

insert into #mytable (custid,company,contactname,phone,address1,address2,city,st,zip)
exec dbo.sp_Node_CustomerList_wService @segid = 1

select * from #mytable
where custid = 5

drop table #mytable
NickyvV Posted at 2013-11-29 08:33:46Z

Otherwise have a look over here, there are more options explained there: How to SELECT * INTO [temp table] FROM [stored procedure]?

Answer this questsion