报错:
select FOrderNum,sum(FOrderQuantity) as [sum of FOrderQuantity],sum(case FGoodName
when 电风扇 then FOrderQuantity else null
End) as [电风扇],sum(case FGoodName
when 毛笔 then FOrderQuantity else null
End) as [毛笔],sum(case FGoodName
when 书 then FOrderQuantity else null
End) as [书],sum(case FGoodName
when 洗衣机 then FOrderQuantity else null
End) as [洗衣机],sum(case FGoodName
when 衣架 then FOrderQuantity else null
End) as [衣架]from[dbo].[T_Customer]group byFOrderNum我的代码:
ALTER PROCEDURE Usp_Static
@tableName nvarchar(50),
@groupCol nvarchar(50), --分组字段.
@staticCol nvarchar(50), --统计字段.
@showCol nvarchar(50), --表头字段.
@opera nvarchar(50) --聚会函数.
AS
declare @SQL nvarchar(max), @proColumn nvarchar(50) --存储游标执行的列.
execute ('declare curCol cursor for select distinct '+ @showCol+ ' from '+ @tableName+' for read only') --游标.
begin
set nocount on
set @SQL = 'select '+@groupCol+','+@opera+'('+@staticCol+') as ['+@opera+' of '+ @staticCol+']';
open curCol --打开游标.
while(0=0)
Begin
Fetch next from curCol into @proColumn
if(@@fetch_status<>0) break --游标失败.
set @SQL += ','+@opera+'(case '+@showCol+'
when '+@proColumn+' then '+@staticCol+' else null
End) as ['+@proColumn+']'
End
set @SQL += 'from'+ @tableName +'group by'+ @groupCol
execute @SQL
if(@@error <> 0) return @@error --出错.
close curCol
deallocate curCol return 0 --释放游标成功则返回0.
end
//我的表字段有:FOrderNum(订单号),FGoodName(商品名),FOrderQuantity(数量),FCustomerName(用户名).
//我的需求这里是,根据 FOrderNum(订单号) 分组, FGoodName(商品名) 为表头.问,根据现在的报错,貌似是每个 "商品名"无法处理...是不是 双引号的原因...
select FOrderNum,sum(FOrderQuantity) as [sum of FOrderQuantity],sum(case FGoodName
when 电风扇 then FOrderQuantity else null
End) as [电风扇],sum(case FGoodName
when 毛笔 then FOrderQuantity else null
End) as [毛笔],sum(case FGoodName
when 书 then FOrderQuantity else null
End) as [书],sum(case FGoodName
when 洗衣机 then FOrderQuantity else null
End) as [洗衣机],sum(case FGoodName
when 衣架 then FOrderQuantity else null
End) as [衣架]from[dbo].[T_Customer]group byFOrderNum我的代码:
ALTER PROCEDURE Usp_Static
@tableName nvarchar(50),
@groupCol nvarchar(50), --分组字段.
@staticCol nvarchar(50), --统计字段.
@showCol nvarchar(50), --表头字段.
@opera nvarchar(50) --聚会函数.
AS
declare @SQL nvarchar(max), @proColumn nvarchar(50) --存储游标执行的列.
execute ('declare curCol cursor for select distinct '+ @showCol+ ' from '+ @tableName+' for read only') --游标.
begin
set nocount on
set @SQL = 'select '+@groupCol+','+@opera+'('+@staticCol+') as ['+@opera+' of '+ @staticCol+']';
open curCol --打开游标.
while(0=0)
Begin
Fetch next from curCol into @proColumn
if(@@fetch_status<>0) break --游标失败.
set @SQL += ','+@opera+'(case '+@showCol+'
when '+@proColumn+' then '+@staticCol+' else null
End) as ['+@proColumn+']'
End
set @SQL += 'from'+ @tableName +'group by'+ @groupCol
execute @SQL
if(@@error <> 0) return @@error --出错.
close curCol
deallocate curCol return 0 --释放游标成功则返回0.
end
//我的表字段有:FOrderNum(订单号),FGoodName(商品名),FOrderQuantity(数量),FCustomerName(用户名).
//我的需求这里是,根据 FOrderNum(订单号) 分组, FGoodName(商品名) 为表头.问,根据现在的报错,貌似是每个 "商品名"无法处理...是不是 双引号的原因...
when 电风扇 then FOrderQuantity else null
End) as [电风扇],sum(case FGoodName
when 毛笔 then FOrderQuantity else null
End) as [毛笔],sum(case FGoodName
when 书 then FOrderQuantity else null
End) as [书],sum(case FGoodName
when 洗衣机 then FOrderQuantity else null
End) as [洗衣机],sum(case FGoodName
when 衣架 then FOrderQuantity else null
End) as [衣架]from[dbo].[T_Customer]group byFOrderNum' is not a valid identifier.//外层是有单引号的...不懂怎么处理那个"商品名"字符串
'select '+char(39)+''+@groupCol+''+char(39)+','只是举例给你看,具体还是要你写完整。
The name 'select FOrderNum,sum(FOrderQuantity) as [sum of FOrderQuantity],sum(case FGoodName
when '电风扇' then FOrderQuantity else null
End) as [电风扇],sum(case FGoodName
when '毛笔' then FOrderQuantity else null
End) as [毛笔],sum(case FGoodName
when '书' then FOrderQuantity else null
End) as [书],sum(case FGoodName
when '洗衣机' then FOrderQuantity else null
End) as [洗衣机],sum(case FGoodName
when '衣架' then FOrderQuantity else null
End) as [衣架]from[dbo].[T_Customer]group byFOrderNum' is not a valid identifier.我这样写:
set @SQL += ','+@opera+'(case '+@showCol+'
when '''+@proColumn+''' then '+@staticCol+' else null这样应该不对...大神帮忙看一下怎么改...而且我不懂是什么意思....麻烦了...
这样来执行字符串