Generate 6 Digit unique number

sql-server

Solution

Generate a list of all the numbers you haven't assigned yet. Shuffle the list and create a new table containing the random ID and an incrementing index. When you need a new ID, select the one with the minimum index; remove it from the unused ID table and return it as your new ID.

Problem

Edit : It will used as employee unique Number(must be in random order) I've a table having column ``` id(auto-increment), uniqueNumber, and some other field ``` for every inserted record i want to generate a 6 digit unique number for uniqueNumber, Right now im getting this by trigger ``` Create TRIGGER [dbo].[trgUniqueCode] ON [dbo].[testtable] FOR INSERT AS declare @id int; DECLARE @Upper INT DECLARE @Lower INT declare @result int select @id=i.id from inserted i; SET @Lower = 100000 SET @Upper = (select top 1(isnull(UniqueNumber,999999)) from testtable) set @result =(ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)) update testtable set UniqueNumber = @result where ID=@id ``` Im not sure this method will work, im searching for a best method for generating(priority time execution ), or i'll generate unique number in C# ??

Original source