日期:2014-05-18  浏览次数:20483 次

如何写这样的SQL语句?
有一数据表:a
fID,fcode,fName
1         a           aa
2         b           bb
3         c           cc
4         d
5         e

另一数据表   b
fID     a       b     c     d       e
即把上面a表fCode字段值当b表中的字段

怎样显示  
fID,   a   b   c  
即把a表中   fName为空对应   的fCode值所对应的b表中的字段不用显示?


------解决方案--------------------
declare @a varchar(100)

select distinct @a=coalesce(@a+ ', ')+fcode from [TableA] where fname <> ' '
set @a= 'select '+@a + ' from [TableB] '
exec(@a)