`
宋科明
  • 浏览: 101869 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

按任意的字段旋转的存储过程

阅读更多

----------------------------------------------------------------------------
-- 分段截取函数
----------------------------------------------------------------------------
CREATE    FUNCTION DBO.FUN_SplitStr(
@S VARCHAR(8000),      -- 包含多个数据项的字符串
@POS INT,              -- 要获取的数据项的位置
@SPLIT VARCHAR(10)    -- 数据分隔符
) RETURNS VARCHAR(128)
AS
BEGIN
IF @S IS NULL RETURN(NULL)
DECLARE @SPLITLEN INT
SELECT @SPLITLEN=LEN(@SPLIT+'A')-2
WHILE @POS>1 AND CHARINDEX(@SPLIT,@S+@SPLIT)>0 BEGIN
SELECT @POS=@POS-1,
@S=STUFF(@S,1,CHARINDEX(@SPLIT,@S+@SPLIT)+@SPLITLEN,'')
END
RETURN  LEFT(@S,CHARINDEX(@SPLIT,@S+@SPLIT)-1)
END

GO
-------------------------------------------
--行列转换
-------------------------------------------
CREATE  PROC PRC_CORSSQUERY
@NVR_TABNAME AS NVARCHAR(1024) = '', -- 此处放表名
@NVR_XCOL AS NVARCHAR(1024) = '', -- 表头分组依据字段 (横向字段,以系统编号作为依据,内部转化,此字段只能有一个)
@NVR_YCOL AS NVARCHAR(1024) = '', -- 分组字段        (纵向字段,直接取名称列,可以有多个,多个用逗号分隔)
@NVR_STATCOL AS NVARCHAR(1024) = '', -- 被统计的字段    (值)
@NVR_WHERE AS NVARCHAR(4000)='', -- WHERE条件  (一定要带 WHERE 语句,如:WHERE 地区=''广州'' )  * 注意字符一定要用两个分号.
@NVR_TOTAL AS NVARCHAR(1024)='', -- 合计的公式( SUM,AVG ,将其放在第一列.如果多项,请用逗号分隔)
@NVR_ORDERBY AS NVARCHAR(1024)=''          -- 排序字段,如: [海关编码],[地区]
AS BEGIN

    --SET NOCOUNT ON

DECLARE @NVR_CMD AS NVARCHAR(4000)
DECLARE @NVR_XCOLNAME AS NVARCHAR(256)

DECLARE @NVR_SQL0 AS NVARCHAR(4000)
DECLARE @NVR_SQL1 AS NVARCHAR(4000)
DECLARE @NVR_SQL2 AS NVARCHAR(4000)
DECLARE @NVR_SQL3 AS NVARCHAR(4000)
DECLARE @NVR_SQL4 AS NVARCHAR(4000)
DECLARE @NVR_SQL5 AS NVARCHAR(4000)
DECLARE @NVR_SQL6 AS NVARCHAR(4000)
DECLARE @NVR_SQL7 AS NVARCHAR(4000)
DECLARE @NVR_SQL8 AS NVARCHAR(4000)
DECLARE @NVR_SQL9 AS NVARCHAR(4000)
DECLARE @NVR_SQL10 AS NVARCHAR(4000)
DECLARE @NVR_SQL11 AS NVARCHAR(4000)
DECLARE @NVR_SQL12 AS NVARCHAR(4000)
DECLARE @NVR_SQL13 AS NVARCHAR(4000)
DECLARE @NVR_SQL14 AS NVARCHAR(4000)
DECLARE @NVR_SQL15 AS NVARCHAR(4000)
DECLARE @NVR_SQL16 AS NVARCHAR(4000)
DECLARE @NVR_SQL17 AS NVARCHAR(4000)
DECLARE @NVR_SQL18 AS NVARCHAR(4000)
DECLARE @NVR_SQL19 AS NVARCHAR(4000)
DECLARE @NVR_SQL20 AS NVARCHAR(4000)

DECLARE @NVR_DATETYPE AS NVARCHAR(1)
DECLARE @INT_ID AS INT
DECLARE @NVR_GOODSUNIT AS NVARCHAR(32)
DECLARE @INT_UNIT INT
    -----------------------------------------------------------------------------

SELECT @NVR_SQL0='',@INT_ID=0,@NVR_SQL0='',@NVR_SQL1='',@NVR_SQL2='',@NVR_SQL3='',@NVR_SQL4='',@NVR_SQL5='',@NVR_SQL6='',@NVR_SQL7='',@NVR_SQL8='',@NVR_SQL9='',
@NVR_SQL10='',@NVR_SQL11='',@NVR_SQL12='',@NVR_SQL13='',@NVR_SQL14='',@NVR_SQL15='',@NVR_SQL16='',@NVR_SQL17='',@NVR_SQL18='',@NVR_SQL19='',@NVR_SQL20=''
 
DECLARE @INT_I INT
DECLARE @NVR_FLDNAME NVARCHAR(128)
SET @INT_I=0
WHILE (1=1) BEGIN
SET @INT_I=@INT_I+1
SELECT @NVR_FLDNAME= DBO.FUN_SplitStr(@NVR_YCOL,@INT_I,',') -- 以逗号为分隔
SET @NVR_FLDNAME=LTRIM(RTRIM(ISNULL(@NVR_FLDNAME,'')))
IF @NVR_FLDNAME <>'' BEGIN
SET @NVR_SQL0 =@NVR_SQL0 +  @NVR_FLDNAME + ','
END ELSE BEGIN
SET @NVR_SQL0 = ' SELECT ' + LEFT(@NVR_SQL0 ,LEN(@NVR_SQL0)-1)
BREAK -- 结束拆分
END
END
 

--=================================================================================
-- 横向合计
DECLARE @NVR_SPTOTAL NVARCHAR(512)
DECLARE @INT_N AS INT
DECLARE @NVR_TOTALSQL  NVARCHAR(512)

SELECT  @INT_N=0,@NVR_TOTALSQL='',@NVR_TOTAL=RTRIM(LTRIM(ISNULL(@NVR_TOTAL,'')))
IF @NVR_TOTAL <>'' BEGIN
WHILE (1=1) BEGIN
    SET @INT_N=@INT_N+1
    SELECT @NVR_SPTOTAL= DBO.FUN_SplitStr(@NVR_TOTAL,@INT_N,',')  -- 以逗号为分隔
    SET @NVR_SPTOTAL=LTRIM(RTRIM(ISNULL(@NVR_SPTOTAL,'')))
    IF  @NVR_SPTOTAL <>'' BEGIN
        IF @NVR_SPTOTAL='SUM' BEGIN
            SET @NVR_TOTALSQL=@NVR_TOTALSQL + ',' + @NVR_SPTOTAL + '(' + @NVR_STATCOL + ') AS [SUM]'
        END ELSE IF @NVR_SPTOTAL='AVG' BEGIN
            SET @NVR_TOTALSQL=@NVR_TOTALSQL + ',' + @NVR_SPTOTAL + '(' + @NVR_STATCOL + ') AS [AVG]'
END ELSE IF @NVR_SPTOTAL='MAX' BEGIN
            SET @NVR_TOTALSQL=@NVR_TOTALSQL + ',' + @NVR_SPTOTAL + '(' + @NVR_STATCOL + ') AS [MAX]'
END ELSE IF @NVR_SPTOTAL='MIN' BEGIN
            SET @NVR_TOTALSQL=@NVR_TOTALSQL + ',' + @NVR_SPTOTAL + '(' + @NVR_STATCOL + ') AS [MIN]'
        END
    END ELSE BEGIN
        BREAK
    END
END
SET @NVR_SQL0=@NVR_SQL0 + @NVR_TOTALSQL
END

--===============================================================================

SET @NVR_CMD='DECLARE CORSS_CURSOR CURSOR FOR SELECT DISTINCT ' + @NVR_XCOL + '  FROM ' + @NVR_TABNAME + ' ' + @NVR_WHERE + ' ORDER BY ' + @NVR_XCOL + ' FOR READ ONLY ' --生成游标


EXECUTE (@NVR_CMD)
OPEN CORSS_CURSOR

WHILE (1=1)  BEGIN
FETCH NEXT FROM CORSS_CURSOR INTO @NVR_XCOLNAME  --遍历游标,将列头信息放入变量@NVR_XCOLNAME
IF (@@FETCH_STATUS <>0) BREAK

SET @INT_ID=@INT_ID+1
SET @NVR_XCOLNAME=REPLACE(@NVR_XCOLNAME ,CHAR(39),CHAR(39)+CHAR(39))

------------------------------------------------------------------------------
-- 可添加 数量,单价,金额 同时显示.单价= ( 金额 / 数量) .
IF @INT_ID <=50
    SET @NVR_SQL1 = @NVR_SQL1 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF @INT_ID <=100
    SET @NVR_SQL2 = @NVR_SQL2 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=150
    SET @NVR_SQL3 = @NVR_SQL3 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=200
    SET @NVR_SQL4 = @NVR_SQL4 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=250
    SET @NVR_SQL5 = @NVR_SQL5 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=300
    SET @NVR_SQL6 = @NVR_SQL6 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=350
    SET @NVR_SQL7 = @NVR_SQL7 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=400
    SET @NVR_SQL8 = @NVR_SQL8 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=450
    SET @NVR_SQL9 = @NVR_SQL9 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=500
    SET @NVR_SQL10 = @NVR_SQL10 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=550
    SET @NVR_SQL11 = @NVR_SQL11 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=600
    SET @NVR_SQL12 = @NVR_SQL12 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=650
    SET @NVR_SQL13 = @NVR_SQL13 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=700
    SET @NVR_SQL14 = @NVR_SQL14 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=750
    SET @NVR_SQL15 = @NVR_SQL15 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=800
    SET @NVR_SQL16 = @NVR_SQL16 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=850
    SET @NVR_SQL17 = @NVR_SQL17 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=900
    SET @NVR_SQL18 = @NVR_SQL18 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=950
    SET @NVR_SQL19 = @NVR_SQL19 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=1000
    SET @NVR_SQL20 = @NVR_SQL20 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
END -- WHILE (1 = 1)

--直接返回结果.
IF @NVR_ORDERBY='' BEGIN  --未指定排序字段.
EXECUTE ( @NVR_SQL0 + @NVR_SQL1 + @NVR_SQL2  + @NVR_SQL3 + @NVR_SQL4 + @NVR_SQL5 + @NVR_SQL6 + @NVR_SQL7 + @NVR_SQL8 + @NVR_SQL9
+ @NVR_SQL10 + @NVR_SQL11 + @NVR_SQL12  + @NVR_SQL13 + @NVR_SQL14 + @NVR_SQL15 + @NVR_SQL16 + @NVR_SQL17 + @NVR_SQL18 + @NVR_SQL19 + @NVR_SQL20
+ N' FROM ' + @NVR_TABNAME + N' ' + @NVR_WHERE + N' GROUP BY ' + @NVR_YCOL )                                                -- 执行
END ELSE BEGIN            --指定排序字段.
EXECUTE (N'SELECT * FROM (' +  @NVR_SQL0 + @NVR_SQL1 + @NVR_SQL2  + @NVR_SQL3 + @NVR_SQL4 + @NVR_SQL5 + @NVR_SQL6 + @NVR_SQL7 + @NVR_SQL8 + @NVR_SQL9
+ @NVR_SQL10 + @NVR_SQL11 + @NVR_SQL12  + @NVR_SQL13 + @NVR_SQL14 + @NVR_SQL15 + @NVR_SQL16 + @NVR_SQL17 + @NVR_SQL18 + @NVR_SQL19 + @NVR_SQL20
+ N' FROM ' + @NVR_TABNAME + N' ' + @NVR_WHERE + N' GROUP BY ' + @NVR_YCOL  + N') AS T86C6S9R ORDER BY ' + @NVR_ORDERBY )  -- 执行
END

CLOSE CORSS_CURSOR
DEALLOCATE CORSS_CURSOR
RETURN 0 --释放游标,返回0表示成功
END

--
--下面用海关数据仓库的实例表,测试
--
EXECUTE PRC_CORSSQUERY_A 'DAT_2007','海关编号','商品编号,国家编号,地区行政编号','金额','WHERE 时间编号 BETWEEN 20070401 AND 20070401  AND 商品编号=''07131010_09'''

商品编号                    国家编号 地区行政编号 22                                                    37                                                    51                                                    52                                                    95                                                    97                                                   
------------------------ ---- ------ ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- -----------------------------------------------------
07131010_09              111  310000 2156.0                                                NULL                                                  NULL                                                  NULL                                                  NULL                                                  NULL
07131010_09              116  350200 NULL                                                  0.0                                                  NULL                                                  NULL                                                  NULL                                                  NULL
07131010_09              116  620100 NULL                                                  NULL                                                  NULL                                                  NULL                                                  0.0                                                  NULL
07131010_09              116  630100 NULL                                                  NULL                                                  NULL                                                  NULL                                                  NULL                                                  0.0
07131010_09              143  350200 NULL                                                  4669.0                                                NULL                                                  NULL                                                  NULL                                                  NULL
07131010_09              143  440100 NULL                                                  NULL                                                  6190.0                                                14400.0                                              NULL                                                  NULL
07131010_09              305  620100 NULL                                                  NULL                                                  NULL                                                  NULL                                                  219.0                                                NULL
07131010_09              502  440100 NULL                                                  NULL                                                  0.0                                                  NULL                                                  NULL                                                  NULL
07131010_09              601  440100 NULL                                                  NULL                                                  19185.0                                              NULL                                                  NULL                                                  NULL

(所影响的行数为 9 行)


每天都看到论坛里有人问 行/列 转换的问题,上面的过程是从实例中修改过来的.应该可以满足大部分的数据旋转要求.

不知道执行起来速度怎么样

==============================

我在产业数据库的数据库版,海关数据的数据库版及其它一些大型库的数据库版 用的都是这个过程.数据量一般有 2亿到5亿之间.
有些库有十二个维度左右.只要建立了合理的索引.速度是没有问题的.虽然看起来长,但都是原生的SQL语句.


--- 改一下过程,由于修改时没有将一些多余的语句去了.
-------------------------------------------
--行列转换
-------------------------------------------
CREATE  PROC PRC_CORSSQUERY
@NVR_TABNAME AS NVARCHAR(1024) = '', -- 此处放表名
@NVR_XCOL AS NVARCHAR(1024) = '', -- 表头分组依据字段 (横向字段,以系统编号作为依据,内部转化,此字段只能有一个)
@NVR_YCOL AS NVARCHAR(1024) = '', -- 分组字段        (纵向字段,直接取名称列,可以有多个,多个用逗号分隔)
@NVR_STATCOL AS NVARCHAR(1024) = '', -- 被统计的字段    (值)
@NVR_WHERE AS NVARCHAR(4000)='', -- WHERE条件  (一定要带 WHERE 语句,如:WHERE 地区=''广州'' )  * 注意字符一定要用两个分号.
@NVR_TOTAL AS NVARCHAR(1024)='', -- 合计的公式( SUM,AVG ,将其放在第一列.如果多项,请用逗号分隔)
@NVR_ORDERBY AS NVARCHAR(1024)=''          -- 排序字段,如: [海关编码],[地区]
AS BEGIN

    --SET NOCOUNT ON

DECLARE @NVR_CMD AS NVARCHAR(4000)
DECLARE @NVR_XCOLNAME AS NVARCHAR(256)

DECLARE @NVR_SQL0 AS NVARCHAR(4000)
DECLARE @NVR_SQL1 AS NVARCHAR(4000)
DECLARE @NVR_SQL2 AS NVARCHAR(4000)
DECLARE @NVR_SQL3 AS NVARCHAR(4000)
DECLARE @NVR_SQL4 AS NVARCHAR(4000)
DECLARE @NVR_SQL5 AS NVARCHAR(4000)
DECLARE @NVR_SQL6 AS NVARCHAR(4000)
DECLARE @NVR_SQL7 AS NVARCHAR(4000)
DECLARE @NVR_SQL8 AS NVARCHAR(4000)
DECLARE @NVR_SQL9 AS NVARCHAR(4000)
DECLARE @NVR_SQL10 AS NVARCHAR(4000)
DECLARE @NVR_SQL11 AS NVARCHAR(4000)
DECLARE @NVR_SQL12 AS NVARCHAR(4000)
DECLARE @NVR_SQL13 AS NVARCHAR(4000)
DECLARE @NVR_SQL14 AS NVARCHAR(4000)
DECLARE @NVR_SQL15 AS NVARCHAR(4000)
DECLARE @NVR_SQL16 AS NVARCHAR(4000)
DECLARE @NVR_SQL17 AS NVARCHAR(4000)
DECLARE @NVR_SQL18 AS NVARCHAR(4000)
DECLARE @NVR_SQL19 AS NVARCHAR(4000)
DECLARE @NVR_SQL20 AS NVARCHAR(4000)

DECLARE @NVR_DATETYPE AS NVARCHAR(1)
DECLARE @INT_ID AS INT
DECLARE @NVR_GOODSUNIT AS NVARCHAR(32)
DECLARE @INT_UNIT INT

SELECT @NVR_SQL0='',@INT_ID=0,@NVR_SQL0='',@NVR_SQL1='',@NVR_SQL2='',@NVR_SQL3='',@NVR_SQL4='',@NVR_SQL5='',@NVR_SQL6='',@NVR_SQL7='',@NVR_SQL8='',@NVR_SQL9='',
@NVR_SQL10='',@NVR_SQL11='',@NVR_SQL12='',@NVR_SQL13='',@NVR_SQL14='',@NVR_SQL15='',@NVR_SQL16='',@NVR_SQL17='',@NVR_SQL18='',@NVR_SQL19='',@NVR_SQL20=''
 
DECLARE @INT_I INT
DECLARE @NVR_FLDNAME NVARCHAR(128)

SET @NVR_SQL0 = ' SELECT ' +  @NVR_YCOL
-- 横向合计
DECLARE @NVR_SPTOTAL NVARCHAR(512)
DECLARE @INT_N AS INT
DECLARE @NVR_TOTALSQL  NVARCHAR(512)

SELECT  @INT_N=0,@NVR_TOTALSQL='',@NVR_TOTAL=RTRIM(LTRIM(ISNULL(@NVR_TOTAL,'')))
IF @NVR_TOTAL <>'' BEGIN
WHILE (1=1) BEGIN
    SET @INT_N=@INT_N+1
    SELECT @NVR_SPTOTAL= DBO.FUN_SplitStr(@NVR_TOTAL,@INT_N,',')  -- 以逗号为分隔
    SET @NVR_SPTOTAL=LTRIM(RTRIM(ISNULL(@NVR_SPTOTAL,'')))
    IF  @NVR_SPTOTAL <>'' BEGIN
        IF @NVR_SPTOTAL='SUM' BEGIN
            SET @NVR_TOTALSQL=@NVR_TOTALSQL + ',' + @NVR_SPTOTAL + '(' + @NVR_STATCOL + ') AS [SUM]'
        END ELSE IF @NVR_SPTOTAL='AVG' BEGIN
            SET @NVR_TOTALSQL=@NVR_TOTALSQL + ',' + @NVR_SPTOTAL + '(' + @NVR_STATCOL + ') AS [AVG]'
END ELSE IF @NVR_SPTOTAL='MAX' BEGIN
            SET @NVR_TOTALSQL=@NVR_TOTALSQL + ',' + @NVR_SPTOTAL + '(' + @NVR_STATCOL + ') AS [MAX]'
END ELSE IF @NVR_SPTOTAL='MIN' BEGIN
            SET @NVR_TOTALSQL=@NVR_TOTALSQL + ',' + @NVR_SPTOTAL + '(' + @NVR_STATCOL + ') AS [MIN]'
        END
    END ELSE BEGIN
        BREAK
    END
END
SET @NVR_SQL0=@NVR_SQL0 + @NVR_TOTALSQL
END

SET @NVR_CMD='DECLARE CORSS_CURSOR CURSOR FOR SELECT DISTINCT ' + @NVR_XCOL + '  FROM ' + @NVR_TABNAME + ' ' + @NVR_WHERE + ' ORDER BY ' + @NVR_XCOL + ' FOR READ ONLY ' --生成游标


EXECUTE (@NVR_CMD)
OPEN CORSS_CURSOR

WHILE (1=1)  BEGIN
FETCH NEXT FROM CORSS_CURSOR INTO @NVR_XCOLNAME  --遍历游标,将列头信息放入变量@NVR_XCOLNAME
IF (@@FETCH_STATUS <>0) BREAK

SET @INT_ID=@INT_ID+1
SET @NVR_XCOLNAME=REPLACE(@NVR_XCOLNAME ,CHAR(39),CHAR(39)+CHAR(39))

------------------------------------------------------------------------------
-- 可添加 数量,单价,金额 同时显示.单价= ( 金额 / 数量) .
IF @INT_ID <=50
    SET @NVR_SQL1 = @NVR_SQL1 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF @INT_ID <=100
    SET @NVR_SQL2 = @NVR_SQL2 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=150
    SET @NVR_SQL3 = @NVR_SQL3 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=200
    SET @NVR_SQL4 = @NVR_SQL4 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=250
    SET @NVR_SQL5 = @NVR_SQL5 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=300
    SET @NVR_SQL6 = @NVR_SQL6 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=350
    SET @NVR_SQL7 = @NVR_SQL7 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=400
    SET @NVR_SQL8 = @NVR_SQL8 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=450
    SET @NVR_SQL9 = @NVR_SQL9 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=500
    SET @NVR_SQL10 = @NVR_SQL10 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=550
    SET @NVR_SQL11 = @NVR_SQL11 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=600
    SET @NVR_SQL12 = @NVR_SQL12 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=650
    SET @NVR_SQL13 = @NVR_SQL13 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=700
    SET @NVR_SQL14 = @NVR_SQL14 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=750
    SET @NVR_SQL15 = @NVR_SQL15 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=800
    SET @NVR_SQL16 = @NVR_SQL16 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=850
    SET @NVR_SQL17 = @NVR_SQL17 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=900
    SET @NVR_SQL18 = @NVR_SQL18 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=950
    SET @NVR_SQL19 = @NVR_SQL19 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
ELSE IF  @INT_ID <=1000
    SET @NVR_SQL20 = @NVR_SQL20 + ',SUM(CASE ' + @NVR_XCOL + ' WHEN ''' + @NVR_XCOLNAME + ''' THEN ' + @NVR_STATCOL + ' ELSE NULL END) AS [' + @NVR_XCOLNAME + ']' --构造查询
END -- WHILE (1 = 1)

 

--直接返回结果.
IF @NVR_ORDERBY='' BEGIN  --未指定排序字段.
EXECUTE ( @NVR_SQL0 + @NVR_SQL1 + @NVR_SQL2  + @NVR_SQL3 + @NVR_SQL4 + @NVR_SQL5 + @NVR_SQL6 + @NVR_SQL7 + @NVR_SQL8 + @NVR_SQL9
+ @NVR_SQL10 + @NVR_SQL11 + @NVR_SQL12  + @NVR_SQL13 + @NVR_SQL14 + @NVR_SQL15 + @NVR_SQL16 + @NVR_SQL17 + @NVR_SQL18 + @NVR_SQL19 + @NVR_SQL20
+ N' FROM ' + @NVR_TABNAME + N' ' + @NVR_WHERE + N' GROUP BY ' + @NVR_YCOL )                                                -- 执行
END ELSE BEGIN            --指定排序字段.
EXECUTE (N'SELECT * FROM (' +  @NVR_SQL0 + @NVR_SQL1 + @NVR_SQL2  + @NVR_SQL3 + @NVR_SQL4 + @NVR_SQL5 + @NVR_SQL6 + @NVR_SQL7 + @NVR_SQL8 + @NVR_SQL9
+ @NVR_SQL10 + @NVR_SQL11 + @NVR_SQL12  + @NVR_SQL13 + @NVR_SQL14 + @NVR_SQL15 + @NVR_SQL16 + @NVR_SQL17 + @NVR_SQL18 + @NVR_SQL19 + @NVR_SQL20
+ N' FROM ' + @NVR_TABNAME + N' ' + @NVR_WHERE + N' GROUP BY ' + @NVR_YCOL  + N') AS T86C6S9R ORDER BY ' + @NVR_ORDERBY )  -- 执行
END

CLOSE CORSS_CURSOR
DEALLOCATE CORSS_CURSOR
RETURN 0 --释放游标,返回0表示成功
END

GO

分享到:
评论

相关推荐

    arcgis工具

    XY 数据表,指定X 坐标字段(东经)和Y 坐标字段(北纬),按“编辑”按钮,选择坐标系统。一般GPS系统采用的坐标系统为WGS_1984。 方法二.直接用TOOL工具下的ADD XY 可以单个加点 17. 用任意多边形剪切栅格数据...

    第6章 数据库的存储结构.ppt

    辅助索引则用于非唯一字段,如多重索引、位图索引等,它们可以加速对多个字段的查询。 **五、B树与B+树** B树和B+树都是平衡多路搜索树,用于索引。B树每个节点可以有多个子节点,数据存储在节点内,而B+树所有数据...

    带有曲折字形的小型高架存储室MDS代码-分布式存储的可解码属性

    在文件描述中提到的二进制字段操作是将MDS代码应用于二进制域,以达到低复杂度的编码和解码过程。 接着,循环移位是数字信号处理中的一个常见操作,它将序列中的一组元素按照固定的位置进行旋转。在本文件中,循环...

    EssentialPIM Pro 8.12.1 个人信息管理专家 密码管理器 笔记管理 日程管理

    您可以安排层次结构中的任务,将它们分成标签,按任意字段排序,分配不同的优先级和类别等。Notes提供了经典的文本编辑工具,可以按层次结构进行组织,将它们分成单独的树,甚至可以在桌面上制作便签。联系人功能...

    B超工作站超声影像XP随心所欲软件免费下载

    光盘刻录后,系统内自动生成一个索引字段,记录图像和数据存储位置。在打开病人光盘信息时能自动提示存储的光盘号。  用户权限管理:能够开发新用户及删除用户,不同用户对病人基本信息、图像及报告内容进行修改、...

    析Android中的Serializable序列化.rar_Serializable _android

    4. 序列化小技巧:为了节省存储空间和提高效率,可以使用 transient 关键字标记那些不需要序列化的字段。 五、替代方案 虽然Serializable是常用的序列化方式,但在Android中,还有其他序列化库,如Parcelable接口,...

    计算机专用英语词汇集合

    45. **Drum**: 鼓存储器是一种早期的计算机存储设备,使用旋转的圆筒体来存储数据。 46. **Electronics**: 电子学是一门研究电子器件和电路原理的技术学科。 47. **Emitter**: 发射器是一种电子设备,用于发射信号...

    2021-2022计算机二级等级考试试题及答案No.10592.docx

    - **字段作用**:数据字段用于存储对象的状态信息。 - **正确答案**:正确 --- #### 18. 视图菜单功能 - **知识点解析**: - **视图菜单**:视图菜单通常包含一系列用于控制窗口显示方式的选项,如显示网格线...

    2023年9月计算机二级MSOffice考试题.docx

    9. 线性表存储结构:链式存储结构通常需要比顺序存储结构更多的存储空间,因为链式结构的每个元素都需要额外的指针字段来链接下一个元素。 10. 循环队列:循环队列中,队头指针可能大于或小于队尾指针,取决于元素...

    助成计算机考研之北航计算机专业复试基础.pdf

    三、二维数组旋转:空间复杂度为O(1)的算法可以使用四个指针,分别指向四个角落,依次交换元素实现旋转。 四、二叉树深度算法:递归算法可以利用二叉树的性质,递归计算左右子树的深度,然后返回最大深度加1。 五...

    计算机英语

    - **Drum (鼓式存储)**:早期的一种旋转磁存储设备,类似于磁带。 - **To Edit (编辑)**:修改或调整文档、图像或音频等文件的过程。 - **Electronics (电子学)**:研究电子设备和技术的科学领域。 - **Emitter ...

    Android应用源码之(Body的m_userData).zip

    这个字段的命名暗示它可能是用来存储非结构化的、与用户相关的任意数据。 在Android的`Body`类中,`m_userData`可能会有以下几种常见用法: 1. **数据持久化**:如果`m_userData`是用来存储用户数据,那么可能会...

    东财20秋《网页制作:Flash动画制作》单元作业三答卷.docx

    Flash的工作环境主要包括舞台(用于放置和编辑内容)、库(存储资源的地方)、工作区(编辑区域)以及各种面板,如信息面板、变形面板、对齐面板等。对齐面板可以帮助将舞台中的对象排列整齐。动作补间动画是基于...

    Delphi7编程100例

    使用远程存储过程 数据模块的同步显示 建立Web服务的数据提供端 建立Web服务的数据访问端 开发WebSnap数据库程序 建立ActiveForm数据浏览 建立数据查询WebServices服务器端 数据查询Web服务客户端...

    Delphi编程100例

    使用远程存储过程 数据模块的同步显示 建立Web服务的数据提供端 建立Web服务的数据访问端 开发WebSnap数据库程序 建立ActiveForm数据浏览 建立数据查询WebServices服务器端 数据查询Web服务客户端开发 基于WAP的手机...

    《Delphi7编程100例》代码

    ToolBar工具栏控件的使用动态建立主菜单选项窗口界面的动态分隔条动态设置...旋转奇妙的拼图游戏使用PaintBox控件制作画图程序使用DrawGrid控件制作五子棋多彩的数据报表按压缩比将BMP转换为JPG16位真彩转换到256色调整...

    9熵的性质1

    通过对数据集的熵分析,可以确定哪些字段的信息密度高,从而优化存储和检索策略,提高搜索性能。 总的来说,熵是一个衡量信息不确定性和复杂性的强大工具,理解和应用熵的性质可以帮助我们更好地理解和处理各种信息...

    MD5加密算法

    它将任意长度的数据转化为一个固定长度的摘要,通常是128位,以16进制表示就是32个字符。MD5的主要应用在于数据的完整性校验和密码存储。 在VC6.0环境下,开发MD5加密算法的程序通常会涉及到以下几个关键知识点: ...

    计算机组成原理复习卷2022

    19. 水平型微指令的特点是微指令格式宽,包含多个并行执行的微操作控制信号,直接编码、字段直接编码、字段间接编码以及直接和字段混合编码都属于水平型微指令。 20. DMA(直接存储器访问)访问主存时,通过周期...

Global site tag (gtag.js) - Google Analytics