您可以通过执行DBMS_METADATA.get_DDL('Table','TABLENAME')
来获取表的DDL。我正在寻找一种方法来更改DDL中表的名称,然后执行DDL以创建一个具有新名称和附加列的等效表。
DECLARE
stmt clob;
BEGIN
SELECT dbms_metadata.get_ddl('TABLE', 'TABLE_NAME') into stmt FROM DUAL;
/*change the name*/
EXECUTE IMMEDIATE(stmt);
END;
答案 0 :(得分:4)
试试这个:
CREATE TABLE XXX as SELECT * FROM YYY WHERE 1 = 0;
答案 1 :(得分:2)
我建议采取以下措施:
DECLARE
stmt clob;
BEGIN
SELECT REPLACE (dbms_metadata.get_ddl('TABLE', 'TABLE_NAME'), 'CREATE TABLE ' || 'TABLE_NAME' , 'CREATE TABLE ' || 'NEW_TABLE_NAME') INTO stmt FROM DUAL;
EXECUTE IMMEDIATE(stmt);
END;
答案 2 :(得分:2)
试试这个:
DECLARE
v_seq NUMBER;
v_orig_ddl CLOB;
BEGIN
v_seq := 13;
v_orig_ddl := dbms_metadata.get_ddl('TABLE','TEST', 'BI');
-- Rename Production tables with extension
EXECUTE IMMEDIATE 'ALTER TABLE BI.TEST RENAME TO TEST_' || TO_CHAR(v_seq);
EXECUTE IMMEDIATE v_orig_ddl;
END;
/