如何使用范围内的随机数更新表列 [英] How to update a Table Column with a Shuffled numbers from within a range

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

问题描述

我正在尝试使用给定范围内的随机数字更新表格列。我已经处理了以下查询,但它正在生成 DUPLICATE 数字。我需要帮助来删除重复项。



请帮忙。我正在使用SQL-server-2008



I am trying to update a Table Column with shuffled numbers from a given range. I have worked on the following query but it is generating DUPLICATE numbers. I need help to remove duplicates.

Please help. I am using SQL-server-2008

UPDATE TAB_EXAM_FORMS
SET
	RNO_CODE = CAST(RAND(CHECKSUM(NEWID())) * 10 AS INT)+1 
	WHERE  SESSION_ID='46'  





请注意 10 是上述查询中的范围。和 Session_Id 是表格行的过滤器。



谢谢和问候

.

Please note 10 is the range in the above query. and Session_Id is the filter for the rows of the table.

Thanks and Regards

推荐答案

如果您使用SQL Server管理工具并运行查询

If you use a SQL Server Management Tool and run the query
select CAST(RAND(CHECKSUM(NEWID())) * 10 AS INT)+1 



约20倍你可以简单地看到结果是1到10之间的随机值。所以很有可能你得到重复。

扩大你的范围或只使用NewID()。



如果您这样做:


about 20 times you could simply see the result is a random value between 1 and 10. So it is very possible you get duplicates.
Enlarge your range or only use the NewID().

If you would do:

DECLARE @generatedValue INT
SET @generatedValue = CAST(RAND(CHECKSUM(NEWID())) * 10 AS INT)+1



你有一个可以测试的变量。

但是如果你使用的话,你必须删除重复的数字更新声明。您的代码中是否存在导致问题的INSERT语句?


You have a variable which you can test for.
But why you have to remove duplicate number if you use an UPDATE statement. Is there somewhere an INSERT statement available in your code that causes the problem?


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

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