22,209
社区成员
发帖
与我相关
我的任务
分享
--遍历一
declare @i int
set @i = 0
while @i < 50
begin
select @i,case when (rand()*10>3) then 1 else 0 end, 20+rand()*10
set @i = @i+1
end
--遍历二
select number,case when (rand()*10>3) then 1 else 0 end, 20+rand()*10 from master..spt_values where type = 'P' and number <50
--rand(checksum(newid()))
select number,case when (rand(checksum(newid()))*10>3) then 1 else 0 end, 20+rand(checksum(newid()))*10 from master..spt_values where type = 'P' and number <50
/*
0 0 21.2405747327088
1 1 27.9456190276326
2 0 20.3312360213083
3 1 24.019508951005
4 1 29.3095912295001
5 1 26.9395037489887
6 1 26.2400571979804
7 1 23.4296874100105
8 1 28.3703433302878
9 0 21.626218368852
10 1 27.3591695962251
11 0 22.8016784433228
12 1 26.0023868835233
13 1 28.6561538822335
14 1 23.5074714316529
15 1 28.1009133831451
16 1 28.965143456475
17 1 27.5475126746001
18 0 20.5414794998682
19 1 23.8204809574518
20 1 25.0554445599713
21 1 28.6953854788861
22 1 24.4446307002363
23 0 20.1444124423214
24 1 27.0964220889718
25 1 25.6736124922018
26 0 22.9542057974812
27 1 24.4325807408667
28 1 23.9710434165019
29 1 26.3196069001703
30 1 28.5227270977844
31 0 22.4891593333787
32 1 23.7556917173
33 1 26.8955025512783
34 1 24.5754633694023
35 0 22.3720849160313
36 1 25.8582963621719
37 1 24.3026179037261
38 1 29.4438262762375
39 1 26.9950067854658
40 1 23.9601687536572
41 0 21.0667823080168
42 1 28.0473460727525
43 1 25.5875455946914
44 1 26.4897495507555
45 1 24.3454389014981
46 0 20.0862760353135
47 1 29.2577682258013
48 1 28.5434470372606
49 0 21.0136464467884
*/
create table #t(id int identity(1,1),rd float)
go
insert into #t select rand()
go 50
select number,case when (b.rd*10>3) then 1 else 0 end, 20+b.rd*10
from master..spt_values a
left join #t b on a.number=b.id-1 where type = 'P' and number <50
drop table #t
SELECT abs(CHECKSUM(NEWID())%50)
GO 50
/*开始执行循环
-----------
10
(1 行受影响)
-----------
37
(1 行受影响)
-----------
34
(1 行受影响)
-----------
7
(1 行受影响)
-----------
32
(1 行受影响)
-----------
5
(1 行受影响)
-----------
47
(1 行受影响)
-----------
26
(1 行受影响)
-----------
11
(1 行受影响)
-----------
22
(1 行受影响)
-----------
37
(1 行受影响)
-----------
29
(1 行受影响)
-----------
28
(1 行受影响)
-----------
9
(1 行受影响)
-----------
47
(1 行受影响)
-----------
35
(1 行受影响)
-----------
5
(1 行受影响)
-----------
44
(1 行受影响)
-----------
43
(1 行受影响)
-----------
32
(1 行受影响)
-----------
26
(1 行受影响)
-----------
12
(1 行受影响)
-----------
18
(1 行受影响)
-----------
35
(1 行受影响)
-----------
5
(1 行受影响)
-----------
49
(1 行受影响)
-----------
7
(1 行受影响)
-----------
25
(1 行受影响)
-----------
44
(1 行受影响)
-----------
0
(1 行受影响)
-----------
38
(1 行受影响)
-----------
32
(1 行受影响)
-----------
14
(1 行受影响)
-----------
49
(1 行受影响)
-----------
22
(1 行受影响)
-----------
43
(1 行受影响)
-----------
42
(1 行受影响)
-----------
8
(1 行受影响)
-----------
36
(1 行受影响)
-----------
34
(1 行受影响)
-----------
27
(1 行受影响)
-----------
1
(1 行受影响)
-----------
14
(1 行受影响)
-----------
38
(1 行受影响)
-----------
16
(1 行受影响)
-----------
2
(1 行受影响)
-----------
4
(1 行受影响)
-----------
40
(1 行受影响)
-----------
0
(1 行受影响)
-----------
7
(1 行受影响)
批处理执行已完成 50 次。
*/
SELECT abs(CHECKSUM(NEWID())%50)
GO 50
SELECT abs(CHECKSUM(NEWID())%100000)
GO 10000