create table py(id varchar(10),a int,b int,c int)
insert into py select '01',7,1,2
insert into py select '02',2,7,1
insert into py select '03',5,3,2
insert into py select '04',2,3,4
create proc wsp111
as
declare @max_a varchar(10)
declare @min_a varchar(10)
declare @max_b varchar(10)
declare @min_b varchar(10)
declare @max_c varchar(10)
declare @min_c varchar(10)
if((select count(1) from py where a =(select max(a) as [a] from py))>1)
begin
select @max_a=isnull(@max_a+',','')+id from py where a in (select max(a) as [a] from py)
set @max_a=@max_a+'('+cast((select min(a) as [a] from py) as varchar)+')'
end
else
begin
select @max_a=id+'('+cast(a as varchar)+')' from py where a in (select max(a) as [a] from py)
end
if((select count(1) from py where a =(select min(a) as [a] from py))>1)
begin
select @min_a=isnull(@min_a+',','')+id from py where a in (select min(a) as [a] from py)
set @min_a=@min_a+'('+cast((select min(a) as [a] from py) as varchar)+')'
end
else
begin
select @min_a=id+'('+cast(a as varchar)+')' from py where a in (select min(a) as [a] from py)
end
if((select count(1) from py where b =(select max(b) as [b] from py))>1)
begin
select @max_b=isnull(@max_b+',','')+id from py where b in (select max(b) as [b] from py)
set @max_b=@max_b+'('+cast((select min(b) as [b] from py) as varchar)+')'
end
else
begin
select @max_b=id+'('+cast(b as varchar)+')' from py where b in (select max(b) as [b] from py)
end
if((select count(1) from py where b =(select max(b) as [b] from py))>1)
begin
select @min_b=isnull(@min_b+',','')+id from py where b in (select min(b) as [b] from py)
set @min_b=@min_b+'('+cast((select min(b) as [b] from py) as varchar)+')'
end
else
begin
select @min_b=id+'('+cast(b as varchar)+')' from py where b in (select min(b) as [b] from py)
end
if((select count(1) from py where c =(select max(c) from py))>1)
begin
select @max_c=isnull(@max_c+',','')+id from py where c in (select max(c) as [c] from py)
set @max_c=@max_c+'('+cast((select min(c) as [c] from py) as varchar)+')'
end
else
begin
select @max_c=id+'('+cast(c as varchar)+')' from py where c in (select max(c) as [c] from py)
end
if((select count(1) from py where c =(select max(c) from py))>1)
begin
select @min_c=isnull(@min_c+',','')+id from py where c in (select min(c) as [c] from py)
set @min_c=@min_c+'('+cast((select min(c) as [c] from py) as varchar)+')'
end
else
begin
select @min_c=id+'('+cast(c as varchar)+')' from py where c in (select min(c) as [c] from py)
end
select @max_a[max_a],@min_a[min_a],@max_b[max_b],@min_b[min_b],@max_c[max_c],@min_c[min_c]
调用存储过程:
EXEC wsp111