The random function of select is a bit false, perhaps because it is time-based, the same select because the time can not stagger the resulting random number is the same, how to make different rows have different random numbers?
The following is an example of a random date that produces a month.
--Create a view with a minimum of 1 maximum of 31 if object_id (' view_rand_int31 ') is not nullbegindrop view View_rand_int31endgocreate view View_rand_ Int31as Select CAST (Ceiling (rand ()) as int) as [r]go--creation Date (days) of the random function if object_id (' dbo. Fun_getrandday ') is not nullbegindrop function dbo. Fun_getranddayendgocreate FUNCTION dbo. Fun_getrandday (@max INT) returns intas begindeclare @r intselect @r = [R] from View_rand_int31while @r> @maxbeginselect @ R = [r] from view_rand_int31if @r<[email Protected]beginbreak;endendreturn @ rendgo--Test Select conditions to achieve multiple records simultaneously take random number--insert test data row declare @t table (rowNum int identity, [yearmonth] nvarchar ()) Declare @i int, @imax intSELECT @i=1, @imax =28while @i<[email Protected]begininsert into @t ([yearmonth]) Select ' February 2014 ' set @[email p rotected]+1end--executes the query select *, cast (' 2014-02-' + cast (dbo. Fun_getrandday (+) as varchar (2) as DateTime) as [Date], (select cast (Ceiling (rand () *) as int)) as [R] from @t
Sqlsever: How do I generate different random numbers for each row in select?