I want to Execute stored procedure into temporary table but it gives me an error You must specify a table where to make the selection - sql

This my code where i want to insert the result of my stored procedure into #TempTable
SELECT * INTO #Tempannuelguiftrecap
EXECUTE PSGetDetailMensuelPPM #Year,#Mois

You can't SELECT from a Stored Procedure. Something like SELECT * FROM EXECUTE dbo.MySP isn't going to work.
You can still INSERT the data from a Stored Procedure into a table, however, you must first define the table, and then INSERT the data. This is Pseudo-SQL as we have no definitions of your objects, however, this should get you on the right path:
CREATE TABLE #MyTempTable({Column1} {Data Type}[,
{Other Column(s)} {Column DataType(s)} ..... ]);
INSERT INTO #MyTempTable ({Columns List})
EXECUTE dbo.MyStoredProcedure #MyParam;

Related

How to keep temporary table after running stored procedure?

I know the temporary table will be deleted after the connection is lost. But within that connection, I want to do something like
EXEC test;
SELECT * FROM #Final;
#Final is the temporary table created in the stored procedure. The stored procedure needs 30 seconds, and I want to check my #final without running stored procedure again.
If I run the script in that stored procedure, the #final can be reused in the connection. But how to use it after the EXEC test?
So, except for creating a real table, is it possible to SELECT * FROM #Final after EXEC test? If no, I'll use real table instead. Thanks!
Then you don't want a temporary table. Use either a global temporary table (##final) or a real table.
Then delete the results after you run the procedure.
I should note that the stored procedure can return a result set which you can insert into a table, using exec().
Use a global temporary Table
create proc demo as
begin
select * into ##temptable from original_table
end
exec demo;
select * from ##temptable

Nested Stored Procedure

How can i insert values of a nested stored procedure into a table.
For example if i created a stored procedure like this.
Create procedure New
begin
create table #tmp
(
id int,
name varchar(50)
);
insert into #tmp
exec stored_procedure 1,2;
insert into #tmp
exec stored_procedure 1,2;
select * from #tmp
end
Now if I execute this command, SQL Server will display error:
insert into #table
exec New;
Does anyone have a solution for this problem? Please share
Right now you’re not returning any data from your procedure New. If you want it to return data for your calling code to insert you’ll need a select statement in it.
Eg add a line at the end:
SELECT * FROM #tmp;
Also, you can’t nest INSERT EXEC statements. See this answer for more details
You can't do a insert with de values returned by a Stored Procedure.
In your case the easiest way is to change de stored procedures by table functions.
You can find more about table functions here
My opinion is to make it simple if u can.

Inserting output parameters from stored procedure directly

I have a stored procedure that returns a pair of output parameters - the ID and the computed value. Is it possible to use a trigger with an insert statement, that inserts those two values directly? Something like this
CREATE TRIGGER Trig_FirstTable ON SecondTable AFTER UPDATE
AS
BEGIN
INSERT INTO FirstTable (OtherID, OtherValue)
VALUES (#otherID, #otherValue)
FROM StoredProcedure inserted.ID, inserted.Value, #otherID OUTPUT, #otherValue OUTPUT
END
According to the MSDN documentation you can use INSERT into with EXEC.
They give the following example:
--INSERT...EXECUTE procedure example
INSERT author_sales EXECUTE get_author_sales
But I think your stored procedure needs a SELECT statement to return the data instead of only filling the output parameters.
You can insert from SP like that:
drop procedure uspTest
GO
create procedure uspTest
AS
select 1 as id, 'x' as val
union all
select 2,'y'
GO
drop table #temp
GO
create table #temp (
id int,
val char(1)
)
GO
insert into #temp (id,val)
EXECUTE uspTest
GO
select
*
from #temp
But you cannot select a subset of columns, so this method will obviously fail if you add more outputs to your SP in the future:
insert into #temp (id)
EXECUTE uspTest
Another way is to store SP results in variables, and then use them for insert.

SQL Server Import and Export wizard gives invalid object error on stored procedure with temp table

I created a stored proc that creates a temp table, inserts, selects then drops. Executing the stored proc within SQL Server Management Studio works fine and gives the expected result.
CREATE PROCEDURE usp_TempTableTest
-- Add the parameters for the stored procedure here
#color VARCHAR(10)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
CREATE TABLE #tmptable (
color VARCHAR(10)
)
INSERT INTO #tmptable (color) VALUES (#color)
SELECT color FROM #tmptable
DROP TABLE #tmptable
END
GO
However, when creating in the Import/Export tool and using that stored proc as the data source, it gives me the error:
Invalid object name '#tmptable'.
Any idea why this would happen? If I change it to a table variable it seems work fine with Import/Export, but I don't understand why it is not working with a temp table.
When I run a mimicked stored procedure, like yours above, in SSMS, I can get the data returned like you mentioned in the procedure. However, if I try the #tmptable, like you did, I also get the same error because the DROP TABLE removes it. From what I can tell, the import/export is basically a final INSERT process. The reason it works with the table variable is because the data still exist on the final insert; in the case of the DROP TABLE, it does not. For instance, when I remove the DROP TABLE, it works.
I might be wrong here, but it seems the logic when it's an import or export in the case of the above procedure is
INSERT data
SELECT data
DROP data
INSERT (import/export): this generates the "Invalid object name tmptable'"
With the variable (or no DROP), it's
INSERT data
SELECT data
INSERT (import/export)
In the second case, the data still exist. In the first case, they're gone. One way around it if you want to use the #tmptable, start your code with:
IF OBJECT_ID('tempdb..#tmptable') IS NOT NULL DROP TABLE #tmptable
Put "SET FMTONLY OFF;" right above "SET NOCOUNT ON"
http://msdn.microsoft.com/en-us/library/ms173839.aspx

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.