Solved

.asp MS SQL unique identifier generator - getting duplicates

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

776 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