SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Useful function for primary keys

    A basic, but simple function I wrote for generating unique ID's for database primary keys. It a combination of the month/day/year, current time in hours/minutes/seconds, and a random number between 1000 and 9999 (or whatever you set it to, I picked these numbers for a consistent 4 digit display).

    Code:
    <%
    'Creates a unique id based on a random number between low and up
    'and today's date and the current time in single digits for primary key(s)
    'low and up represent the lower and upper bounds for the random number
    Function uniqueKey(lower,upper)
    	Dim rndNum, thedate, thetime, uniqueID
    		
    	Randomize
    	rndNum = Int((upper - lower + 1) * Rnd + lower)
    	
    	thedate = DatePart("m", Now()) & DatePart("d", Now()) & DatePart("yyyy", Now())
    	thetime = DatePart("h", Now()) & DatePart("n", Now()) & DatePart("s", Now())
    	
    	uniqueID = thedate & "-" & thetime & "-" & rndNum
    	
    	Response.Write(uniqueID)
    End Function
    %>
    
    <input type="Hidden" name="some_id" value="<% Call uniqueKey(1000,9999) %>">
    The reason I did this instead of a database auto increment is because it can be useful to know the primary key before sending the query (email notification for my purposes). Otherwise you have to turn around and query the database and try to select the record you just added which takes up resources and can be prone to errors.
    Last edited by westmich; Jun 3, 2001 at 15:04.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  2. #2
    SitePoint Member
    Join Date
    May 2001
    Location
    ON, Canada
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Your function is probably good for some purpose though there's an easier way to get around generating a unique id before executing the query.

    This can be done on MS SQL with the help of the uniqueidentifier datatype.

    This type allows INSERT as any other SQL datatype and you can generate an new ID (which is a Windows GUID in fact) by using:
    SELECT NEWID()
    SQL statement.

    Afterwards, you can read it from a usual ASP recordset and user in a query.

    The following code shows some ASP code:

    '....
    strSQL="SELECT NEWID()"
    Set oRS=oConn.Execute(strSQL)
    strNewID=oRS(0)

    strSQL="INSERT INTO SomeTable (my_pr_key_id, other_field_1, ...) VALUES (" & strNewID & ", ...)"

    Set oRS=oConn.Execute(strSQL)
    .....

    Cheers
    Regards,
    Alex Osokin - CTO

    The Web Hosting Portal http://www.findsp.com
    Hosting Forums: http://www.findsp.com/forum
    Web Hosting Newsletter: http://www.findsp.com/hosting/newsletter

  3. #3
    SitePoint Wizard big_al's Avatar
    Join Date
    May 2000
    Location
    Victoria, Australia
    Posts
    1,661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    this is a small function I sometimes use.

    Randomize
    function ZeroPad(sWork, nDigits)
    ZeroPad = right(string(nDigits, "0") & trim(sWork), nDigits)
    End function
    function GetIndexNumber()
    sDay = Day(Now())
    sMonth = Month(Now())
    sYear = Year(Now())
    sHour = Hour(Now())
    sMinute = Minute(Now())
    sSecond = Second(Now())
    Tail = Int(1000000 * Rnd)
    GetIndexNumber = ZeroPad(sYear, 4) & ZeroPad(sMonth, 2) & ZeroPad(sDay, 2) & ZeroPad(sHour, 2) & ZeroPad(sMinute, 2) & ZeroPad(sSecond, 2) & ZeroPad(Tail, 6)
    End function

    Pretty big function but it does the job, an also seams to overcome VB's not so random Random function
    .NET Code Monkey

  4. #4
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Techguy -
    Unless I am reading your code wrong, you've got the database generating a unique ID and then are running a second query to extract the unique ID?

    Big Al-
    Not sure on what you're doing with the ZeroPad function?
    Last edited by westmich; Jun 3, 2001 at 06:49.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  5. #5
    Say WHA?! goober's Avatar
    Join Date
    Sep 2000
    Location
    United States
    Posts
    1,921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Westmich,

    I believe that's what TechGuy is doing.

    As to big_al's code, well, I'm in over my head there.
    Sean Killeen [LinkedIn] [Twitter] [Web]

    Warning: Reality.sys corrupted. Universe halted. Reboot? (Y/N)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •