本文共 854 字,大约阅读时间需要 2 分钟。
/*设计存储过程,给表中随机录入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
转载地址:https://blog.csdn.net/shunzi110/article/details/1527304 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!