Return Table from Store Procedure - SQL Server [duplicate] - sql

This question already has answers here:
SQL server stored procedure return a table
(9 answers)
Closed 4 years ago.
I am trying to get table data which is being returned from Stored procedure.
Create procedure Proc1
as
begin
Select * from Employee
End
Go
I want to use this as :
Select * from Departments D
inner join (Exec proc1) p
on D.Emp_id = p.Emp_id
Please suggest.
Thanks

Short version: you can't. Stored procedures cannot be used as a source of data in a query.
The best you can do is put the results of the stored procedure into a (temporary) table and then query that:
create table #sprocResult (
-- define columns that match the results of the sproc.
-- You should also define a PK, and possibly other indexes
)
insert into #sprocResult exec proc1
(You can use a table valued variable as well.)

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

How do I get the stored procedure result set into temp table without creating temp Table [duplicate]

This question already has answers here:
Insert results of a stored procedure into a temporary table
(33 answers)
Closed 8 years ago.
How do I get the stored procedure result set into temp table without creating temp Table.
Also I did not use Openrowset command.Please help.I want like this
insert * into #tab
exec Tet1.dbo.Proc_GeSpecific #dataToPass
Here is the syntax to use in order to insert result of a stored procedure into a temp table:
INSERT #table(column1, column2,...)
EXEC Tet1.dbo.Proc_GeSpecific #dataToPass
Hope this will help you.

select top 0 * INTO #temp from stored procedure

select top 0 * INTO #temp from stored procedure
Need to create temp table based on the structure of data type returned from stored procedure.
Using sql server 2000,2005, 0r 2008
You can't do this. To get the results from a stored procedure, you have to first define the structure of the results:
create table #temp ( . . . );
insert into #temp
exec(stored procedure)
If you examine the syntax for the SELECT statement (here), you'll see no reference to running a stored procedure.
Perhaps you should post another question describing what you are trying to do. Why would a stored procedure be returning different result formats?