日期:2014-05-18 浏览次数:20659 次
create table A(id int,[name] varchar(10)) insert into A select 1 ,'a' union all select 2 ,'b' go create table B(mid int,id int,[value] varchar(10)) insert into B select 11 ,1 ,'v1' union all select 11 ,2 ,'v2' union all select 22 ,1 ,'v3' union all select 22 ,2 ,'v4' go declare @sql varchar(8000) set @sql = 'select mid' select @sql = @sql + ',max(case id when '+ltrim(id)+' then [value] else '''' end) as [name('+[name]+')]' from A select @sql = @sql + ' from B group by mid' exec(@sql) drop table A,B /*************** mid name(a) name(b) ----------- ---------- ---------- 11 v1 v2 22 v3 v4 (2 行受影响)
------解决方案--------------------