简体   繁体   中英

Insert sql query result inside stored procedure in temp table

I have stored procedure in which i store whole query inside string and then execute that. Now i want to store that execute result into temporary table for further processing. Something like below :

Exec @Mainsql -- this returns me query result and i need to insert its result to temp table

I tried something like this:

Select * Into #TempTable
   From         
      Exec @MainSQL 

But It is lacking in syntax i guess.

So, i need result of mainsql into temptable

Try this:

CREATE TABLE #TempTable AS 
Exec @MainSQL 

You must create Temp Table first, You have to define all columns which will be returned from procedure, if you need to insert data using Stored Procedure:

CREATE TABLE #TempTable (Col1 INT, Col2 VARCHAR(10))
INSERT INTO #TempTable
EXEC [ProcedureName]

Another option is to use OPENROWSET , if you do not know returned columns :

SELECT * INTO #TempTable
FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;', 'EXEC DBName.Schema.ProcedureName')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM