title: Generate a unique alphanumeric ID
body: <p>My idea is to generate a unique ID for my table Alphanumeric for My table in SQL server So I use Newid function to do that and then I truncate the result to 8 character. My question is with this code I am sure to have a unique ID? Or maybe not here is the code: </p>
  
  <pre><code>DECLARE @r varchar(8) 
  
  SELECT @r = coalesce(@r, '') + n 
  FROM (SELECT top 8 CHAR(number) n 
     FROM master..spt_values 
     WHERE type = 'P' AND 
        (number between ascii(0) and ascii(9) 
           OR number between ascii('A') and ascii('Z') 
           OR number between ascii('a') and ascii('z')) 
     ORDER BY newid()) a 
  
  DECLARE @id varchar(10)  
  SET @id=CONVERT(varchar(8), @r)  
  DECLARE @myid varchar(10) 
  
  SELECT @myid=SUBSTRING(@r,1,2)+'-'+SUBSTRING(@r,3,3)+'-'+SUBSTRING(@r,6,3)  
  
  PRINT 'Value of @myid is: '+ @myid
  </code></pre>