sql - How to generate random numbers using C# that hasn't been listed in a database? -


i'm working on application allows me generate random id numbers (6 integers) employees know how generate these random numbers using c# , not duplicate numbers has been used employee in sql database. below code done in sql:

declare @unique_uid varchar(6)  select @unique_uid = right(abs(cast(cast(newid() varbinary) int)), 6)  while exists (select 1 dbo.tbluserid      @unique_uid = userid_uid           , len(@unique_uid) = 6          , @unique_uid <> @parm_user_pin)(          select @unique_uid = right(abs(cast(cast(newid() varbinary) int)), 6) 

`

6 integeres make 999999 numbers. write them available ones in table. pick 1 randomly , delete it:

  cte (     select top (1) number     available     order newid()   )   delete cte   output deleted.number; 

this more reliable trying generate random , check availability. if org has 999998 employees already, trying randomly generate 1 still available going take ages...


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -