搜索
首页 电脑/网络 软件 系统软件

SQL如何实现动态交叉表?

SQL如何实现动态交叉表?

全部回答

2017-07-27

50 0
    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。
    。

类似问题换一批

热点推荐

热度TOP

相关推荐
加载中...

热点搜索 换一换

电脑/网络
系统软件
硬件
电脑装机
程序设计
互联网
操作系统/系统故障
笔记本电脑
反病毒
百度
软件
软件
系统软件
多媒体软件
办公软件
网络软件
图像处理软件
系统软件
系统软件
举报
举报原因(必选):
取消确定举报