我正在尝试将列标题和相应字段插入另一个表中。
Table1 :
col1 col2 col3 col4
1 2 3 4
输出应如下所示:
COL_A COL_B COL_C COL_D COL_E COL_F COL_G COL_H
col1 1 col2 2 col3 3 col4 4
表1将动态创建,因此表1的结构可能会有所不同,例如。 有时,可能是
Case1:
col1 col2 col3 col4
1 2 3 4
对于Case1,输出应如下所示:
COL_A COL_B COL_C COL_D COL_E COL_F COL_G COL_H
col1 1 col2 2 col3 3 col4 4
或者它可能是 情况2:
col1 col2
1 2
对于Case2,输出应如下所示:
COL_A COL_B COL_C COL_D
col1 1 col2 2
我在Stack Overflow的另一个问题中得到了类似要求的静态查询的帮助,但我试图将静态查询转换为动态查询。 静态查询:
静态表结构:
CREATE TABLE Table1
(col1 int, col2 int, col3 int, col4 int)
;
INSERT ALL
INTO Table1 (col1, col2, col3, col4)
VALUES (1, 2, 3, 4)
SELECT * FROM dual
;
静态查询:
SELECT * FROM
(
SELECT *
FROM Table1
UNPIVOT(val FOR col IN (
COL1
,COL2
,COL3
,COL4
))
)
PIVOT( MAX(COl) as C, MAX(VAL) as V FOR COL IN (
'COL1' as VAL1
,'COL2' as VAL2
,'COL3' as VAL3
,'COL4' as VAL4
));
输出:
VAL1_C VAL1_V VAL2_C VAL2_V VAL3_C VAL3_V VAL4_C VAL4_V
COL1 1 COL2 2 COL3 3 COL4 4
当我试图使静态查询动态化时,我尝试了这个:
SELECT * FROM
(
SELECT *
FROM Table1
UNPIVOT(val FOR col IN (
select regexp_substr((select * from (SELECT LISTAGG(COLUMN_NAME, ',') WITHIN GROUP ( order by COLUMN_ID) aa
FROM (select * from all_tab_columns where table_name = 'TABLE1'))),'[^,]+', 1, level) "yyy" from dual
connect by regexp_substr((select * from (SELECT LISTAGG(COLUMN_NAME, ',') WITHIN GROUP ( order by COLUMN_ID) aa
FROM (select * from all_tab_columns where table_name = 'TABLE1'))), '[^,]+', 1, level) is not null
))
)
PIVOT( MAX(COl) as C, MAX(VAL) as V FOR COL IN (
select regexp_substr((select '''' || AA ||'''' from (SELECT LISTAGG(COLUMN_NAME, ''',''') WITHIN GROUP ( order by COLUMN_ID) aa
FROM (select * from all_tab_columns where table_name = 'TABLE1'))),'[^,]+', 1, level) "yyy" from dual
connect by regexp_substr((select '''' || AA ||'''' from (SELECT LISTAGG(COLUMN_NAME, ''',''') WITHIN GROUP ( order by COLUMN_ID) aa
FROM (select * from all_tab_columns where table_name = 'TABLE1'))), '[^,]+', 1, level) is not null
));
但这引发了错误:
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 6 Column: 5
我尝试使用动态sql .Below是我使用的动态sql:
declare
sql_stmt varchar2(4000);
pivot_clause1 varchar2(4000);
pivot_clause2 varchar2(4000);
begin
SELECT LISTAGG(COLUMN_NAME, ',') WITHIN GROUP ( order by COLUMN_ID) aa
into pivot_clause1
FROM (select * from all_tab_columns where table_name = 'TEST11') ;
select * into pivot_clause2 from
(
select AA ||'''' aa
from (SELECT ''''||LISTAGG(COLUMN_NAME, ''',''') WITHIN GROUP ( order by COLUMN_ID) aa
FROM (select * from all_tab_columns where table_name = 'TEST11'))
);
dbms_output.put_line(pivot_clause1);
dbms_output.put_line(pivot_clause2);
sql_stmt := 'create table test13 as ('|| 'SELECT * FROM
(
SELECT *
FROM TEST11
UNPIVOT(val FOR col IN ( '
|| pivot_clause1 || '
))
)
PIVOT( MAX(COl) as C, MAX(VAL) as V FOR COL IN (' ||
pivot_clause2 || '
))
)';
execute immediate sql_stmt ;
end;
/
对于具有以下描述的表格罚款:
Name Null Type
---- ---- ----------
COL1 NUMBER(38)
COL2 NUMBER(38)
COL3 NUMBER(38)
COL4 NUMBER(38)
但不适用于具有说明的表格
COL1 DATE
COL1 VARCHAR2(100)
COL1 VARCHAR2(100)
COL1 NUMBER
COL1 NUMBER
我收到以下错误:
Error report -
ORA-01790: expression must have same datatype as corresponding expression
ORA-06512: at line 38
01790. 00000 - "expression must have same datatype as corresponding expression"
*Cause:
*Action:
我刚学会 unpivot不能与具有不同数据类型的列一起使用,有什么办法吗? 有人可以帮忙!!