0

你能帮我解决这个错误吗?一切正常,但我对 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"

谢谢。

4

2 回答 2

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

或在过程中执行:

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
于 2013-03-22T08:25:46.893 回答
0

AGG_COLUMNS应该对应于以下格式:

Value1 as ColumnName1, Value2 as ColumnName2, ...
于 2013-03-22T07:07:45.867 回答