if object_id('tempdb..#a') is not null
drop table #a
create table #a(Id int,Name nvarchar(5),Show_Order int)
insert into #a select 1,'A',4 union all select 2,'B',5 union all select 3,'C',6
select * from #a
当我传入4时,它会返回1,证明它的Show_Order的值为最小值,
当我传入5时,它会返回2,证明它的Show_Order的值不是最值,
当我传入6时,它会返回3,证明它的Show_Order的值为最大值。
------解决方案--------------------
修改变量@a
if object_id('tempdb..#a') is not null
drop table #a
create table #a(Id int,Name nvarchar(5),Show_Order int)
insert into #a select 1,'A',4 union all select 2,'B',5 union all select 3,'C',6
DECLARE @a INT =6
DECLARE @Max INT
DECLARE @Min INT
select @max=MAX(Show_Order),@Min=MIN(Show_Order)
from #a
IF (SELECT Show_Order FROM #a WHERE Show_Order=@a)=@Max
BEGIN
SELECT 3
END
ELSE IF (SELECT Show_Order FROM #a WHERE Show_Order=@a)=@min
BEGIN
SELECT 1
END
ELSE
BEGIN
SELECT 2
END
------解决方案--------------------
select id, CASE Show_Order
WHEN (select max(Show_Order) from #a) THEN 'MAX'
WHEN (select min(Show_Order) from #a) THEN 'MIN'
ELSE 'OTHER'
END
from #a
where Show_Order = @Show_Order