一个行装置列的存储过程
USE [BW_SMS]
GO
/****** Object: StoredProcedure [dbo].[byPivotHZ] Script Date: 03/29/2012 13:50:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[byPivotHZ] --创建转置存储过程
@strTabName as varchar(50) = 'SMS_DateReport',
@strCol as varchar(50) = 'vc_CustomerName ', --列标题
@strGroup as varchar(50) = 'vc_EmployeeName', --分组字段
@strNumber as varchar(50) = 'dt_ReceiveTime', --被统计的字段
@strNumberDp as varchar(50) = '出差天数', --被统计字段的显示值
@strSum as varchar(10) = 'count', --运算方式
@strRowCondtion as varchar(1000) ='vc_EmployeeName=''小李'' and left(convert(varchar(20),dt_ReceiveTime,120),10) >=''2012/3/11 0:00:00''', -- , ' 1=1'
@strColCondtion as varchar(1000)=' AND 1=1' --=' and left(convert(varchar(20),dt_ReceiveTime,120),10) >=''2011-03-11'' and left(convert(varchar(20),dt_ReceiveTime,120),10) <=''2012-03-11'' and 1=1'
--left(convert(varchar(20),dt_ReceiveTime,120),10) >='2012/3/11 0:00:00' and left(convert(varchar(20),dt_ReceiveTime,120),10) <='2012/3/11 0:00:00' and 1=1
--@strColCondtion as varchar(1000)=' where 1=1 and left(convert(varchar(20),dt_ReceiveTime,120),10)>=''2013-02-28'''
AS
BEGIN
DECLARE @strSql as varchar(8000), @strTmpCol as varchar(100),@strsqlHZ as varchar(8000)
--如果存在临时表,则删除临时表
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[temp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[temp]
SET @strsql ='DECLARE corss_cursor CURSOR FOR SELECT DISTINCT ' + @strCol + ' from ' + @strTabName + ' WHERE '+@strCol+' IS NOT NULL '+@strColCondtion +' for read only '
EXECUTE (@strsql) --生成游标
begin
SET nocount ON
SET @strsqlHZ ='select DISTINCT '' '' AS 姓名,' + @strSum + '(distinct left(convert(varchar(20),' + @strNumber + ',120),10)) AS [' + @strNumberDp + ']' --汇总查询的前半段,带每个客户总计出差天数的汇总表
SET @strsql ='select ' + @strGroup + ' AS 姓名, ' + @strSum + '(distinct left(convert(varchar(20),' + @strNumber + ',120),10)) AS [' + @strNumberDp + ']' --查询的前半段
OPEN corss_cursor
while (0=0)
BEGIN
FETCH NEXT FROM corss_cursor --遍历游标,将列头信息放入变量@strTmpCol
INTO @strTmpCol
if (@@fetch_status<>0) break
if @strTmpCol<>''
BEGIN
SET @strsqlHZ = @strsqlHZ +
',(select ' + @strSum + '( distinct left(convert(varchar(20),' + @strNumber + ',120),10)) from '+ @strTabName +
' WHERE '+@strRowCondtion +' and '+ @strCol+'=''' + @strTmpCol +''') as [' + @strTmpCol +']' --汇总查询的中间段
SET @strsql = @strsql + ', ' + @strSum + ' (distinct CASE ' + @strCol + ' WHEN ''' + @strTmpCol + ''' THEN left(convert(varchar(20),' + @strNumber + ',120),10) ELSE Null END) AS [' + @strTmpCol + ']' --构造查询
END
END
SET @strsqlHZ = @strsqlHZ + ' into temp from SMS_DateReport WHERE '+ @strRowCondtion --汇总查询的后段,将每个客户总计出差天数存入temp表中,后续再取出这样可以放在SQL语句过长
EXECUTE(@strsqlHZ) --执行
SET @strsql = @strsql + ' from ' + @strTabname + ' where '+@strRowCondtion+' group by ' + @strGroup --查询结尾
SET @strsql='select * from temp union all ' + @strsql
EXECUTE(@strsql) --执行
IF @@error <>0 RETURN @@error --如果出错,返回错误代码
CLOSE corss_cursor
DEALLOCATE corss_cursor RETURN 0 --释放游标,返回0表示成功
end
END
传入参数后,执行
错误信息如下:
服务器: 消息 170,级别 15,状态 1,行 4
第 4 行: '123' 附近有语法错误。在关键字 'as' 附近有语法错误。
调试信息:
--AS 总是提示as附近有语法错误,原因,列名称里面含数字,而且列名称没有加[],红色字体部分加【】即可成功执行。
select DISTINCT ' ' AS 姓名,
count(distinct left(convert(varchar(20),dt_ReceiveTime,120),10)) AS [出差天数],
(select count( distinct left(convert(varchar(20),dt_ReceiveTime,120),10)) from SMS_DateReport WHERE 1=1 and vc_CustomerName ='123陕西煤矿') as [123陕西煤矿],
(select count( distinct left(convert(varchar(20),dt_ReceiveTime,120),10)) from SMS_DateReport WHERE 1=1 and vc_CustomerName ='nan') as nan,
(select count( distinct left(convert(varchar(20),dt_ReceiveTime,120),10)) from SMS_DateReport WHERE 1=1 and vc_CustomerName ='北路科技') as 北路科技,
(select count( distinct left(convert(varchar(20),dt_ReceiveTime,120),10)) from SMS_DateReport WHERE 1=1 and vc_CustomerName ='山西煤矿') as 山西煤矿,
(select count( distinct left(convert(varchar(20),dt_ReceiveTime,120),10)) from SMS_DateReport WHERE 1=1 and vc_CustomerName ='陕西煤矿') as 陕西煤矿
into temp from SMS_DateReport WHERE 1=1