Insert sql query result inside stored procedure in temp table - sql

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')

Related

Inserting into a user-defined table type via a dynamic query in SQL Server?

I have a user-defined table type tyAnalysisNumbers. I need to populate my user defined data type within a stored procedure with a SELECT statement and I am struggling to get that working within my stored procedure.
The following ways I have tried do not work
DECLARE #MyTable tyAnalysisNumbers;
INSERT INTO #MyTable
EXEC ('SELECT * FROM ' + #someTable);
I get this error:
An INSERT EXEC statement cannot be nested
I am unsure how to insert into my custom table via a select statement.
Can anyone help me accomplish this?
An INSERT EXEC statement cannot be nested
Above error is self explanatory. Please look at below scenario:
For example, we have one procedure which inserts data in table type and return result.
CREATE PROCEDURE uspInsertData1
AS
BEGIN
DECLARE #MyTable tyAnalysisNumbers;
INSERT INTO #MyTable
EXEC ('SELECT * FROM someTable');
select * from #MyTable
END
Now, let's say we have another procedure which will call above procedure and again insert data in another table.
CREATE PROCEDURE uspInsertData2
AS
BEGIN
DECLARE #MyTable tyAnalysisNumbers;
INSERT INTO sometable
EXEC uspInsertData1
END
Now, if you execute 1st procedure it will work fine but if you execute second procedure you will get this error.
An INSERT EXEC statement cannot be nested.
Because now you have nested EXEC statements.
I suggest to finish your work in single stored procedure if possible.
Try it like this:
DECLARE #MyTable tyAnalysisNumbers;
SELECT * INTO #Temp FROM #MyTable;
DECLARE #tblName AS SYSNAME = (SELECT name FROM sys.tables WHERE name = #someTable);
EXEC ('INSERT INTO #Temp SELECT * FROM ' + #tblName);
This also addresses the SQL Injection problem.

How to get a dataset returned from a stored procedure in temp table

I have a stored procedure which returns a dataset
Let's say its name is spx and it returns
I'd name
1. Abc
2. Def
I want to get it's result in a temp table in another stored procedure dynamically like if in future I change the dataset in my above so it will reflect here
Like this
exec spx
It will execute the SP and I want it's result set in a table
Any help would be much appreciated. Thanks.
If you want to define the temporary table you can use standard SQL
CREATE TABLE #tmpTable
(
ID INT,
Name nvarchar(50)
)
INSERT INTO #TempTable
EXEC spTest
Select * FROM #TempTable
If you don't want to define the table you can use OPENROWSET
SELECT * INTO #TempTable FROM OPENROWSET('SQLNCLI', 'Server=(local);Trusted_Connection=yes;', 'EXEC spTest')
Select * FROM #TempTable
You can try this:
SELECT * INTO [temp-table] FROM OPENQUERY("server-name", 'EXEC spx');

Is it possible to call a stored procedure within an INSERT statement?

In a nutshell, here's what I'm trying to do:
CREATE PROCEDURE sp_insertingStuff
#value1
AS
INSERT INTO [my_table] (column1, column2)
VALUES (#value1, execute sp_anotherSP)
GO
What I need to know is: is it possible to execute a stored procedure from within an INSERT statement like that? If there's a better way to do this I'm open to the advice. The reason I need to do something like this is because I need to run some IF statements to get the value for that second column.
execute the second one just above the insert statement inside the first sp
like:
CREATE PROCEDURE sp_insertingStuff
#value1
AS
declare #value2 int
exec #value2 = sp_anotherSP
INSERT [my_table] (
column1,
column2
)
VALUES (
#value1,
#value2
)
GO
You can not execute a sp in the VALUES part. You have to execute and get the return/out value before insert, like this.
CREATE PROCEDURE sp_insertingStuff
#value1
AS
DECLARE #anotherValue INT
execute #anotherValue = sp_anotherSP
INSERT [my_table] (
column1,
column2
)
VALUES (
#value1,
#anotherValue
)
Add OUTPUT parameter in you sp_anotherSP
https://technet.microsoft.com/en-us/library/ms187004(v=sql.105).aspx
and
DECLARE #Val <type>
EXEC sp_anotherSP #Val OUTPUT
... insert using #val
One mentioned solution is to add an OUTPUT parameter. Another is to rewrite your stored procedure as a function, as long as your stored procedure doesn't already UPDATE/INSERT/DELETE any data.
Yes this can be done by making the temp table in the insert sp.
then call the sp that you want to call and store the value in the temp table and then you can use that temp table values in your sp and at last drop that temp table

How to use the return table data of one procedure in another procedure?

I had a procedure it will return data from a temporary table.The temporary table structure may varies depends on the input parameter of the procedure.This procedure is a general procedure so i cant modify this one.
My requirement is that i want to use the return temporary table data to do some calculation from another procedure.Is it possible to achieve this one????
requirement is some thing like this
Create Procedure Proc2 ( #Condition int)
As
BEGIN
execute #temp = Proc1 input1,input2
select * from #temp where column1 = #Condition
END
This Proc1 is using in some other procedures also so i cant include the condition inside Proc1.
Am using SQL SERVER 2008 R2
Thejus T V
You need to create a temporary table and use like this
Create Procedure Proc2 ( #Condition int) As
BEGIN
create table #temp(col1 int, col2,....) -- similar to reseultset of proc1
insert into #temp
execute Proc1 input1,input2
select * from #temp where column1 = #Condition
END
If the column names are unknown you can use OPENROWSET
Select
*
from
OPENROWSET('SQLOLEDB','Data Source=Server_name;Trusted_Connection=yes;
Integrated Security=SSPI','Execute yourdb..proc1')
If you know the structure being returned by the procedure then you can simply create a temp table and insert the data into that temp table from the stored procedure result set.
Create Table #Temp (Col INT , Col2 INT)
INSERT INTO #Temp
Exec Proc1 input1,input2
But since you have mentioned you don't know the exact structure of the table begin returned from the stored procedure you can use OPENQUERY to manipulate further the result set of a stored procedure something like this....
SELECT *
FROM OPENQUERY(YOURSERVERNAME, 'Proc1 input1,input2')
where column1 = #Condition

To insert the result of sp_executesql into a temp table

I am using a dynamic query in SQL which returns me a large data set which I need to insert into a temporary table.
Does
SELECT INTO #tmp from EXEC sp_executesql #query work?
I did try it out but I get an error 'Incorrect syntax near the keyword 'EXEC'
I know that it works using Insert into, but I don't want to create the table manually as it is a large table with a lot of columns. Is there an alternative without having to creating the temp table manually?
No it does not workb you cannot do this like that. However the work around is this, before the dynamic SQL create a temp table like normal, and in the dynamic SQL insert into that table. Then when you are done you will have the table filled.
IF OBJECT_ID('tempdb..##TmepTable') IS NOT NULL DROP TABLE ##TmepTable
CREATE TABLE ##TmepTable (TmpCol CHAR(1))
DECLARE #SQL NVARCHAR(max) =' IF OBJECT_ID(''tempdb..##TmepTable'') IS NOT NULL DROP TABLE ##TmepTable
SELECT * INTO ##TmepTable from [MyTableName]'
EXEC sp_executesql #SQL
SELECT Alias.* FROM ##TmepTable as Alias
IF OBJECT_ID('tempdb..##TmepTable') IS NOT NULL DROP TABLE ##TmepTable