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

sql列转行的问题,总说语法错误,在线等,拜谢了!
"   declare   @s   varchar(1000)   "
"   set   @s   =   ' ' "
"   select   @s   =   @s+ ',max(case   Name   when   ' ' '   +   Name   +   ' ' '   then   Data   end) '   from   参数表   group   by   Name   order   by   Name "
"   set   @s   =   left(@s,len(@s)-1)   +   'from   参数表   group   by   Name '   "
"   EXEC(@s) "

提示第一行 ', '附近有语法错误
可是网上查了N多大家都是这么写的呀

------解决方案--------------------
--try

declare @s varchar(1000)
--set @s = ' '
select @s = coalesce(@s+ ', ', 'select ')+ 'max(case Name when ' ' ' + Name + ' ' ' then Data end) ' from 参数表 group by Name order by Name
--set @s = left(@s,len(@s)-1) + 'from 参数表 group by Name '
set @s = @s + ' from 参数表 group by Name '
EXEC(@s)

------解决方案--------------------
declare @s varchar(1000)
set @s = ' '
select @s = 'select '+ 'max(case Name when ' ' ' + Name + ' ' ' then Data end) ' from 参数表 group by Name order by Name
set @s = left(@s,len(@s)) + 'from 参数表 group by Name '
EXEC(@s)
------解决方案--------------------
---行转列??Try
declare @s varchar(1000)
set @s = ' '
select @s = @s+ ',max(case when Name= ' ' '+ Name+ ' ' ' then Data else ' ' ' ' end) As [ '+Name+ '] ' from 参数表 group by Name order by Name
--- " set @s = left(@s,len(@s)-1) + 'from 参数表 group by Name ' "
set @s= 'Select '+stuff(@s,1,1, ' ')+ ' from 参数表 '
EXEC(@s)
------解决方案--------------------
declare @s varchar(1000)
set @s = ' '
Select @s=N 'Select Col '
select @s = @s+N ',max(case Name when ' ' ' + Name + ' ' ' then Data end) ' from 参数表 group by Name order by Name "
set @s = left(@s,len(@s)-1) + 'from 参数表 group by Name '
EXEC(@s)
------解决方案--------------------
declare @s varchar(1000)
--set @s = ' '
select @s = coalesce(@s+ ', ', 'select ')+ 'max(case Name when ' ' ' + Name + ' ' ' then Data end) ' from 参数表 group by Name order by Name
--set @s = left(@s,len(@s)-1) + 'from 参数表 group by Name '
set @s = @s + ' from 参数表 group by Name '
print(@s)
--EXEC(@s)

--结果贴出来
------解决方案--------------------
--PRINT都不行?新开一个查询窗口,就执行这几句,把 @s 打印出来,贴出:

declare @s varchar(1000)
select @s = coalesce(@s+ ', ', 'select ')+ 'max(case Name when ' ' ' + Name + ' ' ' then Data end) ' from 参数表 group by Name order by Name
set @s = @s + ' from 参数表 group by Name '
print(@s)

------解决方案--------------------
我们的语句都没有错
怎么你????????????????要么给出你的表结构吧
------------------------------
Create Table 参数表(name varchar(10),data int)
Insert 参数表 Select '张三 ',100
Union All Select '李四 ',200