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
Post a Comment