Save result of stored procedure in a Table variable [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to SELECT * INTO [temp table] FROM [stored procedure]
I have a nested stored procedure call
In one of the stored procedures I want to save the result into a table variable likt this :
INSERT INTO #myTable
EXEC sp_myStoredProcedure
however, because the proc. is nested the following error occurs :
An INSERT EXEC statement cannot be nested
The procedure must be called from another procedure, changing this is not an option.
I wanted to try to use an output parameter but it still has to be set with a Insert into statement.
What are other options to save the data that is retrieved from the call of a Stored Procedure into a variable ?

Table variables are not visible to the calling procedure in the case of nested procs. The following is legal with #temp tables.
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html
http://support.microsoft.com/kb/305977/en-us

Not tested but maybe do this:
DECLARE #insertedProc as varchar(300)
SET #insertedProc = 'INSERT INTO ' + #myTable + ' sp_myStoredProcedure'
EXEC #insertedProc
Make sure you have defined what #myTable is before hand.

Related

Using variable to create temporary table as another table [duplicate]

This question already has answers here:
T-SQL Dynamic SQL and Temp Tables
(5 answers)
A table name as a variable
(10 answers)
Closed 8 months ago.
I'm building a procedure into SQL Server, and for that I need to receive a table name as a variable, create a temporary table based on the table requested, search some substrings on each column of the given table, remove then, and return the result to the user.
For that, I'm first tried to do that:
declare #TableName varchar(max)
select #TableName = 'xxxxxx'
select * into #temp from #TableName
select * from #temp
From this code, I've got the error:
Must declare the table variable "#TableName"
To use a table variable, I need to know the structure of said table beforehand, which I don't.
If I build the statement and execute it inside an EXEC() function, then the scope changes, and the temporary table will not be available for the outer process.
Is there a way to use the variable directly, or to declare this table variable dynamically?

Run SQL Dynamic using column value [duplicate]

This question already has answers here:
T-SQL: How to use parameters in dynamic SQL?
(4 answers)
sql stored procedure argument as parameter for dynamic query
(2 answers)
SQL Server 2017 - How to pass a parameter in a SELECT inside a dynamic SQL
(1 answer)
Closed last year.
I am using dynamic SQL.
I inserted a select statement into one row from #example, I want to call that select statement and run it with dynamic SQL, my select stored has a variable call #NumOrder
create table #example(id int, description varchar(1000))
insert into #example(id, description)
values(1, 'select case when name=''Carlos'' then ''Pass'' else ''FAIL'' from server.dbo.person where number = #NumOrder')
declare #NumOrder int, #SQL nvarchar(max)
set #NumOrder=2621
set #SQL='description'
--here is where I want to call my query and is failing
--once I filled #NumOrder I want to display the results
exec(#SQL)
I want to run this dynamic SQL using a row stored into the temp table, I know that I can put the complete select statement into the #SQL and works but I want to know if there is a way to call the select statement in a row stored from a table. Is there a way to do this?

return 60 column table from one stored procedure into another [duplicate]

This question already has answers here:
How do I use the results of a stored procedure from within another?
(4 answers)
Closed 3 years ago.
ALTER PROCEDURE [dbo].[outer_sp]
#PrmIdUsAD nchar(128)
,#PrmAnnee smallint
,#PrmMois tinyint
,#PrmIdAct tinyint
AS
BEGIN
DECLARE #DateMin datetime, #DateMax datetime, #idAgence nchar(2), #nomAgence nchar(50);
EXEC inner_sp( #DateMin, #DateMax, #idAgence, #nomAgence)
...
END
inner_sp ends up by (table has 60 columns)
SELECT
ISNULL(nomAgence, '') AS nomAgence,
ISNULL(chemin, '') AS chemin,
...
FROM #temp_arbo_of_4
ORDER BY chemin
END
How do I retrieve this output table in the calling stored procedure ?
I know some variables are not set, but this is outside the scope of question.
I assume you want to do further processing on the results? If not, the output will just be returned anyway.
If you do want to process further, create a temp table with the right columns
CREATE TABLE #temp
(
nomAgence {type},
chemin {type},
...
)
then use select into
SELECT INTO #temp
EXEC SelectOFArbo( #DateMin, #DateMax, #idAgence, #nomAgence);
Note that SQL Server wont let you nest these calls, so SelectOfArbo could not SELECT-INSERT from another proc

Insert into table from stored proc

I am trying to use a stored procedure that contains two different cursors as table input like such:
INSERT INTO table1 EXEC * FROM tblDailySales
The stored proc contains two cursors - I did not run just using.
I get the following error:
A cursor with the name 'csrDistricts' does not exist.
I also, get this error
An INSERT EXEC statement cannot be nested
The stored proc contains no EXEC that I can see.
What kind of stored proc other than simple SELECT can be used as source for table?
Is table1 already defined? If so all you should have to do is
INSERT INTO table1
EXEC storedProcedureName
Now, the trick is, the stored procedure will only be able to return one result set and insert into the table.
If you need to insert two different result sets, you'll have to gather then in two different stored procedures, then run two INSERT statements.
If you must do them at once, you'll need to do the insert from within the stored procedure.

Why can't use INSERT EXEC statement within a stored procedure called by another stored procedure?

First I try to explain the circumstances.
I store the the filter expression in one column separated by line breaks. The base idea was this:
SELECT
'SELECT ''' + REPLACE(topic_filter,CHAR(10),''' UNION ALL SELECT ''') + ''''
FROM dbo.topic_filter T
WHERE
T.id = #id
FOR XML PATH('')
After this I simply execute this string to put the datas into a temp table.
My problem starts here.
The snippet is in a stored procedure and used by multiple stored procedures to generate the base source to fill.
Approach 1:
Call this sp from another SP to fill a temp table.
Result 1:
An INSERT EXEC statement cannot be nested.
(If I call simply with exec dbo... style the code is working. I only get the error if I try to call within a stored procedure)
Approach 2:
I put the code above into a table values function.
Result 2:
Invalid use of a side-effecting operator 'INSERT EXEC' within a function.
(The function itself don't compiled)
Thanks,
Péter
In the meantime I managed to solve the problem (with help :) ). The solution is simple:
exec('insert into t2 ' + #str)
Where #str contains a select statement.
I don't know why but this way there is no error. The method I call the stored procedure:
SET #exec = 'exec dbo.trFilterTopic ''' + #id+ ''',null,null,1'
INSERT INTO #filtered
exec (#exec)
I hope I spare some time to other folks with this solution.
Bye,
Péter
It is an SQL Server restriction. You cannot have a nested insert exec (I'm not sure why).
If you go:
insert into t(value)
exec dbo.proc
, and inside dbo.proc you have
insert into t2(value2)
exec(#str)
, then it will not run.
Consider different ways of passing tables around, such as temporary tables or table-valued parameters.
Functions on SQL Server have limitations,
they arenot procedures, you can't use dynamic SQL like 'EXECUTE STRING', 'INSERT EXEC'...