Hi,
In my asp page I am inserting a record in to the sql server 2005 db as follows...
OpenDataConnect ion()
oConn.BeginTran s
set cm = CreateObject("A DODB.Command")
set cm.ActiveConnec tion = oConn
cm.CommandText ="INSERT INTO t_test( blah blah")
cm.execute
oConn.commitTra ns
set cm = nothing
i would now like to get the id of the record just inserted. i know about @@identity but records are being inserted into this table from multiple sources so i don't think i can gurantee this will be foolproof i.e. i insert a record, then another record is inserted from elsewhere immedialty after, i then run @@identity and it will give me the latter rather than the former...i think ?
Would greatly appreciate any ideas on how to do this. Thanks
2 12789 jeffstl 432
Recognized Expert Contributor
Hi,
In my asp page I am inserting a record in to the sql server 2005 db as follows...
OpenDataConnect ion()
oConn.BeginTran s
set cm = CreateObject("A DODB.Command")
set cm.ActiveConnec tion = oConn
cm.CommandText ="INSERT INTO t_test( blah blah")
cm.execute
oConn.commitTra ns
set cm = nothing
i would now like to get the id of the record just inserted. i know about @@identity but records are being inserted into this table from multiple sources so i don't think i can gurantee this will be foolproof i.e. i insert a record, then another record is inserted from elsewhere immedialty after, i then run @@identity and it will give me the latter rather than the former...i think ?
Would greatly appreciate any ideas on how to do this. Thanks
The @@identity should be pretty full proof actually. Execution of code happens quicker then you think. If you are getting the ID in execution immediately after the insertion you should always have the correct ID.
This also might even be dependant on an individual users database session, which further makes it solid.
Anyway, I could be wrong but I'm pretty sure @@identity is your best option. Anything else would be even less reliable.
One note of mention however, if you need to get the latest record only and not necessarily the last one inserted by an active user, you can use SQL MAX on the ID column to get the highest valued ID (therefore the latest record at the time of the query) -
SELECT MAX(ClientID) FROM table
-
Delerna 1,134
Recognized Expert Top Contributor
You could put the insert query into a stored proc
and have it return @@identity to your page.
That way subsequent calls to the stored proc by other users would have to wait their turn.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Matt |
last post by:
how to get the last record from database without looping?
Whenever the user need to insert a new record to the database, it just
increment the id field by one from the last record.
I tried objRS.MoveLast, but it wont work.
|
by: Carlos San Miguel |
last post by:
Hi, I'm going to be using Oracle with a customer. I will access
Oracle with ODBC. All the tables have an Identity column, this is the
ID for the record. I need to know how I can get the value of the
Identity column for the last record inserted. A stored procedure maybe?
TIA
Carlos San Miguel
|
by: Alain Guichaoua |
last post by:
Good evening to all
Here is my problem :
I have a form with a subform. They are linked.
When I open the form I would like the subform to reach its last
record.
I tried the method docmd.gotorecord aclast but i did not
|
by: Mark |
last post by:
I'm using ASP.Net to accress a database, what I need to do is get the fields
out of the very last record in the db.
How do I do this?
Actually I'm after the primary key, titled 'AdID' it'll tell me what number
this last record is. so I can tell what number the next record inserted will
be.
How do I do this?
Thanks,
|
by: MaRCeLO PeReiRA |
last post by:
Hi guys,
I am in troubles with a SERIAL field.
I have five tables. A parent table and four child
tables. When I do the INSERT in the parent table, I
have an ID (generated) by the sequence (SERIAL field),
and I have to use this ID to reference all child
tables.
| |
by: michel.ank |
last post by:
Hi,
I'm using the class PrintLines and my last record of page aren't with
the borders.
Somebody can help me?
Thanks,
|
by: Jim in Arizona |
last post by:
I forgot what the SQL command was to return the key of the last inserted
record. Does anyone happen to know what it is?
Thanks,
Jim
--
|
by: psyvanz |
last post by:
here is the code im working with...
hope you can understand? cause this thing searches the first one, not the last one record...
i wanted ONLY to find the last record in a table...
Please help me what was wrong with my code..
================================================================================
|
by: Van Fitz |
last post by:
I am importing an excel worksheet into an existing access 2000 table using TransferSpreadsheet. Im using a timestamp field as well in this table. This is going well, however, once the data is imported I need to find the last record imported and pull the value of a field called Code. Then add that value to another table where Code is the Key Value. This is all one continuous operation in the same sub and executing inline directly after
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |