我有一个随机密码生成器。它本身工作正常。当我将它放在 while 循环中时,即使种子看起来不同,我也会得到相同的密码。
declare @count int = 0
declare @end int = 500
while (@count < @end)
BEGIN
DECLARE @r varchar(6)
select @r = coalesce(@r, '') +char(
case when r between 0 and 9 then 48
when r between 10 and 35 then 55
else 61 end + r)
from master..spt_values
cross join
(SELECT CAST(RAND(ABS(CHECKSUM(NEWID()))) *61 as int) r) a
where type = 'P' and number < 6
print(newId())
print(@r)
set @count = @count + 1
END
您需要清除
@r
每个循环的值。我可以看到你认为
declare @r varchar(6);
会为你做到这一点,但事实并非如此。查看在存储过程的 while 块中定义的变量作用域 - SQl Server,以获得更多变量声明的乐趣。