一、在sql中实现将一个正整数随机拆分为三个正整数
1、sql代码
declare @i int
set @i =100;--要拆分的正整数
declare @j int
set @j =round(rand()*@i+1.0,0)
declare @k int
select @k=round(rand()*(@i-@j)+1.0,0)
declare @table table(id int)
if(@j>0 and @k>0 and @i-@j-@k>0)
begin
insert into @table
select @j union all
select @k union all
select @i-@j-@k
select * from @table order by newid();
end
go 100
2、运行结果
/* 运行结果(部分)
id
-----------
77
6
17
(3 row(s) affected)
id
-----------
7
31
62
(3 row(s) affected)
id
-----------
29
66
5
(3 row(s) affected)
id
-----------
4
67
29
(3 row(s) affected)
id
-----------
37
32
31
(3 row(s) affected)
id
-----------
39
39
22
(3 row(s) affected)
id
-----------
64
22
14
(3 row(s) affected)
id
-----------
9
13
78
(3 row(s) affected)
id
-----------
10
62
28
(3 row(s) affected)
id
-----------
33
10
57
(3 row(s) affected)
*/