views:

77

answers:

2

I have to create a temp table (#temp) from a variable or stored procedure

(i.e)

My stored procedure contains

......

set @sql='select ...'
set @sql=@sql+'..join..'
set @sql=@sql+'....join..'

when i execute it (i.e) exec (@sql) it returns some rows,i want to store that rows in a temp table.How to acheieve it?

i tried something like

select
       id,name,ward_no into,........ 
#temp

from
   ( 
     exec (@sql)
   )derived

Update

I found the logic

(i.e)

create table #temp(....)

insert #temp exec(@sql) 

select * from #temp
A: 

change #temp to ##temp and you'll be able to do that

Vidar Nordnes
+1  A: 

Not exactly what you asked for in your question, but this might be one way of doing what you're after anyway:

CREATE TABLE #temp (
ID INTEGER
)

DECLARE @Sql NVARCHAR(100)
SET @Sql = 'INSERT INTO #temp VALUES (5)'

EXEC(@Sql)

SELECT * FROM #temp
Will A