sql语句向表中某个字段中插入随机生成的几位数字并且去重

2025-03-11 01:14:42
推荐回答(2个)
回答1:

declare @s varchar(6),@i numeric(10,0)
set @i=1
while 1=1
begin
set @s=left (newid(),6)
if not exists(select 1 from Oder_Table where code=@s)
begin
insert Code (code) values(@s)
set @i=@i+1
if @i>1000000
break
end
else
continue
end

回答2:

一条语句实现不了啊
得多条才能实现