Advertisement

06.23.2005 at 03:54PM PDT, ID: 21469141
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Get last inserted record

Tags: last, get, inserted, record, id
Hi I need to retrive the id (primary key and auto increment) of the last inserted rowso that i can ue that reference thru the is and i know we could do it using max function. But its not a reliable way when u consider multi user applications.

Does anybody kno anything more relaible. How about UUID. Can you tell me the code how to use it?

Thanks
AB
Start your free trial to view this solution
Question Stats
Zone: Software
Question Asked By: anup001
Solution Provided By: pinaldave
Participating Experts: 6
Solution Grade: A
Views: 306
Translate:
Loading Advertisement...
06.23.2005 at 04:03PM PDT, ID: 14289831

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.23.2005 at 04:07PM PDT, ID: 14289846

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.23.2005 at 04:28PM PDT, ID: 14289920

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.23.2005 at 04:29PM PDT, ID: 14289925

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.23.2005 at 04:35PM PDT, ID: 14289953

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.23.2005 at 05:02PM PDT, ID: 14290064

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.23.2005 at 05:08PM PDT, ID: 14290080

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.23.2005 at 05:20PM PDT, ID: 14290125

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.23.2005 at 08:56PM PDT, ID: 14291051

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.23.2005 at 08:59PM PDT, ID: 14291059

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2005 at 08:21AM PDT, ID: 14294542

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2005 at 09:23AM PDT, ID: 14295169

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2005 at 09:27AM PDT, ID: 14295210

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2005 at 02:01PM PDT, ID: 14297267

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2005 at 02:44PM PDT, ID: 14297516

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.24.2005 at 02:46PM PDT, ID: 14297528

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
06.23.2005 at 04:03PM PDT, ID: 14289831

Rank: Sage

hi, you can always use lock around your insert and selection like this and it will give you last isserted record for sure.

