我试图在Oracle中动态重命名列 我有一个表,其列以月份名称命名,每个月我需要刷新数据并相应地更改列名。
DECLARE
v_cur_month_name VARCHAR2(100);
i INT := 0;
vsql VARCHAR2(250);
BEGIN
FOR my_tab IN (WITH month_counter AS
(SELECT column_name, rownum AS cnt
FROM all_tab_columns
WHERE lower(owner) = 'ppl'
AND lower(table_name) = 'test')
SELECT column_name AS MONTH, cnt FROM month_counter WHERE rownum <= 12) LOOP
v_cur_month_name := to_char(to_date(extract(MONTH FROM to_date(to_char(add_months(SYSDATE, i), 'YYYY-MM-DD'), 'YYYY-MM-DD')), 'MM'), 'MONTH');
vsql := 'alter table ppl.test rename column' || my_tab.month || 'TO' || v_cur_month_name;
EXECUTE IMMEDIATE vsql;
i := i + 1;
END LOOP;
END;
对于此:我收到错误:
ORA-14155:缺少PARTITION,PARTITIONS,SUBPARTITION或SUBPARTITIONS关键字
ORA-06512:第20行
答案 0 :(得分:1)
您需要添加空格才能获得正确的语法:
vsql := 'alter table ppl.test rename column' || my_tab.month || 'TO' || v_cur_month_name;
=>
vsql := 'alter table ppl.test rename column ' || my_tab.month || ' TO ' || v_cur_month_name;
<强> DBFiddle Demo 强>
无论如何,动态列重命名表示设计不佳。