Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,


How do I do a SELECT * INTO [temp table] FROM [EXEC SQL1+SQL2+SQL3]? Not FROM [Table] and without defining [temp table]?

Following is my syntax: EXEC @SQL1+@SQL2+@SQL3

i want to insert this resultset into temp table.

Please help me.

Thanks in advance.

What I have tried:

i have tried following two methods but it didn't work.
1)
insert into #temptable exec(@SQL1+@SQL2+@SQL3)
select * from #temptable

2)
SELECT
  *
INTO
  #temptable
FROM
  OPENROWSET(
    'SQLNCLI',
    'Server=(local)\SQL2008;Trusted_Connection=yes;',
    'EXEC ' + @SQL1 + '+' + @SQL2 +',' +
)
Posted
Updated 20-Sep-16 3:43am
v3
Comments
mansi chaudhari 12-Aug-16 3:17am    
Can you please mention what is in @sql1,@sql2,@sql3 contains?
Maciej Los 19-Sep-16 2:31am    
Totally unclear!!!

Here is the workable code...

SQL
--prepare table
create table t1(code nvarchar(10), name nvarchar(64));
go
insert into t1 values('001', 'Jon Doe');
insert into t1 values('002', 'Michael Doe');
go
--prepare stored procedure
create procedure getdata
as
begin
	--functional stuff
	select * from t1;
end


actual code to execute sp and store in temp table

SQL
CREATE TABLE #temp
(
   code nvarchar(10),
   name nvarchar(64)
)

INSERT INTO #temp
Exec getdata
 
Share this answer
 
Try with below code
SQL
SELECT  *
INTO    #tempTable
FROM    OPENQUERY(YOURSERVERNAME, 'EXEC exec(@SQL1+@SQL2+@SQL3)')


it requires additional permission on sqlserver and format the query string.
hint- [^]
 
Share this answer
 

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