不知道楼主是不是这个意思?
use
ceshi
if object_id('a') is not null
drop table a
if object_id('b') is not null
drop table b
create table a(id int identity(1,1),name varchar(10))
create table b(id int identity(1,1),aa varchar(10),bb varchar(10),cc varchar(10))
insert into a select '徐强' union all select '小棉花' union all select '小会计'
insert into b select 'a','b','c' union all select 'a1','bsa','c1' union all select 'a2','b2','c2' union all select 'a3','b3','c3' union all select 'a4','b4','c4' union all select 'a5','b5','c5'
select * from a
select * from b
declare @t table (name varchar(10),aa varchar(10),bb varchar(10),cc varchar(10))
insert into @t select a.name,b.aa,b.bb,b.cc from a,b where a.id=b.id
declare @a varchar(1000)
set @a='select '
select @a=@a+'max(case name when '''+name+''' then name end),max(case aa when '''+aa+''' then aa end),max(case bb when '''+bb+''' then bb end),max(case cc when '''+cc+''' then cc end), ' from @t
set @a=substring(@a,1,len(@a)-1)+' from (select a.name,b.aa,b.bb,b.cc from a,b where a.id=b.id) as a'
exec (@a)