日期:2014-05-19  浏览次数:20515 次

邹老大书上的一个行列转置,怎么不对?
create   table   #tb(
Year   int,
Quar   int,
Price   decimal(10,2))

insert   #tb   select   2001,1,2.1
union   all   select   2001,2,1.7
union   all   select   2001,3,1.6
union   all   select   2002,1,2.7
union   all   select   2002,2,3.1
union   all   select   2002,3,2.4
union   all   select   2003,1,1.9
union   all   select   2003,2,1.3

declare   @s   varchar(8000)
set   @s= 'select   Year '
set   @s=@s
        + ',(case   Quar   when '+cast(Quar   as   varchar)+ 'then   price   end)   as   '+cast(Quar   as   varchar)
        + '   from   #tb '

SQL是我自己写的,不是邹老大的;
提示:列名   'Quar '   无效。

请问哪错了,为什么?

------解决方案--------------------
最後的+ ' from #tb '

改成 from #tb