sql 验证码生成和补位

declare @a int = 0,@b varchar(10) = ''

CREATE TABLE #temp2  (
                        vcCode varchar(10),
                        nStatus int
                    )
while(@a < 10000)
begin
       --随机数生成(不推荐)  
   --set @b = right('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()
原文地址:https://www.cnblogs.com/ghelement/p/11423171.html