当前位置: 首页 > 图文教程 > 数据库 > MYSQL > SQL实现动态交叉表

MYSQL
MySQL安全性指南
linux下建立mysql镜像数据库
Mysql备份脚本
MySQL的DBI/DBD简明安装手册
Mysql中左连接的使用
MySQL管理介绍
采用ODBC接口访问MySQL指南
MySQL UDF调试方式debugview
MySQL数据目录结构
MySQL数据导入导出方法与工具介绍
MySQL服务维护笔记(上)
跟着lanche学MySQL
MYSQL初学者使用指南
MySQL数据库的漏洞相当于其它数据库1/4
通过Mysql的语句生成后门木马的方法
怎样使MySQL安全以对抗解密高手
MySQL3.23.31之前版本的安全漏洞
MySQL安全问题(匿名用户)的一点心得
通过mysql入侵NT和win2000
Mysql数据库的安全配置、实用技巧

MYSQL 中的 SQL实现动态交叉表


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 67 ::
收藏到网摘: n/a

以下为引用的内容:
SET QUOTED_IDENTIFIER ON
  GO
  SET ANSI_NULLS ON
  GO
  ALTER procedure CrossTable
  @strTableName as varchar(50)='', --查询表
  @strCol as varchar(50)='',
  @strGroup as varchar(50)='',--分组字段
  @strNumber as varchar(50)='',--被统计的字段
  @strCompute as varchar(50)='Sum'--运算方式
  as
  declare @strSql as varchar(1000),@strTempCol as varchar(100)
  execute ('DECLARE corss_cursor CURSOR FOR SELECT DISTINCT '+@strCol+' from '+@strTableName+' for read only') --生成游标
  begin
  set nocount on
  set @strSql='select '+@strGroup+','+@strCompute+'('+@strNumber+') as ['+@strNumber+']'
  open corss_cursor
  while(0=0)
  begin
  fetch next from corss_cursor
  into @strTempCol
  if(@@fetch_status <>0) break
  set @strSql=@strSql+','+@strCompute+'( case '+@strCol+' when '''+@strTempCol+''' then '+@strNumber +' else 0 end ) as        ['+@strTempCol+']'
  end
  set @strsql=@strSql+' from '+@strTableName+' group by '+@strGroup
  print @strSql
  execute(@strSql)
  if @@error <>0 return @@error
  print @@error
  close corss_cursor
  deallocate corss_cursor return 0
  end
  GO
  SET QUOTED_IDENTIFIER OFF
  GO
  SET ANSI_NULLS ON
  GO