Is there any way using a 'datareader' to find out the autonumber field
value of a record that I've just inserted?
Thanks,
-- cohenmarvin 10 1384
Well it depends on what database you are using. If you are using SQL Server
this should work:
SELECT @@Identity
--
T. DAVIS JR
- .NET will change the way developers develop applications
"COHENMARVIN" <co*********@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com... Is there any way using a 'datareader' to find out the autonumber field value of a record that I've just inserted? Thanks, -- cohenmarvin
COHENMARVIN wrote: Is there any way using a 'datareader' to find out the autonumber field value of a record that I've just inserted? Thanks, -- cohenmarvin
You're best bet is to avoid using autonumber in the first place.
Life is MUCH easier if you assign it yourself
--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com
"Curt_C [MVP]" <software_at_darkfalz.com> wrote in news:#OTlPBfnFHA.2156
@TK2MSFTNGP14.phx.gbl: COHENMARVIN wrote: Is there any way using a 'datareader' to find out the autonumber field value of a record that I've just inserted? Thanks, -- cohenmarvin
You're best bet is to avoid using autonumber in the first place. Life is MUCH easier if you assign it yourself
??? Then you run into the possiblity of generating a duplicate...
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/
"T. DAVIS JR" <so*****@hotmail.com> wrote in
news:ev**************@tk2msftngp13.phx.gbl: Well it depends on what database you are using. If you are using SQL Server this should work:
SELECT @@Identity
@@IDENTITY is potentially a BAD thing! SELECT SCOPE_IDENTITY() is a
better choice since @@identity could return the wrong value if there are
triggers on the table.
Example: http://www.trigonblue.com/sp_identity.htm
Here's a table explaining the difference:
SELECT @@IDENTITY <- Session Scope
SELECT IDENT_CURRENT('tablename') <- Table scope
SELECT SCOPE_IDENTITY <- statement scope
In detail: http://www.sqlteam.com/item.asp?ItemID=319
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/
Well I agree, but for 95% of us that don't use triggers it shouldn't really
matter. However, selecting scope_identity() wouldn't hurt I guess.
--
T. DAVIS JR
- .NET will change the way developers develop applications
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn**************************@127.0.0.1... "T. DAVIS JR" <so*****@hotmail.com> wrote in news:ev**************@tk2msftngp13.phx.gbl:
Well it depends on what database you are using. If you are using SQL Server this should work:
SELECT @@Identity
@@IDENTITY is potentially a BAD thing! SELECT SCOPE_IDENTITY() is a better choice since @@identity could return the wrong value if there are triggers on the table.
Example:
http://www.trigonblue.com/sp_identity.htm
Here's a table explaining the difference:
SELECT @@IDENTITY <- Session Scope SELECT IDENT_CURRENT('tablename') <- Table scope SELECT SCOPE_IDENTITY <- statement scope
In detail:
http://www.sqlteam.com/item.asp?ItemID=319
-- Lucas Tam (RE********@rogers.com) Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/
"T. DAVIS JR" <so*****@hotmail.com> wrote in
news:O1**************@TK2MSFTNGP10.phx.gbl: Well I agree, but for 95% of us that don't use triggers it shouldn't really matter. However, selecting scope_identity() wouldn't hurt I guess.
It's not only triggers... if you do two inserts in a row, it could return
the wrong value ; )
And yes, it's better to be safe than sorry! I found out the hardway
wondering why my identity fields were all wrong ; )
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/
Lucas Tam wrote: "Curt_C [MVP]" <software_at_darkfalz.com> wrote in news:#OTlPBfnFHA.2156 @TK2MSFTNGP14.phx.gbl:
COHENMARVIN wrote:
Is there any way using a 'datareader' to find out the autonumber field value of a record that I've just inserted? Thanks, -- cohenmarvin
You're best bet is to avoid using autonumber in the first place. Life is MUCH easier if you assign it yourself
??? Then you run into the possiblity of generating a duplicate...
true... but if you put in a proper id generator you wont have this
issue. Very few "professional" apps use the auto generator, they ID it
themselves.
--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com
"Curt_C [MVP]" <software_at_darkfalz.com> wrote in news:#3kzrEinFHA.2484
@TK2MSFTNGP15.phx.gbl: ??? Then you run into the possiblity of generating a duplicate... true... but if you put in a proper id generator you wont have this issue. Very few "professional" apps use the auto generator, they ID it themselves.
Of course there are situations where this maybe needed - but for the
average application how much do you gain from rolling your own generator?
Building your own id generator means you'll need to maintain state, insure
that IDs are unique, concurrency issues - too much work especially when any
decent database already does it for you.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/
Prevention is better that cure..
But at the same time if your not inserting 2 rows
then the latter should be fine
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@127.0.0.1... "T. DAVIS JR" <so*****@hotmail.com> wrote in news:O1**************@TK2MSFTNGP10.phx.gbl:
Well I agree, but for 95% of us that don't use triggers it shouldn't really matter. However, selecting scope_identity() wouldn't hurt I guess.
It's not only triggers... if you do two inserts in a row, it could return the wrong value ; )
And yes, it's better to be safe than sorry! I found out the hardway wondering why my identity fields were all wrong ; )
-- Lucas Tam (RE********@rogers.com) Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/
Avoiding the autogenerator issue, I would simply create a Stored Procedure
that selects the records and returns the ID (however generated) as an output
parameter.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn**************************@127.0.0.1... "Curt_C [MVP]" <software_at_darkfalz.com> wrote in news:#3kzrEinFHA.2484 @TK2MSFTNGP15.phx.gbl:
??? Then you run into the possiblity of generating a duplicate... true... but if you put in a proper id generator you wont have this issue. Very few "professional" apps use the auto generator, they ID it themselves.
Of course there are situations where this maybe needed - but for the average application how much do you gain from rolling your own generator? Building your own id generator means you'll need to maintain state, insure that IDs are unique, concurrency issues - too much work especially when any decent database already does it for you.
-- Lucas Tam (RE********@rogers.com) Please delete "REMOVE" from the e-mail address when replying. http://members.ebay.com/aboutme/coolspot18/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ilan Sebba |
last post by:
I have a 'supertype' table with only one field: autonumber. Call this table
the 'parent' table. There are two subtypes, 'androids' and 'martians'.
Martian have only one thing in common: they give...
|
by: Steve Jorgensen |
last post by:
Hi all,
I've migrated an Access database schema to PostgreSQL, and I'm trying to get
everything working again. I'm having the following problem...
Within an Access transaction, I insert a row...
|
by: bigbinc |
last post by:
I am in autonumber hell, it is important for me to get the AutoNumber
even in case of previous deleted records, and I cant get the value
through a 'test' insert and then delete method. There is no...
|
by: ben.werdmuller |
last post by:
Hi,
Is there an easy way in ASP/VBscript to grab an autonumber (primary
key) field just after an SQL insert? This is probably easy, but I'm
stuck ..
Cheers.
|
by: loreille |
last post by:
To insert a record in a Ms Access database and be able to retrieve the
newly
created ID (autonumber) I used the code below (code 1).
Now, the problem is that this is not very secure and that, if...
|
by: petebeatty |
last post by:
I have created a SQL string the properly inserts a record in the table.
However, the insert does not occur at the end of the table. Instead
it inserts a record after the last record that I viewed....
|
by: jason.teen |
last post by:
Hi,
Curerntly I have designed my table in MS Access with a column of
"RuleID"
and is set to (autonumber)
tblRule:
RuleID | Name
-------------------------------
1 ...
|
by: magmike |
last post by:
Accidentally deleted a record. Anyway to get it back?
If not, I know the ID number - which is an autonumber field. Because
of the related data from other tables, would I be able to create a new...
|
by: ashes |
last post by:
Hi,
I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net
When someone wants to register on the website, they fill out a form and the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |