DECLARE @a int = 0 , @b VARCHAR ( 10 ) = '' the CREATE TABLE # temp2 of ( vcCode VARCHAR ( 10 ), nStatus int ) the while ( @a < 10000 ) the begin - random number generator (not recommended) - SET right = @b ( '000' + Cast (ceiling (RAND () * 10000) AS VARCHAR),. 4) SET @b = right ( ' 000 ' +cast(@a as varchar),4) if not exists(select null from #temp2 where vcCode = @b) begin insert into #temp2 values(@b,11) set @a = @a +1 end end insert into Table_a select * from #temp2 order by newid()