PIVOT IN子句值作为变量

时间:2013-03-22 06:38:58

标签: plsql oracle11g

你能帮我解决一下这个错误。一切正常,但我对pivot IN子句感到震惊,我希望通过使用变量来传递值。

CREATE OR REPLACE PROCEDURE Sample
(
  INPUT_TABLE_ID IN NUMBER,OUTPUT_TABLE IN VARCHAR2
)
IS
  AGG_COLUMNS VARCHAR2(2000);

 EXECUTE IMMEDIATE 'SELECT COLUMNS FROM COLUMN_NAMES' INTO AGG_COLUMNS;
 EXECUTE IMMEDIATE      
      'CREATE TABLE '||OUTPUT_TABLE||' AS
      (
          SELECT *
          FROM BW_COLUMN_ROW_CELL_JOIN
          PIVOT
          (
            MAX(STRING_VALUE)
            FOR COLUMN_NAME  IN ( '||AGG_COLUMNS ||')
          )
      )';

错误是:

00936. 00000 -  "missing expression"

感谢。

2 个答案:

答案 0 :(得分:1)

您必须对COLUMN_NAMES表中的值进行编码,如:

SQL> create table  COLUMN_NAMES(COLUMNS  varchar2(20));

Table created.


SQL> insert into column_names values ('''A'',''B''');

1 row created.

SQL>
SQL> create table  BW_COLUMN_ROW_CELL_JOIN(column_name  varchar2(15),
  2                                        string_value  varchar2(15));

Table created.

SQL> insert into   BW_COLUMN_ROW_CELL_JOIN values ('A', 'test');

1 row created.

SQL> insert into   BW_COLUMN_ROW_CELL_JOIN values ('B', 'pickle');

1 row created.

SQL> insert into   BW_COLUMN_ROW_CELL_JOIN values ('C', 'foo');

1 row created.

SQL>
SQL> commit;

Commit complete.

SQL> CREATE OR REPLACE PROCEDURE Sample
  2  (
  3    INPUT_TABLE_ID IN NUMBER,OUTPUT_TABLE IN VARCHAR2
  4  )
  5  IS
  6    AGG_COLUMNS VARCHAR2(2000);
  7  begin
  8   EXECUTE IMMEDIATE 'SELECT COLUMNS FROM COLUMN_NAMES' INTO AGG_COLUMNS;
  9   EXECUTE IMMEDIATE
 10        'CREATE TABLE '||OUTPUT_TABLE||' AS
 11        (
 12            SELECT *
 13            FROM BW_COLUMN_ROW_CELL_JOIN
 14            PIVOT
 15            (
 16              MAX(STRING_VALUE)
 17              FOR COLUMN_NAME  IN ( '||AGG_COLUMNS ||')
 18            )
 19        )';
 20  end;
 21  /

Procedure created.

SQL> exec Sample(1, 'TEST');

PL/SQL procedure successfully completed.

SQL> select * from test;

'A'             'B'
--------------- ---------------
test            pickle

或在proc中执行:

SQL> insert into column_names values ('A,B');

1 row created.

SQL>
SQL> create table  BW_COLUMN_ROW_CELL_JOIN(column_name  varchar2(15),
  2                                        string_value  varchar2(15));

Table created.

SQL> insert into   BW_COLUMN_ROW_CELL_JOIN values ('A', 'test');

1 row created.

SQL> insert into   BW_COLUMN_ROW_CELL_JOIN values ('B', 'pickle');

1 row created.

SQL> insert into   BW_COLUMN_ROW_CELL_JOIN values ('C', 'foo');

1 row created.

SQL>
SQL> commit;

...

SQL> CREATE OR REPLACE PROCEDURE Sample
  2  (
  3    INPUT_TABLE_ID IN NUMBER,OUTPUT_TABLE IN VARCHAR2
  4  )
  5  IS
  6    AGG_COLUMNS VARCHAR2(2000);
  7  begin
  8   EXECUTE IMMEDIATE 'SELECT COLUMNS FROM COLUMN_NAMES' INTO AGG_COLUMNS;
  9   EXECUTE IMMEDIATE
 10        'CREATE TABLE '||OUTPUT_TABLE||' AS
 11        (
 12            SELECT *
 13            FROM BW_COLUMN_ROW_CELL_JOIN
 14            PIVOT
 15            (
 16              MAX(STRING_VALUE)
 17              FOR COLUMN_NAME  IN ( '''||replace(AGG_COLUMNS, ',', ''',''') ||''')
 18            )
 19        )';
 20  end;
 21  /

Procedure created.

SQL> exec Sample(1, 'TEST');

PL/SQL procedure successfully completed.

SQL> select * from test;

'A'             'B'
--------------- ---------------
test            pickle

答案 1 :(得分:0)

您的AGG_COLUMNS应符合以下格式:

Value1 as ColumnName1, Value2 as ColumnName2, ...