Do stored procedures need output variables?

I'm just learning about stored procedures. If I don't specify output variables, I can still get all of the data. (i.e. in a Gridview control). But do I really need output variables? Can I still access the individual column data without output variables?

Example below (includes one output variable for row count only):

CREATE PROCEDURE [dbo].[spMyStoredProcedure]
@RowCount int output

AS
BEGIN
	SELECT * FROM dbTable
	select @RowCount=@@ROWCOUNT
END

Open in new window

intoxicated_curveballAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
No.  They can use output parameters, but in most cases they don't and will return a set of data such as your SELECT * FROM dbTable.
0
intoxicated_curveballAuthor Commented:
I meant to say output parameter (not variable). If I don't set a parameter, how can I access a column (for example in VB.NET C#)?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
You will only need the output parameter if you want the stored procedure to return more than one value.
The main value is returned by the SP itself:
SET @MyVariable = sp_spname()

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

intoxicated_curveballAuthor Commented:
Vitor: In the example I posted above I'm retrieving the enter table and all columns and rows (in addition to the RowCount parameter). So that is not just "one value", can you clarify your response? However I'm not sure I can access the individual columns with this approach. Here is my ASP.NET code:

SqlConnection MyConnection = new SqlConnection("server=myserver;database=mydatabase;Integrated Security=SSPI;pooling=true");
SqlDataAdapter MyDataAdapter = new SqlDataAdapter("spMyStoredProcedure", MyConnection);
 MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@RowCount", SqlDbType.Int, 4));
MyDataAdapter.SelectCommand.Parameters["@RowCount"].Direction = ParameterDirection.Output;

DataSet DS = new DataSet();
MyDataAdapter.Fill(DS, "spMyStoredProcedure");
lblRows.Text = MyDataAdapter.SelectCommand.Parameters[0].Value + " Rows Found!";

GrdData.DataSource = DS.Tables["spMyStoredProcedure"].DefaultView;
GrdData.DataBind();

MyDataAdapter.Dispose(); 
MyConnection.Close(); 

Open in new window


Output
0
intoxicated_curveballAuthor Commented:
I feel like the following has answered my question as it is returning a 'result set', however I will await for additional comment for answering my own question.

https://docs.microsoft.com/en-us/sql/relational-databases/stored-procedures/return-data-from-a-stored-procedure?view=sql-server-2017
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
I'm retrieving the enter table and all columns and rows (in addition to the RowCount parameter)
The SP just return the table with the row and columns (that's one object). The row count is an output parameter (that's another object).

CREATE PROCEDURE [dbo].[spMyStoredProcedure]
@RowCount int output --> The OUTPUT parameter

AS
BEGIN
      SELECT * FROM dbTable --> The Table that is returned with all rows and columns
      select @RowCount=@@ROWCOUNT --> Store the row count into the Output parameter
END
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.