1. 程式人生 > >sql隨機插入資料--記錄

sql隨機插入資料--記錄

sql面試題中經常出現一張學生表,表字段有學生ID,學生課程,學生成績

今天要實測,so,需要有資料,now,隨機生成資料,,,

 1 create table student
 2 (
 3 id varchar(50), --編號
 4 class varchar(50),--課程
 5 soure int --成績
 6 )
 7 go
 8 
 9 declare @i int
10 set @i = 0
11 
12 while @i<30
13 begin
14 
15 --插入資料
16 declare @r int
17  exec awf_RandInt 50
,100,@r output 18 insert into student values(''+@i,'語文',@r) 19 20 --修改資料 21 exec awf_RandInt 0,30,@r output 22 update student set class = '數學' where id = @r+'' 23 24 --修改資料 25 exec awf_RandInt 0,30,@r output 26 update student set class = '英語' where id = @r+'' 27 28 29 set @i=@i+1 30 end 31 select * from
student
sql Code

 隨機整數實在園子裡找得,貼出來

 1 --生成隨機整數
 2 create procedure awf_RandInt
 3    @min int,
 4    @max int,
 5    @result int output
 6 as
 7 begin
 8     set @result= cast((rand()*(@max-@min)+@min) as int)
 9     return @result
10 end
sql Code