如何使用随机数填充数据库列 [英] How to populate a database column with random numbers

查看:167
本文介绍了如何使用随机数填充数据库列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

我想你可以使列成为主键,以防止重复,虽然这是一种黑客。

  ----创建随机数生成的变量
DECLARE @Random INT;
DECLARE @Upper INT;
DECLARE @Lower INT;
DECLARE @Index integer

----这将创建一个介于1和999之间的随机数
SET @Lower = 1 ----最低的随机数
SET @Upper = 999 ----最高随机数
SET @Index = 0 --- while循环计数器

---循环从0到10
WHILE @Index< 10
BEGIN
SELECT'loop counter =',@index
SELECT @Random = ROUND((@ Upper - @Lower -1)* RAND()+ @Lower),0)
--Inand @Random这里。
SET @index = @index + 1
END


How do I populate a int column, currently empty, with random numbers with no duplicates?

解决方案

I suppose you could make the column a primary key to prevent duplicates, though that's kind of a hack. You can remove the key later.

---- Create the variables for the random number generation
DECLARE @Random INT;
DECLARE @Upper INT;
DECLARE @Lower INT;
DECLARE @Index integer

---- This will create a random number between 1 and 999
SET @Lower = 1 ---- The lowest random number
SET @Upper = 999 ---- The highest random number
SET @Index = 0 --- A while loop counter

--- Loop from 0 to 10
WHILE @Index < 10
BEGIN
  SELECT 'loop counter = ', @index
  SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
  --Insert @Random here.
  SET @index = @index + 1
END

这篇关于如何使用随机数填充数据库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