SQL数据表插入随机数(转)

declare @T TABLE (id int identity(1,1),[Name] nvarchar(20), Randnum int)

insert @T ([Name])
select 'A1' union all
select 'A2' union all
select 'A3' union all
select 'A4' union all
select 'A5' union all
select 'A6' union all
select 'A7' union all
select 'A8' union all
select 'A9' union all
select 'A10' union all
select 'A11' union all
select 'A12'

select * from @T
/*
id          Name                 Randnum
----------- -------------------- -----------
1           A1                   NULL
2           A2                   NULL
3           A3                   NULL
4           A4                   NULL
5           A5                   NULL
6           A6                   NULL
7           A7                   NULL
8           A8                   NULL
9           A9                   NULL
10          A10                  NULL
11          A11                  NULL
*/


update @T
set Randnum=rand()*1000

select * from @T

/*
id          Name                 Randnum
----------- -------------------- -----------
1           A1                   357
2           A2                   357
3           A3                   357
4           A4                   357
5           A5                   357
6           A6                   357
7           A7                   357
8           A8                   357
9           A9                   357
10          A10                  357
11          A11                  357
12          A12                  357
*/

update @T
set Randnum=(select cast(ceiling(rand(checksum(newid()))*100) as  int))

select * from @T

/*
id          Name                 Randnum
----------- -------------------- -----------
1           A1                   28
2           A2                   52
3           A3                   66
4           A4                   3
5           A5                   97
6           A6                   100
7           A7                   61
8           A8                   65
9           A9                   82
10          A10                  62
11          A11                  68
12          A12                  7
*/

  

posted on 2013-12-26 11:35  douqiumiao  阅读(2743)  评论(0编辑  收藏  举报