--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)
max(case 前面没空格...
有空格之后依据报错:
消息 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
@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的前面多个引号,后面少个引号.
而且这句也重复了.