Select Max(ID) As MaxID From ( Select ID1 As ID From TableName Union All Select ID2 From TableName Union All Select ID3 From TableName Union All Select ID4 From TableName Union All Select ID5 From TableName Union All Select ID6 From TableName ) A
create FUNCTION mmax(@num1 int ,@num2 int ) RETURNS int AS begin if(@num1>=@num2) return @num1 return @num2 end select mmax(mmax(maxx(maxx(maxx(INT1,INT2),INT3),INT4),INT5),INT6) from table
declare @t table(INT1 int,INT2 int,INT3 int,INT4 int,INT5 int,INT6 int) insert into @t select 1,2,4,6,2,1 union all select 0,4,6,1,0,5 union all select 0,1,0,0,0,0select [max]=(select max(col) from (select col=a.int1 union all select a.int2 union all select a.int3 union all select a.int4 union all select a.int5 union all select a.int6)a) from @t a
(
Select ID1 As ID From TableName
Union All
Select ID2 From TableName
Union All
Select ID3 From TableName
Union All
Select ID4 From TableName
Union All
Select ID5 From TableName
Union All
Select ID6 From TableName
) A
1 2 4 6 2 1
2 4 6 1 0 5
0 1 0 0 0 0
返回结果
6
6
1
RETURNS int
AS
begin
if(@num1>=@num2)
return @num1
return @num2
end
select mmax(mmax(maxx(maxx(maxx(INT1,INT2),INT3),INT4),INT5),INT6) from table
insert into @t select 1,2,4,6,2,1
union all select 0,4,6,1,0,5
union all select 0,1,0,0,0,0select [max]=(select max(col) from (select col=a.int1 union all select a.int2 union all select a.int3 union all select a.int4 union all select a.int5 union all select a.int6)a) from @t a