Current location: Hot Scripts Forums » Programming Languages » ASP » How to get a unique record ID


How to get a unique record ID

Reply
  #1 (permalink)  
Old 02-19-10, 08:43 AM
Vertigomf Vertigomf is offline
New Member
 
Join Date: Feb 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
How to get a unique record ID

Hi,

Let me say first of all that I am completely new to this forum and also not a desperately experienced in ASP and VBScript programmer.

My problem is the generation of a truly unique ID for a record in an access DB (please don't start telling me to use a 'real' database - that's what I've got and that's what I have to use!). Yes, I know there are loads of discussions out there on this topic but none of them are completely satisfactory.

Here are my thoughts as I understand things and the problems with them:

The simplest approach is to use an autonumber - fine this gives a unique ID, but my problem is that I need to copy these records from one db to another (identical structure) and they MUST retain the same ID after the copy. This is not an option with autonumber.

Next we have max approach as in:

Code:
      rsNewPacks_cmd.CommandText = "SELECT max(pkPackID) AS maxid FROM tblPacks WHERE pkEventID=" + CStr(newEventID)
      Set rsNewPacks = rsNewPacks_cmd.Execute
      newPackID = rsNewPacks("maxid")
Now I can go ot to use the newPackID in a new record. I have seen this proposed many times in discussions throughout the web and indeed used in some of our own legacy code and quite frankly I'm horrified. Maybe I'm misunderstanding it, but it seems to be that the internet is pretty much the biggest multiuser environment in the world and sooner or later two users will access the database at approximately the same time both getting the same maximum value before the other has the chance to save their new record. Result - two (or more if we are really busy) records with identical IDs.

I have considered using a random number and keep generating a new one until we get one that doesn't yet exist. Not as horrendous as it might seem if (a) we are not doing this too often and (b) the table in question is not too big. Unfortunately whilst I could go down this route successfully it is really going to be much more convenient to the user if the generated IDs are (more or less) sequential - which pushes me back towards the autonumber arghhhh.

Can anyone out there help? pretty please!

Mike
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-22-10, 08:07 AM
Vertigomf Vertigomf is offline
New Member
 
Join Date: Feb 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Well I have to say I'm a little disappointed by the lack of response to my first post but maybe there's less interest in plain vanilla ASP now. So I shall stick my neck out and post the solution I have come up with and see what people think.

Basically I want to generate a unique record ID in a multi user environment without using autonumber (as discussed in the original post).

The solution I have come with is to use the max approach as shown above but put it in a loop checking for an id number clash. At first glance it seems inefficient but then most of the time there will not be a multi user clash and the first attempt should work fine. So here it is:

First make sure the field is set to indexing with no duplicates allowed
use something like:

Code:
cmd.CommandText = "SELECT max(oOrderID) AS maxid FROM tblOrders"
Set rs = cmd.Execute
Dim orderid, done
if IsNull(rs("maxid")) Then orderid = 1 Else orderid = rs("maxid") + 1
done = false
' we have set up a number field set to 'no duplicates'. 
' Then we will work out the next logical id (in the code just above).
' however since the internet is a major multiuser system we have to 
' assume that it is likely for another user to come in at the same time
' and end up with the exact same id. So we will try to save the record 
' with that id and if it fails increment it by one and try again until it succeeds

cmd.CommandText = "INSERT INTO tblOrders (oOrderID, ........) VALUES (?, ...........)" 
cmd.Parameters.Append cmd.CreateParameter("param0", 3, 1, -1, orderid)
..............

on error resume next
While Not done
  Err.Clear
  cmd.Parameters.Item(0).Value = orderid
  cmd.Execute		  
  If Err.Number <> 0 Then  ' we will assume that it has failed due to an id number clash
    orderid = orderid + 1
   Else
    done = true
  End If
Wend
on error goto 0
I have left much of the code out only putting in the relevant bits!

Maybe someone would like to comment on this solution, in particular the commented assumption about the error trapping. Maybe offer a better solution?

regards
Mike
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-22-10, 08:41 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 02-22-10, 08:59 AM
Vertigomf Vertigomf is offline
New Member
 
Join Date: Feb 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for that wirehopper, however whilst I may have misunderstood - that article seems to suggest that the Access GUID is still essentially an autonumber which would mean that I would not be able to explicitly copy an existing one into the field. This is essentially my primary requirement. I need a unique ID that can be overwritten with a different value, which autonumbers do not permit. It looks to me like the GUID would suffer from the same problem.

Thanks
Mike
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 02-24-10, 10:21 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
I think it's worth a try - if the database is set up to allow the key to be a string, rather than an autoincrement, you should be able to create and update with the GUIDs as they described it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 02-25-10, 06:33 AM
Vertigomf Vertigomf is offline
New Member
 
Join Date: Feb 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for that again Wirehopper, I'll try and give that a go sometime. I've got the other mechanism working now and as always tends to be the case I have such a tight deadline that I'm going to have to live with that for now. My other slight concern, as the table in question has relatively few fields in each record but does have a LOT of records, is that a GUID is pretty big (in this case almost as big as the rest of the fields put together) and so the overhead is pretty high. That said I will have to take a look at it and see what happens.

Mike
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Case Study: Unique Visitors Kobra The Lounge 2 12-12-06 09:04 AM
Adding to Record? tommyc325 Database 7 11-02-06 01:17 PM
Record Clicks cyberfolli PHP 0 09-26-06 07:14 PM
scrolling in record pallabmondal123 PHP 0 03-27-06 08:01 AM
Need help with ip unique hits with times control postyourweb PHP 5 07-19-05 09:07 PM


All times are GMT -5. The time now is 12:59 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.