Solved

.asp MS SQL unique identifier generator - getting duplicates

Posted on 2014-12-16
6
112 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 33

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

830 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