博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Random in SQL Server
阅读量:7154 次
发布时间:2019-06-29

本文共 2625 字,大约阅读时间需要 8 分钟。

Generating Random Numbers in Transact-SQL

The Transact SQL Rand function can be used to return a random floating point number between 0 and 1:

SELECT RAND() AS RandomNumber

The Rand function can also be supplied with an integer value (i.e. smallint, tinyint or int) to use as a random seed:

SELECT RAND(@MyInteger) AS RandomNumber

Creating Random Numbers in a Certain Numerical Range

The following SQL code can be used to generate random integers between the values of the @MinValue and @MaxValue variables.

DECLARE @RandomNumber float

DECLARE @RandomInteger int
DECLARE @MaxValue int
DECLARE @MinValue int
SET @MaxValue = 4
SET @MinValue = 2
SELECT @RandomNumber = RAND()
SELECT @RandomInteger = ((@MaxValue + 1) - @MinValue) * @RandomNumber + @MinValue
SELECT @RandomNumber as RandomNumber, @RandomInteger as RandomInteger

The output of this SQL will be a random number between 2 and 12 (inclusive).

Random Numbers in SELECT Statements

An important consideration is that if the RAND function is called within a single query then it will return the same random number. You might, therefore, want to consider a different approach, such as the solution described below:

Select random row from a table, solution: Use NewID function:

 

This solution describes a straightforward method of generating randomly sorted result sets in SQL Server. This procedure has a number of potential uses, such as displaying a few randomly chosen news article headlines on a website, or it could be used to randomly select a few advertisements while ensuring the same advert isn't always displayed at the top of the advertising space.

Although it is possible to introduce randomness in SQL Server using time functions, in practice this does not work (especially in stored procedures) because of the speed of execution of the SQL statements [hence many or all of the rows could be returned in exactly the same instant of time]. A far better alternative is, therefore, to use the NewID function to create a unique identifier for each row returned. This returns GUID-like identifiers such as AF53DB47-766F-44B7-82EC-7459E353B3F3.The results set can then be ordered by this column.

The use of the NewID function is shown in this example stored procedure shown below:

CREATE PROCEDURE sp_GetAdverts

(
@MaxNumberOfAdverts int
)
AS
set rowcount @MaxNumberOfAdverts 
select top 100 t_Adverts.AdvertID,
    t_Adverts.TargetURL,
    t_Adverts.AltTag,
    newID() as 'RandomColumn'
from t_Adverts
where getdate() between t_Adverts.AdvertStartDate and t_Adverts.AdvertEndDate
order by newID()
GO

 

 

转载于:https://www.cnblogs.com/programmingsnail/archive/2012/03/01/2375988.html

你可能感兴趣的文章
Android开发, 引入jar包到 Android Private Libraries方法
查看>>
CSS3 实现的一个简单的"动态主菜单" 示例[转]
查看>>
Hibernate使用详解(一)
查看>>
[BZOJ5358]/[HDU6287]口算训练
查看>>
Angular Cli 创建并且运行项目
查看>>
《几何与代数导引》例2.7.2
查看>>
Synplify 使用过程中最常用的选项及命令的介绍
查看>>
创建带属性的XML文档
查看>>
JS实现跟随鼠标的魔法文字
查看>>
[转载]谈谈关于MVP模式中V-P交互问题
查看>>
linux下用eclipse开发mapreduce遇到的问题
查看>>
阻止微信浏览器下拉滑动效果(ios11.3 橡皮筋效果)
查看>>
小程序如何处理键盘覆盖输入框
查看>>
ajax
查看>>
java日历显示年份、月份
查看>>
KMP 算法
查看>>
TLD正确运行方法
查看>>
Rocket - util - GenericParameterizedBundle
查看>>
JDB调试代码 20165324 何春江
查看>>
DIV+CSS中清除浮动的7种方法分析
查看>>