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

行转列后结果集生成新表的问题.
SQL code

if object_id('dest1') is not  null
    drop table dest1

declare @yf1 varchar(7)
declare @yf2 varchar(7)
declare @yf3 varchar(7)
declare @sqlstr Nvarchar(4000)
set @yf1='2012-07'
set @yf2='2012-08'
set @yf3='2012-09'

set @sqlstr='select *  into dest1 from (select 部品品番,供应商名称,sum(case 月份 when '''+ @yf1 + '''then 部品数量 else 0 end) '''+  @yf1 +'''
           ,sum(case 月份 when '''+ @yf2 + '''then 部品数量 else 0 end) '''+  @yf2 +''' 
           ,sum(case 月份 when '''+ @yf3 + '''then 部品数量 else 0 end) '''+  @yf3 +'''
           from dest group by 部品品番,供应商名称)'
select @sqlstr
exec(@sqlstr)

/*-----------------------------
if object_id('dest1') is not  null
    drop table dest1

declare @yf1 varchar(7)
-----------------------------*/

(所影响的行数为 1 行)

服务器: 消息 170,级别 15,状态 1,行 4
第 4 行: ')' 附近有语法错误。




------解决方案--------------------
望给出测试数据
------解决方案--------------------
需要给子查询一个表别名。