0

このエラーを解決するのを手伝ってくれませんか。すべて正常に動作しますが、変数を使用して値を渡したいピボット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

または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
于 2013-03-22T08:25:46.893 に答える
0

AGG_COLUMNSの形式に対応する必要があります。

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