dimanche 20 novembre 2016

inner while with random values

I want to create 100 random cardnumbers. I have an while for those 100 rows and a inner while for random numbers. With this code i can create random numbers but the 100 rows have always the same number and i want a new one for each row.

Create Table #Nr_Cartao
(valor varchar(20) not null) 

declare @NrCartao varchar(20);
declare @NrCartao16Digitos varchar(20);
declare @counter int;
declare @random int;
declare @Nr_iterações int;
declare @digitos int;

set @Nr_iterações = 100;
set @NrCartao9Digitos = '';
set @NrCartao = '';
set @counter = 0;
set @digitos = 1

While @counter < @Nr_iterações
begin

While @digitos <= 16
Begin

set @random = rand()*10
set @NrCartao = @NrCartao + '' + CONVERT(varchar(20), @random)

if (@digitos % 4) = 0 and @digitos < 16
set @NrCartao = @NrCartao + '-'

set @digitos= @digitos +1

end;

set @NrCartao16Digitos = @NrCartao;
set @counter = @counter + 1;

insert into #Nr_Cartao values( @NrCartao16Digitos);

end

select *
from #Nr_Cartao
go;

Can you help?




Aucun commentaire:

Enregistrer un commentaire