1

https://www.itjungle.com/2015/04/21/fhg042115-story01/下可以找到一篇关于如何编写通用 Db2 数据透视方法的好文章。

执行以下代码(注意 'end #' 而不是 'end ;'):

SET SCHEMA = TESTSCHEMA;

CREATE PROCEDURE DO_PIVOT
(IN FOR_SCHEMA CHARACTER (10) , 
IN FOR_TABLE CHARACTER (10) , 
IN PIVOT_COLUMN VARCHAR (250) , 
IN VALUE_COLUMN VARCHAR (250) , 
IN AGG_FUNCTION VARCHAR (5) DEFAULT 'SUM' , 
IN GROUP_COLUMN VARCHAR (250) DEFAULT NULL ) 
LANGUAGE SQL 
MODIFIES SQL DATA 
PROGRAM TYPE SUB 
CONCURRENT ACCESS RESOLUTION DEFAULT 
DYNAMIC RESULT SETS 1 
OLD SAVEPOINT LEVEL COMMIT ON RETURN NO 

BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0 ;
DECLARE SQL_STATEMENT VARCHAR ( 5000 ) ;
DECLARE PIVOT_VALUE VARCHAR ( 20 ) ;
DECLARE PAD CHAR ( 2 ) DEFAULT ' ' ;
DECLARE C1 CURSOR FOR D1 ;
DECLARE C2 CURSOR WITH RETURN FOR D2 ;

SET SCHEMA = FOR_SCHEMA ;

-- Get the list of values available for the pivot column

-- Each value will be a column in the return set
SET SQL_STATEMENT = 'select distinct ' 
                   || PIVOT_COLUMN  
                   || ' from ' 
                   || FOR_TABLE 
                   || ' order by 1' ;

PREPARE D1 FROM SQL_STATEMENT ;
OPEN C1 ;

-- Construct a dynamic select statement for the pivot
SET SQL_STATEMENT = 'select ' ;

-- If requested, add the Group By Column 
-- to the select clause
IF GROUP_COLUMN IS NOT NULL THEN
  SET SQL_STATEMENT = SQL_STATEMENT || GROUP_COLUMN ;
  SET PAD = ', ' ;
END IF ;

-- For each possible value for the Pivot Column, 
-- add a case statement to perform the requested 
-- aggregate function on the Value Column
FETCH NEXT FROM C1 INTO PIVOT_VALUE ;
WHILE ( SQLCODE >= 0 AND SQLCODE <> 100 ) DO
  SET SQL_STATEMENT = SQL_STATEMENT 
                      || PAD 
                      || AGG_FUNCTION 
                      || '(CASE WHEN ' 
                      || PIVOT_COLUMN 
                      || ' = ''' 
                      || PIVOT_VALUE 
                      || ''' THEN ' 
                      || VALUE_COLUMN 
                      || '  END) AS ' 
                      || PIVOT_VALUE ;
  SET PAD = ', ' ;
  FETCH NEXT FROM C1 INTO PIVOT_VALUE ;
END WHILE ;
CLOSE C1 ;

-- Specify the table to select from
SET SQL_STATEMENT = SQL_STATEMENT 
                    || ' from ' 
                    || FOR_TABLE ;

-- If requested, add the Group By Column
-- to the select clause
IF GROUP_COLUMN IS NOT NULL THEN
  SET SQL_STATEMENT = SQL_STATEMENT 
                      || ' group by ' 
                      || GROUP_COLUMN 
                      || ' order by ' 
                      || GROUP_COLUMN;
END IF ;

PREPARE D2 FROM SQL_STATEMENT ;
OPEN C2 ;

END #

LABEL ON ROUTINE DO_PIVOT 
( CHAR(), CHAR(), VARCHAR(), VARCHAR(), VARCHAR(), VARCHAR() )  
IS 'Perform a General Purpose Pivot';

COMMENT ON PARAMETER ROUTINE DO_PIVOT 
( CHAR(), CHAR(), VARCHAR(), VARCHAR(), VARCHAR(), VARCHAR() ) 
(FOR_SCHEMA IS 'Schema for Table' , 
FOR_TABLE IS 'For Table' , 
PIVOT_COLUMN IS 'Name of Column to be Pivoted' , 
VALUE_COLUMN IS 'Column to be Aggregated for Pivot' , 
AGG_FUNCTION IS 'Use Aggregate Function' , 
GROUP_COLUMN IS 'Group on Column' ) ;

产生错误消息:

DB21034E  The command was processed as an SQL statement because it was
not a valid Command Line Processor command.  During SQL processing it
returned:
SQL0104N  An unexpected token "FROM" was found following "    FETCH
NEXT".
Expected tokens may include:  ".".  LINE NUMBER=50.  SQLSTATE=42601

有什么建议么?

4

1 回答 1

1

对于 Db2-LUW v11.1 及更高版本,要编译 sproc,您可以进行下面列出的更改。这只是为了获得成功的编译。我没有检查功能。

  • 删除(或注释掉)PROGRAM TYPE SUB

  • 删除(或注释掉)CONCURRENT ACCESS RESOLUTION DEFAULT

  • 将 FETCH NEXT FROM 更改为 FETCH(在两个地方),或使用 FETCH FROM

  • 删除(或注释掉)LABEL ON ROUTINE 语句

  • 删除(或注释掉)COMMENT ON PARAMETER ROUTINE 语句

于 2018-04-17T07:17:28.453 回答