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

这段SQL是不是日期格式有问题?
drop   table   ta
create   table   ta(员工   varchar(2),日期   datetime   ,数据   int)
insert   ta   select   'A ', '2000-1-1 ',2000
union   all   select   'A ', '2000-1-2 ',2500
union   all   select   'A ', '2000-1-5 ',1600
union   all   select   'A ', '2000-1-7 ',2600
union   all   select   'A ', '2000-1-10 ',3000
union   all   select   'B ', '2000-1-1 ',3200
union   all   select   'B ', '2000-1-5 ',3400
union   all   select   'B ', '2000-1-8 ',5600
union   all   select   'C ', '2000-1-2 ',1100
union   all   select   'C ', '2000-1-3 ',1300
union   all   select   'C ', '2000-1-4 ',2356
union   all   select   'C ', '2000-1-6 ',1700
union   all   select   'C ', '2000-1-9 ',1200


declare   @m   datetime,   @n   datetime,@sql   varchar(1000)
set   @m=cast( '2000-1-1 '   as   datetime)
set   @n=cast( '2000-1-31 '   as   datetime)

while   @m <=@n
begin

select   @sql=isnull(@sql, ' ')+ ',[ '+cast(@m   as   varchar)+ ']=sum(case   when   日期= ' ' '+cast(@m   as   varchar)+ ' ' '   then   数据   else   0   end) '
set   @m=dateadd(day,1,@m)

end

set   @sql= 'select   员工 '+@sql+ '   from   ta   group   by   员工 '

exec   (@sql)

--------------------------------

(所影响的行数为   13   行)

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


------解决方案--------------------
字段定义太短

@sql varchar(8000)

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

最新版本:20070130

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html