Re: SPs which return one value
From: Sean Nolan (seann_at_imgno%spaminc.com)
Date: 03/18/05
- Next message: Robbe Morris [C# MVP]: "Re: SPs which return one value"
- Previous message: Flip: "Re: question about proper production db/ado usage"
- In reply to: Mark Rae: "SPs which return one value"
- Next in thread: Mark Rae: "Re: SPs which return one value"
- Reply: Mark Rae: "Re: SPs which return one value"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 18 Mar 2005 15:57:48 -0600
Hi Mark,
There's a comparison here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/bdadotnetarch031.asp
That article goes over much more than just the single value cases you asked
about, but the following paragraph sums it up:
"performance of the ExecuteScalar, Output Parameter, and DataReader
approaches is very similar across the entire range of user load when a
single value is being retrieved.
The ExecuteScalar approach requires less code than the other approaches and
may therefore be the best option from a code maintainability perspective."
I'd agree with that based on personal experience.
Please note three important things:
1. ExecuteScalar always returns the single value that is returned in the
first row and first column of the SQL query, so it is up to you to ensure
that you don't write inefficient code that actually queries lots of rows and
columns - ExecuteScalar will not generate an exception it will just ignore
everything execept the value from the first column and row.
2. You cannot use the integer return value from ExecuteNonQuery because that
is the count of the records affected by your query, it is not a value
returned from your query or sp.
3. You could use the return value from the sp (the value returned by the
RETURN statement in the sp. To do that you need to create a parameter for
your command named @RETURN_VALUE, with a ParameterDirection of
ParameterDirection.ReturnValue. This is an automatic parameter that is
always there. I don't typically use it for returning application values
because it is limited to integer values, but you can use it pretty much like
a regular output parameter, except that you don't have to declare it.
Here's more recommended info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingstoredprocedureswithcommand.asp
Sean
"Mark Rae" <mark@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:ei1YXT%23KFHA.4092@tk2msftngp13.phx.gbl...
> Hi,
>
> Looking for opinions / advice / tips etc on SQL Server (but I guess this
> would apply to just about any RDBMS) stored procedures which, for whatever
> reason, return only one value. E.g.
>
> 1. INSERTS which return @@IDENTITY
>
> 2. UPDATES which return the number of rows affected by the SP
>
> 3. SELECTS which, say, return a COUNT(*) of rows which satisfy certain
> criteria to prevent duplicate records etc
>
> 4. multi-statement SPs involving transactions which return a 1 or 0 at the
> end to indicate that the transaction was successful and has been
> committed, or was unsuccessful and has been rolled back
>
> In all of the above examples, the SPs could simply return a recordset
> which could be read into an SqlDataReader object, apply the .Read()
> method, and interrogate dr[0].ToString() or whatever. In this case, would
> ExecuteScalar be more efficient than ExecuteReader, even though there will
> only be one row and one column?
>
> Or, would it be more efficient not to return a recordset at all and,
> instead, have the SPs return an int output parameter?
>
> In the case of 2 above, am I correct in thinking that the most efficient
> method would be to interrogate the integer return value of the
> ExecuteNonQuery() method of the Command object?
>
> Any advice gratefully received.
>
> Mark Rae
>
- Next message: Robbe Morris [C# MVP]: "Re: SPs which return one value"
- Previous message: Flip: "Re: question about proper production db/ado usage"
- In reply to: Mark Rae: "SPs which return one value"
- Next in thread: Mark Rae: "Re: SPs which return one value"
- Reply: Mark Rae: "Re: SPs which return one value"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|