472,378 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

Scope_Identity() - Output parameter or not?

Sql Server, Scope_Identity(), Ado.NET:

Which is better? Using an output parameter to return Scope_Identity through
ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the
procedure or ad hoc SQL and using ExecuteScalar()?

Thanks.
May 31 '07 #1
10 3951
The standard way is to run a batch
INSERT...;SELECT SCOPE_IDENTITY()...
in a single ExecuteScalar call.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Mike" <bl***@blank.blankwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
Sql Server, Scope_Identity(), Ado.NET:

Which is better? Using an output parameter to return Scope_Identity
through
ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the
procedure or ad hoc SQL and using ExecuteScalar()?

Thanks.


May 31 '07 #2
Great! That's what I do, but I've seen examples here and there using output
parameters and wondered why.

Thanks

Mike
"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote in
message news:e%****************@TK2MSFTNGP03.phx.gbl...
The standard way is to run a batch
INSERT...;SELECT SCOPE_IDENTITY()...
in a single ExecuteScalar call.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Mike" <bl***@blank.blankwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
>Sql Server, Scope_Identity(), Ado.NET:

Which is better? Using an output parameter to return Scope_Identity
through
>ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the
procedure or ad hoc SQL and using ExecuteScalar()?

Thanks.



May 31 '07 #3
Mike,
I believe using a output parameter is more efficient.
If you do the select scope_identity() technique you are returning an
entire row of data as a DataSet, DataTable, DataReader.. There is
still overhead associated with it (more network traffic, bigger object
to instantiate)

An output parameter is certainly better.

Vince

On May 31, 4:56 pm, "Mike" <b...@blank.blankwrote:
Great! That's what I do, but I've seen examples here and there using output
parameters and wondered why.

Thanks

Mike

"Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.orgwrote in
messagenews:e%****************@TK2MSFTNGP03.phx.gb l...
The standard way is to run a batch
INSERT...;SELECT SCOPE_IDENTITY()...
in a single ExecuteScalar call.
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Mike" <b...@blank.blankwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
Sql Server, Scope_Identity(), Ado.NET:
Which is better? Using an output parameter to return Scope_Identity
through
ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the
procedure or ad hoc SQL and using ExecuteScalar()?
Thanks.- Hide quoted text -

- Show quoted text -

Jun 1 '07 #4
??
Why do you think select scope_identity() returns more than just a single
scalar value?

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
<vi***********@gmail.comwrote in message
news:11*********************@p77g2000hsh.googlegro ups.com...
Mike,
I believe using a output parameter is more efficient.
If you do the select scope_identity() technique you are returning an
entire row of data as a DataSet, DataTable, DataReader.. There is
still overhead associated with it (more network traffic, bigger object
to instantiate)

An output parameter is certainly better.

Vince

On May 31, 4:56 pm, "Mike" <b...@blank.blankwrote:
Great! That's what I do, but I've seen examples here and there using
output
parameters and wondered why.

Thanks

Mike

"Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.orgwrote in
messagenews:e%****************@TK2MSFTNGP03.phx.gb l...
The standard way is to run a batch
INSERT...;SELECT SCOPE_IDENTITY()...
in a single ExecuteScalar call.
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
>http://msmvps.com/blogs/egoldin
"Mike" <b...@blank.blankwrote in message
>news:uX**************@TK2MSFTNGP06.phx.gbl...
>Sql Server, Scope_Identity(), Ado.NET:
>Which is better? Using an output parameter to return Scope_Identity
through
>ExecuteNonQuery(), or adding Select Scope_Identity() to the end of
the
>procedure or ad hoc SQL and using ExecuteScalar()?
>Thanks.- Hide quoted text -
- Show quoted text -


Jun 1 '07 #5
My understanding of ExecuteScalar is that it returns 1 single value. The
docs say nothing about returning this in a datareader/dataset. In fact, the
docs say that any values other than the first column of the first row are
discarded.

http://msdn2.microsoft.com/en-us/lib...utescalar.aspx

But thanks anyway.

Mike
<vi***********@gmail.comwrote in message
news:11*********************@p77g2000hsh.googlegro ups.com...
Mike,
I believe using a output parameter is more efficient.
If you do the select scope_identity() technique you are returning an
entire row of data as a DataSet, DataTable, DataReader.. There is
still overhead associated with it (more network traffic, bigger object
to instantiate)

An output parameter is certainly better.

Vince

On May 31, 4:56 pm, "Mike" <b...@blank.blankwrote:
>Great! That's what I do, but I've seen examples here and there using
output
parameters and wondered why.

Thanks

Mike

"Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.orgwrote in
messagenews:e%****************@TK2MSFTNGP03.phx.g bl...
The standard way is to run a batch
INSERT...;SELECT SCOPE_IDENTITY()...
in a single ExecuteScalar call.
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Mike" <b...@blank.blankwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
Sql Server, Scope_Identity(), Ado.NET:
>Which is better? Using an output parameter to return Scope_Identity
through
ExecuteNonQuery(), or adding Select Scope_Identity() to the end of
the
procedure or ad hoc SQL and using ExecuteScalar()?
>Thanks.- Hide quoted text -

