Solved

.asp MS SQL unique identifier generator - getting duplicates

Posted on 2014-12-16
6
105 Views
Last Modified: 2014-12-19
I am trying to generate some unique identifiers through .asp and I am getting duplicates.  I could need up to 2000 of them.  The reason I am generating it because I have to reference that GUID in another table.  How can I make sure I am getting non duplicates?

Function CreateWindowsGUID()
  CreateWindowsGUID = CreateGUID(8) & "-" & _
    CreateGUID(4) & "-" & _
    CreateGUID(4) & "-" & _
    CreateGUID(4) & "-" & _
    CreateGUID(12)
End Function

Function CreateGUID(tmpLength)
  Randomize Timer
  Dim tmpCounter,tmpGUID
  Const strValid = "0123456789ABCDEF"
  For tmpCounter = 1 To tmpLength
    tmpGUID = tmpGUID & Mid(strValid, Int(Rnd(1) * Len(strValid)) + 1, 1)
  Next
  CreateGUID = tmpGUID
End Function

Open in new window


Thanks.

/****** GUID = CAC8CD83-A0A3-A39E-4E4A-E55EEB3FC73D
/****** GUID = 7B975254-5062-545C-EF08-851D7100F775
/****** GUID = 1C55E816-BB0C-F42B-90D7-36EC06D227BD
/****** GUID = CD247DE7-5BCA-A5E9-3196-E7AA9CA35605
/****** GUID = 6EE303B9-0C89-46A8-AB3F-88692175864D
/****** GUID = 1FA1988A-BD58-F777-4CFE-3938B746C685
/****** GUID = BF703E5C-5E16-9835-FCBD-93C1B0AC57BD
/****** GUID = 603EC32D-0FD5-4804-9D8B-3490567D97F5
/****** GUID = CAC8CD83-A0A3-A39E-4E4A-E55EEB3FC73D
/****** GUID = 7B975254-5062-545C-EF08-851D7100F775
/****** GUID = 1C55E816-BB0C-F42B-90D7-36EC06D227BD
/****** GUID = CD247DE7-5BCA-A5E9-3196-E7AA9CA35605
/****** GUID = 6EE303B9-0C89-46A8-AB3F-88692175864D
/****** GUID = 1FA1988A-BD58-F777-4CFE-3938B746C685
/****** GUID = BF703E5C-5E16-9835-FCBD-93C1B0AC57BD
/****** GUID = 603EC32D-0FD5-4804-9D8B-3490567D97F5
/****** GUID = CAC8CD83-A0A3-A39E-4E4A-E55EEB3FC73D
/****** GUID = 7B975254-5062-545C-EF08-851D7100F775
/****** GUID = 1C55E816-BB0C-F42B-90D7-36EC06D227BD
/****** GUID = CD247DE7-5BCA-A5E9-3196-E7AA9CA35605
/****** GUID = 6EE303B9-0C89-46A8-AB3F-88692175864D
/****** GUID = 1FA1988A-BD58-F777-4CFE-3938B746C685
/****** GUID = BF703E5C-5E16-9835-FCBD-93C1B0AC57BD
/****** GUID = 603EC32D-0FD5-4804-9D8B-3490567D97F5
/****** GUID = CAC8CD83-A0A3-A39E-4E4A-E55EEB3FC73D
/****** GUID = 7B975254-5062-545C-EF08-851D7100F775
/****** GUID = 1C55E816-F121-4B50-D6FC-7D01924DC6DE
/****** GUID = 03490963-A2FF-EC1E-87CB-2EDF271FF516
/****** GUID = B4189E24-43BE-9DDD-E154-CE9EBDE0255E
/****** GUID = 55D634F6-F47D-3DAC-8223-7F5D52B25596
/****** GUID = 0695C9C7-944B-EE6A-33E2-D9F65C18F6CE
/****** GUID = A7635F99-450A-8F29-D4A0-8AB5E1E92606
/****** GUID = 11FD59FF-E6C8-F9C3-857F-2B8377BA565E
/****** GUID = B2CCEEC0-9797-9A81-253D-DC420C8C8696
/****** GUID = 638A7492-F121-4B50-D6FC-7D01924DC6DE
/****** GUID = 03490963-A2FF-EC1E-87CB-2EDF271FF516
/****** GUID = B4189E24-43BE-9DDD-E154-CE9EBDE0255E
/****** GUID = 55D634F6-F47D-3DAC-8223-7F5D52B25596
/****** GUID = 0695C9C7-944B-EE6A-33E2-D9F65C18F6CE
/****** GUID = A7635F99-450A-8F29-D4A0-8AB5E1E92606
/****** GUID = 11FD59FF-E6C8-F9C3-857F-2B8377BA565E
0
Comment
Question by:DMTechGrooup
  • 3
6 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 250 total points
ID: 40504100
If you are using mssql server, you can create a field type uniquidentifier and set the default value to newid() and a unique guid will be generated.  

You can also simply generate random characters in asp, then update your db.  For the field that you are going to update, create a new index on it and make the index unique.  When setting up the index, click on the options tab on the left  (assuming you are in sql manager) and find the general section in the main part of the window and set Ignore Duplicates to True.

Finally generate your random characters and loop through 3000 times and exit the loop when the row count gets to 2000.
0
 
LVL 32

Assisted Solution

by:Big Monty
Big Monty earned 250 total points
ID: 40504706
here is a simple function that'll generate a 14 random char GUID for you by using the current date/time:

<%@LANGUAGE="VBSCRIPT"%>
<HTML>
<BODY>
<%

Response.Write "GUID = " & CreateGUID()

Function CreateGUID()
  Dim tmpTemp
  tmpTemp = Right(String(4,48) & Year(Now()),4)
  tmpTemp = tmpTemp & Right(String(4,48) & Month(Now()),2)
  tmpTemp = tmpTemp & Right(String(4,48) & Day(Now()),2)
  tmpTemp = tmpTemp & Right(String(4,48) & Hour(Now()),2)
  tmpTemp = tmpTemp & Right(String(4,48) & Minute(Now()),2)
  tmpTemp = tmpTemp & Right(String(4,48) & Second(Now()),2)
  CreateGUID = tmpTemp
End Function
%>
</BODY>
</HTML>

Open in new window


http://support.microsoft.com/kb/320375

that article also has functions that'll use a time offset if you want more "uniqueness"
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40504821
Correction: "the main part of the window and set Ignore Duplicates to True"  should be to False
0
 
LVL 24

Author Closing Comment

by:DMTechGrooup
ID: 40507638
Thanks.. I actually created the record and let SQL create the UID.. then did a select on that record to get the UID for my other insert functions.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40509034
Good choice.   I  believe that guid will be unique for the entire database, not just the table.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now