首页 新闻 搜索 专区 学院

消息 156,级别 15,状态 1,第 1 行 关键字 'case' 附近有语法错误。

0
悬赏园豆:10 [已解决问题] 解决于 2013-06-05 16:07

--try:
declare @sql1 varchar(8000)
set @sql1=''
select @sql1=@sql1+'max(case when PicTitle='''+PicTitle+''' then PicUrl else 0 end) as ['+PicTitle+']' from Attachment group by PicTitle
set @sql1='select'+@sql1+' from Attachment group by PicTitle'
exec(@sql1)

新麦穗的主页 新麦穗 | 初学一级 | 园豆:82
提问于:2013-05-29 14:55
< >
分享
最佳答案
0

max(case 前面没空格...

收获园豆:10
三阶 | 小虾三级 |园豆:1436 | 2013-05-29 15:09

有空格之后依据报错:

消息 102,级别 15,状态 1,第 1 行
'max' 附近有语法错误。
消息 105,级别 15,状态 1,第 1 行
字符串 '室内屋顶梁, from Attachment group by PicTitle' 后的引号不完整。

-------- 

declare @sql1 varchar(8000)
 set @sql1=''
 select @sql1=@sql1+' max(case when PicTitle='''+PicTitle+''' then PicUrl else 0 end) as ['+PicTitle+']' from Attachment group by PicTitle
 set @sql1='select'+@sql1+', from Attachment group by PicTitle'
 print @sql1
 exec(@sql1)
 --------------------------

SQL 为什么后一个case执行不完整呢?max(case when PicTitle='室内屋顶梁 from Attachment group by PicTitle

新麦穗 | 园豆:82 (初学一级) | 2013-05-29 15:20

@Anders_Zeus: 检查一下引号吧.

select @sql1=@sql1+' max(case when PicTitle='''+PicTitle+''' then PicUrl else 0 end) as ['+PicTitle+'] from Attachment group by PicTitle'

 from Attachment group by PicTitle的前面多个引号,后面少个引号.

而且这句也重复了.

三阶 | 园豆:1436 (小虾三级) | 2013-05-29 15:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册