日期:2014-05-17  浏览次数:20404 次

关于任意输入3个数,要求用存储过程排序,按大到小排或者小到达都可以
问题一:
要求任意输入三个数 比如x,y,z,要求按其大到小排列显示出来(小到大呢?)

或者三个数中求最大值或者最小值

提供一种做法如下:
create proc proc_three(@x float,@y float,@z float)
as
if @x>@y
if @x>@z
print '最大值:' +str(@x)
else 
print '最大值:' +str(@z)
else
if @y>@z
print '最大值:' +str(@y)
else
print 
'最大值:' +str(@z)

问题二:
求三个数的最大值,最小值有没有更好的办法?



存储过程 三个数排序 最大值、最小值的算法

------解决方案--------------------

create proc proc_three(@x float,@y float,@z float)
as
set nocount on
declare @max float,@min float

select @max=max(col),@min=min(col)
from 
(select @x as col 
union all select @y
union all select @z
)t

select @max as 'max',@min as 'min'

go


exec proc_three 2,3,7


/*
max    min
-----------------
7.0 2.0
*/

------解决方案--------------------
create proc proc_three(@x float,@y float,@z float)
as
set nocount on

select col
from 
(select @x as col 
union all select @y
union all select @z
)t
order by col

declare @max float,@min float

select @max=max(col),@min=min(col)
from 
(select @x as col 
union all select @y
union all select @z
)t

print '最大值:' +str(@max)
print '最小值:' +str(@min)

go


exec proc_three 2,3,7


/*
col
-----------------
2.0
3.0
7.0

*/

------解决方案--------------------
一个内存表就可搞定了:
create proc proc_three(@x float,@y float,@z float)
as
declare @t table ( num float )
insert into @t values(@x),(@y),(@z)
select * from @t order by 1
select MAX(num) max from @t
select MIN(num) min from @t