Solved

.asp MS SQL unique identifier generator - getting duplicates

Posted on 2014-12-16
6
113 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 53

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 53

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 53

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 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