Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to store value in temptable after exec statement

I have one,-
exec (@sql )

Here @sql contains a select statement. I want to store that result into a temp table. but it will store after the exec statement.
Posted
Updated 24-Nov-11 20:01pm
v2


Something like this you have to create the Temp Table

CREATE TABLE #LocalTempTable(
UserID int,
UserName varchar(50), 
UserAddress varchar(150))



And for inserting in Temp table
insert into #LocalTempTable values ( 1, 'Abhijit','India');


And for seleting

select * from #LocalTempTable
 
Share this answer
 
Comments
Prasanta_Prince 25-Nov-11 2:22am    
Here create table will not work .. I cant recognise table column datatype and all that . something like select * into from tableName can work.
Following will work if the columns returned by exec statement are same as the columns specified for temporary table.

insert into @tempTable
exec(@sql)


Complete example:

SQL
declare @tempTable table(userid int, userName varchar(50))

declare @sql varchar(1000)

set @sql = 'select 1, ''test user'''

insert into @tempTable
exec (@sql)

select * from @tempTable
 
Share this answer
 
v2
Have a look at similar Question-Answer discussion below.

Dynamic SQL results into temp table in SQL Stored procedure
 
Share this answer
 
Comments
Prasanta_Prince 25-Nov-11 2:22am    
Here create table will not work .. I cant recognise table column datatype and all that . something like select * into from tableName can work.
Here create table will not work .. I cant recognise table column datatype and all that . something like select * into from tableName can work but i have to implement after exec statement, not with in the exec statement.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900