Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

.asp MS SQL unique identifier generator - getting duplicates

Posted on 2014-12-16
6
Medium Priority
?
115 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 1000 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 1000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

715 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