Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL server stored procedure return a table

I have a stored procedure that takes in two parameters. I can execute it successfully in Server Management Studio. It shows me the results which are as I expect. However it also returns a Return Value.

It has added this line,

 SELECT 'Return Value' = @return_value 

I would like the stored procedure to return the table it shows me in the results not the return value as I am calling this stored procedure from MATLAB and all it returns is true or false.

Do I need to specify in my stored procedure what it should return? If so how do I specify a table of 4 columns (varchar(10), float, float, float)?

like image 671
mHelpMe Avatar asked Apr 09 '14 13:04

mHelpMe


People also ask

Can we return table from stored procedure in SQL Server?

The RETURN exits the stored procedure, and nothing that follows it will be executed, including the SELECT statement on the following line. Otherwise, if you want the data for the entire table, as your question shows, add a SELECT after the INSERT . But don't put RETURN in front of it!

How do you get the output of a stored procedure into a table?

CREATE TABLE #StudentData_Log (ID INT, Name VARCHAR(100)) SELECT * FROM #StudentData_Log; Lets execute the stored procedure and insert output into above temp table. Lets check the temp table, and you can see the stored procedure output is inserted in table.

Can stored procedure return resultset?

In addition to returning output parameters, a stored procedure can return a result set (that is, a result table associated with a cursor opened in the stored procedure) to the application that issues the CALL statement.

Can we return from stored procedure?

You can use one or more RETURN statements in a stored procedure. The RETURN statement can be used anywhere after the declaration blocks within the SQL-procedure-body. To return multiple output values, parameters can be used instead. Parameter values must be set prior to the RETURN statement being executed.


1 Answers

A procedure can't return a table as such. However you can select from a table in a procedure and direct it into a table (or table variable) like this:

create procedure p_x as begin declare @t table(col1 varchar(10), col2 float, col3 float, col4 float) insert @t values('a', 1,1,1) insert @t values('b', 2,2,2)  select * from @t end go  declare @t table(col1 varchar(10), col2 float, col3 float, col4 float) insert @t exec p_x  select * from @t 
like image 185
t-clausen.dk Avatar answered Oct 06 '22 02:10

t-clausen.dk