select a.AA,
(case when (select count(*) from b where AA=a.AA)=0 then ''
when (select count(*) from b where AA=a.AA)=1 then
(selec top 1 DD from b where AA=a.AA)
when (select count(*) from b where AA=a.AA)>1 then
(selec top 1 DD from b where AA=a.AA)+'等'
end ) as DD
from a
--测试数据
create table A(AA varchar(10),BB varchar(10),CC varchar(10))
insert A select 'A1','B1','C1'
union all select 'A2','B2','C2'
create table B(AA varchar(10),DD varchar(10))
insert B select 'A1','D1'
union all select 'A1','D2'
union all select 'A2','D3'
go
--查询处理的函数
create function f_qry(
@AA varchar(10)
)returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r=''
select @r=@r+'+'+rtrim(DD)
from B where AA=@AA
return(stuff(@r,1,1,''))
end
go