求MS Sql Sever内部某个范围的随机发生数

求MS Sql Sever内部某个范围的随机发生数

1、直接上代码:

-- 随机更新基础资料-物品资料表中的schedule_days供应商正常补货天数,与供应商表Ctl00504默认值对应:
-- 需多执行几次! Ctl03001表  (范围替换):
--------------------------------------------------------开始
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
declare @schedule_days decimal(4,1)
declare @id numeric(18, 0)
declare @ARandNumer int

select @ARandNumer=0

declare mycur CURSOR FOR
select [ID],schedule_days from Ctl03001
OPEN mycur
FETCH NEXT FROM mycur
  INTO @id,@schedule_days
WHILE @@FETCH_STATUS = 0
begin
-- 5、更新Ctl03001表的:schedule_days供应商正常补货天数(是一个范围): 
   while not ( (@ARandNumer>1) and (@ARandNumer<7) )
   begin
     select @ARandNumer=(ceiling(rand() * 7)-ceiling(rand() * 1) )
   end
   if ( (@ARandNumer>1) and (@ARandNumer<7) )
   begin
     -- @ARandNumer: 是一个范围: 1<@ARandNumer<7 :随机发生基数:视基础资料表的记录范围做相应调整
     update a set a.schedule_days=@ARandNumer
     from ctl03001 a
     where ltrim(rtrim(coalesce(a.ID,''))) = @id
   end
   select @ARandNumer=0
FETCH NEXT FROM mycur
  INTO @id,@schedule_days
end
CLOSE mycur
DEALLOCATE mycur
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

select [ID],schedule_days from Ctl03001
go

2、相关:

2.1、https://blog.csdn.net/pulledup/article/details/100786153 《Sql数据库杂谈》

喜欢的话,就在下面点个赞、收藏就好了,方便看下次的分享:

猜你喜欢

转载自blog.csdn.net/pulledup/article/details/106872107