创建INSERT语句以从每个现有表中导出数据

时间:2015-07-21 09:18:52

标签: oracle oracle11g export sql-insert

我目前无法获得正确的数据库转储,因为数据库在封闭系统内的远程服务器上运行 - >没有远程复制的可能性,只有通过物理存在于服务器位置或通过电子邮件(但我无法通过邮件发送几个GB大转储......)来获取文件进/出的方法。

但是,我仍然需要数据才能将其导入我的开发系统。

我认为最好的方法是创建包含所需信息的INSERT语句。

SQL-Developer软件实际上可以做到这一点,但显然它一次只适用于一个表。只要选择多个表,相应的选项就会从右键单击菜单中消失,而且只能导出DDL语句: - /

所以这种方法对我来说并不可行,因为有数百张表......

有没有人知道通过查询元数据表(user_tables,user_columns,...)来创建INSERT语句的标准化方法?我可以想象通过巧妙地加入这些元表来创建所有语句是可能的。然而,在倾倒几个小时之前,如果有人能够先确认这种怀疑,我会很感激。

此外其他人一定有这个问题,所以我希望你们中的一些人能够给我一些其他方法的暗示。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我的回答并非完全解决方案。
1)提取DDL使用。

select table_name,dbms_metadata.get_ddl(OBJECT_TYPE=>'TABLE', NAME=>table_name) from user_tables; 

2)要从表中提取记录,请使用xmltype(refucursor)和dbms_xmlstore来插入它们。 以下仅建议如何执行此操作。

create table test as select level as "LP" from dual connect by level < 100;

declare 
 v_cursor sys_refcursor;
 xmlDoc xmltype; 
 curid    NUMBER;
 insCtx DBMS_XMLSTORE.ctxType;
 rows NUMBER;
begin
  open v_cursor for 'select * from test';
    xmlDoc  := xmltype(v_cursor); 
  close v_cursor;
  dbms_output.put_line(xmlDoc.getClobVal()); -- extracted row into xml. 

  insCtx := DBMS_XMLSTORE.newContext('test'); 
  DBMS_XMLSTORE.clearUpdateColumnList(insCtx); 
  rows := DBMS_XMLSTORE.insertXML(insCtx, xmlDoc); 
  dbms_output.put_line('ROWS inserted' || rows);
   DBMS_XMLSTORE.closeContext(insCtx); 
   commit;
end;