- Show quoted text -


Jun 1 '07 #6
Eliyahu Goldin wrote:
??
Why do you think select scope_identity() returns more than just a single
scalar value?
Because you are executing a select. It creates a result in the form of
data rows. The result is a single row containing a single field, but
it's still a result in the form of data rows.

--
Göran Andersson
_____
http://www.guffa.com
Jun 1 '07 #7
Mike wrote:
My understanding of ExecuteScalar is that it returns 1 single value. The
docs say nothing about returning this in a datareader/dataset. In fact, the
docs say that any values other than the first column of the first row are
discarded.
Yes, the method returns a single value, but the database query returns a
complete result to return that single value.

--
Göran Andersson
_____
http://www.guffa.com
Jun 1 '07 #8

"Göran Andersson" <gu***@guffa.comwrote in message
news:el****************@TK2MSFTNGP05.phx.gbl...
Mike wrote:
>My understanding of ExecuteScalar is that it returns 1 single value. The
docs say nothing about returning this in a datareader/dataset. In fact,
the docs say that any values other than the first column of the first row
are discarded.

Yes, the method returns a single value, but the database query returns a
complete result to return that single value.
But it appears to be faster (and there more efficient?) to use ExecuteScalar

http://codebetter.com/blogs/john.pap.../10/61745.aspx

Mike
Jun 1 '07 #9
If you are using a stored procedure, you can return Scope_Identity() and it
will attach to the return parameter. Or, you can create an output parameter.

Using ExecuteScalar() gives you a firehose cursor with very few lines of
code, however. The speed of SELECT SCOPE_IDENTITY() is equivalent, and you
have fewer lines of code in the end than you do pulling a parameter.

In other words, do what you desire. You are not really going to see a major
difference either way you go.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
Co-author: Microsoft Expression Web Bible (upcoming)

************************************************
Think outside the box!
************************************************
"Mike" <bl***@blank.blankwrote in message
news:uX**************@TK2MSFTNGP06.phx.gbl...
Sql Server, Scope_Identity(), Ado.NET:

Which is better? Using an output parameter to return Scope_Identity
through ExecuteNonQuery(), or adding Select Scope_Identity() to the end
of the procedure or ad hoc SQL and using ExecuteScalar()?

Thanks.

Jun 1 '07 #10
Mike wrote:
"Göran Andersson" <gu***@guffa.comwrote in message
news:el****************@TK2MSFTNGP05.phx.gbl...
>Mike wrote:
>>My understanding of ExecuteScalar is that it returns 1 single value. The
docs say nothing about returning this in a datareader/dataset. In fact,
the docs say that any values other than the first column of the first row
are discarded.
Yes, the method returns a single value, but the database query returns a
complete result to return that single value.

But it appears to be faster (and there more efficient?) to use ExecuteScalar

http://codebetter.com/blogs/john.pap.../10/61745.aspx

Mike
With such a small difference as 3%, I would say that they seem to be
equally fast.

That test doesn't say anything at all about scalability or the use of
resources, though.

I don't have much substantial on this yet, but I would say that a
parameter is likely to use less resources than a data reader. A data
reader is built to handle data of very different sizes, and uses a
buffer of several kB for buffering the data, even if the data that you
actually get is only a few bytes.

--
Göran Andersson
_____
http://www.guffa.com
Jun 2 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: vivienne.netherwood | last post by:
I am developing an Access Project front end with a SQL server database. I have written a stored procedure that returns a record set and also a value via an output parameter. The procedure is as...
8
by: Christopher Weaver | last post by:
I'm having trouble accessing the value of an output parameter of a stored procedure. The SP looks like this: SET TERM ^ ; CREATE PROCEDURE SP_NEW_TASK RETURNS ( "uidTask" INTEGER) AS begin
8
by: Patreek | last post by:
Hi, On the line where I'm assigning RecordCount to be the value of my output parameter, I'm getting the generic "Object reference not set to an instance of an object" error. I've isolated it...
4
by: Mr Not So Know It All | last post by:
im new to SQL Server and ASP.Net. Here's my problem. I have this SQL Server stored procedure with an input parameter and output parameter CREATE PROCEDURE . @in_rc varchar(8) @out_eList...
0
by: rockdale | last post by:
Hi, All How to get the output parameter's value when you use the SQLHelper (Microsoft Data Access Block)? When I try to access my ourput parm I got the following error. ...
6
by: c676228 | last post by:
Hi everyone, I wrote a store procedure that fetch one row data in the database based on the parameter value I entered. After I created the store procedure, the store procedure code looks like...
8
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for...
7
by: ashtek | last post by:
Hi, I have a generic function that executes a stored procedure & returns a data table. Code: === public static DataTable ExecuteStoredProcedure(string strProc,SqlParameter paramArray) {...
1
by: John Bailo | last post by:
This is a my solution to getting an Output parameter from a SqlDataSource. I have seen a few scant articles but none of them take it all the way to a solution. Hopefully this will help some...
4
by: jpatchak | last post by:
OK, not sure if there is a way around this. I have some relatively complicated SQL code in a stored proc. The proc is working fine. When I execute this code in SSMS, it returns a record set and an...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.