您要么必须对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