SQL Server:使用随机十进制数填充列

时间:2021-02-23 09:37:12

i want my function to generate float numbers ( like : -123.000 , 874.000 ) in range ( like between: 272 and 3357 ) and update every record's "pos_x" field with unique float numbers. I write this Code but i see my table's field are all identical and also integer and they are positive.

我希望我的函数在范围内生成浮点数(如:-123.000,874.000)(如:272和3357之间),并使用唯一的浮点数更新每个记录的“pos_x”字段。我写这个代码,但我看到我的表的字段都是相同的,也是整数,他们是积极的。

this is my code :

这是我的代码:

UPDATE Driver_tbl
SET pos_x = (ROUND((RAND()* 10000),0))

1 个解决方案

#1


12  

RAND is evaluated once per query

每个查询评估一次RAND

You can seed it like this though using CHECKSUM(NEWID()) so it is random per row

你可以像这样使用CHECKSUM(NEWID())播种它,因此每行都是随机的

UPDATE Driver_tbl
SET pos_x = ROUND(RAND(CHECKSUM(NEWID())) * (3357-272),0) + 272

However, you can also cut out the middle man if you are using ROUND(.., 0)

但是,如果你使用ROUND(...,0),你也可以删除中间人

UPDATE Driver_tbl
SET pos_x = ABS(CHECKSUM(NEWID())) % 9999

#1


12  

RAND is evaluated once per query

每个查询评估一次RAND

You can seed it like this though using CHECKSUM(NEWID()) so it is random per row

你可以像这样使用CHECKSUM(NEWID())播种它,因此每行都是随机的

UPDATE Driver_tbl
SET pos_x = ROUND(RAND(CHECKSUM(NEWID())) * (3357-272),0) + 272

However, you can also cut out the middle man if you are using ROUND(.., 0)

但是,如果你使用ROUND(...,0),你也可以删除中间人

UPDATE Driver_tbl
SET pos_x = ABS(CHECKSUM(NEWID())) % 9999