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

	SELECT * FROM dbTable
	select @RowCount=@@ROWCOUNT

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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 Data DudeCommented:
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.
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#)?
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

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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;


Open in new window

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.
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

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

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

From novice to tech pro — start learning today.