/*设计存储过程,给表中随机录入1--99999的数字,通过游标找出其中的最大值和最小值*/
create table emp
(
eid varchar(10)
)

--drop table emp

create proc prand
as
begin
 declare @i int
 set @i=0
 while @i<100
 begin
 insert into emp select floor(rand()*100000)
 --rand()*100000的取值范围为1--99999
 set @i=@i+1
 --循环插入100条随机数
 end

 declare crl scroll cursor for select * from emp
 --定义游标
 open crl
 --打开游标
 --fetch first from crl
 declare @max int,@min int,@temp int
 --@max最大值,@min最小值,@temp 临时变量
 fetch next from crl into @max
 --首次推进游标,'into @max'是把其推进结果赋值给@max,关于into子句的用法建议参看联机丛书了解一下,帅的很...
 set @min=@max
 --将此初值也赋给最小值
 while @@fetch_status=0
 begin

 fetch next from crl into @temp
 if @temp>@max
 begin
 set @max=@temp
 end

 if @temp<@min
 begin
 set @min=@temp

 end

 end
 print '最大值为'+convert(varchar,@max)
 print '最小值为'+convert(varchar,@min)
 --输出结果,需要强制转换
 close crl
 --关闭游标
 deallocate crl
 --删除游标
end
--drop proc prand

exec prand
--执行存储过程,该过程没有参数
--最大值和最小值的输出请把消息框往下
posted on 2007-06-18 22:59  小角色  阅读(288)  评论(0)    收藏  举报