<cflock name="insertrecord" timeout="10">
<cfquery datasource="mydsn">
INSERT INTO mytable (yourfields) VALUES (#yourvalues#)
</cfquery>
<cfquery datasource="mydsn" name="getnewID">
SELECT MAX(RecordID) AS NewID FROM mytable
</cfquery>
</cflock>

This way that will always select the last inserted value.
Regards,
---Pinal
Accepted Solution
 
06.23.2005 at 04:07PM PDT, ID: 14289846
If you are using SQL Server, you can also use the @@IDENTITY function in SQL Server and wrap the statement in the same query as the insert statement.  @@IDENTITY returns the very last ID that was inserted into the database,regardless of table.

For example:
<CFQUERY NAME="myQuery" DATASOURCE="myDataSource">
     INSERT INTO Table
          (Field1,
          Field2,
          Field3)
     VALUES
          ('Field1',
          'Field2',
          'Field3')

     SELECT @@IDENTITY AS RecordID
</CFQUERY>

<CFDUMP VAR="#myQuery.RecordID#">

This will insert the record and return its ID as a single process.
 
06.23.2005 at 04:28PM PDT, ID: 14289920

Rank: Master

Enclose both your main CFQUERY and SELECT MAX() CFQUERY in CFTRANSACTION block.  Should work for most databases.

If you under MS SQL, @@IDENTITY is a better choice than MAX() - faster.

CFLOCK will work also, but, will be required everywhere, when you access the table, even for SELECTs.
 
06.23.2005 at 04:29PM PDT, ID: 14289925

Rank: Genius

anup001 - this is a duplicate of this question:

http:Q_21467699.html

you just worded it slightly differently.

The maxium points you can offer for a question is 500 - which you already offered for the other question.

See here:
http://www.experts-exchange.com/help.jsp#hi4

We can either delete this question or have it changed to 20pts as a pointer question to the other question.

Please let us know how you want this question handled.

mrichmon
Page Editor
 
06.23.2005 at 04:35PM PDT, ID: 14289953

Rank: Wizard

<CFQUERY NAME="myQuery" DATASOURCE="myDataSource">
     INSERT INTO Table
          (Field1,
          Field2,
          Field3)
     VALUES
          ('Field1',
          'Field2',
          'Field3')

     SELECT SCOPE_IDENTITY() + 1 AS lastIdPlusOne
</CFQUERY>
 
06.23.2005 at 05:02PM PDT, ID: 14290064

Rank: Sage

Hi Anup,
Your similar question (http://www.experts-exchange.com/Web/WebDevSoftware/ColdFusion/Q_21467699.html) is already answered.
Please make a request in Community Support ( http://www.experts-exchange.com/Community_Support/ ) with a link to this question to delete and refund your points.
Regards,
pinaldave
{ColdFusion Page Editor}
 
06.23.2005 at 05:08PM PDT, ID: 14290080

Rank: Guru

on a high volume site you can have problems with a race condition.  In theory you can have 2 records inserted at almost the same instant.  cftransaction can cover this in most instances.

We use a different method because such race conditions have happened in all the scenarios we have tried above.

On the insert we have a UID we create with CreateUUID() and insert that into the record. Within the cftransaction we then do a select from where uid = the UId we just created.

<cfset thisUID = CreateUUID() />

<cftransaction>
 <CFQUERY NAME="myQuery" DATASOURCE="myDataSource">
     INSERT INTO Table
          (Field1,
          Field2,
         uniqueID)
     VALUES
          ('Field1',
          'Field2',
          #thisUID#)
</cfquery>
 <CFQUERY NAME="getId" DATASOURCE="myDataSource">
     SELECT id FROM Table
         WHERE uniqueID = #thisUID#
</cfquery>
</cftransaction>

In my view this is the only bullet proof way of accomplishing what you want on a scaleable basis.
 
06.23.2005 at 05:20PM PDT, ID: 14290125

Rank: Guru

actually @@identity should be fine if you are on SQL server 2000. SQL 7 doesnt support scope_identity().

http://weblogs.asp.net/rosherove/archive/2003/11/13/37217.aspx

I am also reliably informed that @@identity uses the SQL process id to retreive the last id passed in. So even if there were 2 'simultaneous' transactions you would end up with teh right number.
 
06.23.2005 at 08:56PM PDT, ID: 14291051
hi mrichmon,you can go ahead and delete the the duplication question, i thought somebody from the data base field would also give some vali answer rather than a programmer so thought of asking it in that section too aprt from coldfusion section. So anyways u can go ahead and delete it.

Thanks
an
 
06.23.2005 at 08:59PM PDT, ID: 14291059
Hey Pinal,
   Thanks for your answer but are u sure we would not have any problems about simultaneous transactions if we use cflock and retrive the max id for last inserted record, becoz mms98d1 suggested the use of UUID. But i guess if cflock works then i would want to use that. Also for your information the data would not be inserted by more than 8 peopls at a time.

So tell me what do u think

Thanks
Anup
 
06.24.2005 at 08:21AM PDT, ID: 14294542

Rank: Genius

anup001,

As you went ahead and marked the other question as answered and awarded the points there this is now the question that needs to be removed.

For now I have reduced the points to 20.

mrichmon
Page Editor
 
06.24.2005 at 09:23AM PDT, ID: 14295169
Ok thanks for letting me know richmon

Anup
 
06.24.2005 at 09:27AM PDT, ID: 14295210
Richmon, what is your suggestion on my above comment about simultaneous transactions with the use of MAX() and lock?

Thanks
Anup
 
06.24.2005 at 02:01PM PDT, ID: 14297267

Rank: Wizard

when its locked one transaction has to wait for the other, thats why you use locks...
 
06.24.2005 at 02:44PM PDT, ID: 14297516
So taco does it mean that it slows down the performance if there or more numebr of users?

This is a general question though coz i just have 6-7 simultaneous users in the worst case!

Anup
 
06.24.2005 at 02:46PM PDT, ID: 14297528

Rank: Genius

The general theory is YES.

Because each one that hits the lock while in use has to wait.

But then if they end up having to wait longer thatn the timeout attribute they will give a failed attempt mesage/error to the user.

SO it will usually error out before slowing down.

But with only a handful of users you shouldn't notice if you use locking sparingly and correclty.
 
 
20080236-EE-VQP-29