Is there a proper way to get the auto number Id of a recorded that was just inserted? The only way I know is to select the max. What is the preferred way to get it?
Where are you creating the record? VB, ASP or some other programming language? If so, then you cannot use @@Identity unless you're calling a stored proc.
If you're using a .AddNew function of a recordset in VB/ASP, then you're stuck with the doing a Max() after doing a .Update to the recordset.
If you can use a stored proc from VB/ASP to create the new record, then the proc can return the new Id value as described above as an output parameter to the proc.
Hi there,
Adding on to <balves> comments, it's usually considered good practice to be doing the Insert from inside a stored procedure. In that situation, the SP is not ordinarily returning a recordset to you (i.e. since you are Inserting, not Selecting), and so most times you would have the SP return the @@Identity value as an Output parameter instead of a recordset.
When returning just a single value like an Identity number, an output parameter will be more efficient (generate less network traffic) than a recordset.
I think this is an example of returning the identity as a recordset. i.e. a recordset with one row and one column. As long as the calling client expects to see a recordset back, i imagine this would work.
------------------------------------
However, I think returning an identity number is more commonly done with an output parameter in the SP. If you wanted to set this up, your client has to get set to look for and see the output parameter when it is passed back from the SP. And within the SP, you would make these changes:
(1) Add something like this to your 'Variables Declaration' section:
@parmRowcount int = 0 output
(the output keyword alerts the SP that this parameter must be passed back)
(2) Change SELECT @@IDENTITY AS 'Identity' to
select @parmIdentity = Scope_Identity()
Hopefully, that should be it, if I haven't screwed it up. This is all only if you wanted to use the output parameter approach.
-----------------------------------------
I think this is an example of returning the identity as a recordset. i.e. a recordset with one row and one column. As long as the calling client expects to see a recordset back, i imagine this would work.
------------------------------------
However, I think returning an identity number is more commonly done with an output parameter in the SP. If you wanted to set this up, your client has to get set to look for and see the output parameter when it is passed back from the SP. And within the SP, you would make these changes:
(1) Add something like this to your 'Variables Declaration' section:
@parmIdentity int = 0 output
(the output keyword alerts the SP that this parameter must be passed back)
(2) Change SELECT @@IDENTITY AS 'Identity' to
select @parmIdentity = Scope_Identity()
Hopefully, that should be it, if I haven't screwed it up. This is all only if you wanted to use the output parameter approach.
-----------------------------------------
Sorry about that.
I apologize for those last two double posts.
Please ignore the first one, which contains a small error, and read the second one (if you wish).
bp
I guess if that is correct, I will start to work on calling it from VB. This may take even longer for me. By the way, thanks for all of the help that you have given me.
I believe that the Scope_Identity() function is new to SQL Server 2000. Earlier versions, use @@Identity.
Just from an VB/ASP efficiency aspect, if you're calling an stored proc to do an INSERT, UPDATE or DELETE, using an ADO Command object is more efficient than using an ADO recordset.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.