how to export data from around 300 tables in ORACLE DB to csv or txt files

廉价感情. 提交于 2021-02-08 04:48:03

问题


Is there any possibility to export data from around 300 tables within single schema with millions of records to CSV or TXT using any PL/SQL procedure?

What do you propose, which is fastest way to do it? For the moment I do not need to import these exported files to any other schema...

I tried with Toad manually exporting table by table...


回答1:


you can try following steps.

  1. write a loop to get the table names
  2. use cursors to fetch the data from each table
    1. use SYS.UTL_FILE utilities to write the data to files in any required format.

This is a very high level solution. But i am sure it will work.




回答2:


I have created a utility by which you can generate PL/SQL procedures to export data from a table. It will take the following parameters, table name, column names, directory name and the delimiter. You can generate 50 procedures for 50 tables in no time to export data from Oracle. Check this link Generate PL/SQL Procedure to export data into CSV




回答3:


I managed to dynamically go through all tables and get column names and write to a file. I am struggling into part how to fetch data rows from tables dynamically when execute immediate query? how should I save data rows and than fetch it and write to files? Here is the code:

DECLARE p_table        VARCHAR2 (100);
l_file UTL_FILE.FILE_TYPE;
l_string       VARCHAR2 (10000);
query_string   VARCHAR2 (4000);
BEGIN
FOR tab IN (SELECT *
             FROM dba_tables
            WHERE owner = 'XYZ' AND table_name LIKE 'XYZ%')
LOOP
  p_table := tab.table_name;

  l_file :=
     UTL_FILE.FOPEN ('my_path',
                     tab.table_name || '.txt',
                     'w',
                     10000);
  l_string := NULL;


  FOR col_he IN (SELECT *
                   FROM dba_tab_columns
                  WHERE owner = 'DWHCO' AND table_name = p_table)
  LOOP
     CASE
        WHEN l_string IS NULL
        THEN
           l_string := col_he.column_name;
        ELSE
           l_string := l_string || ',' || col_he.column_name;
     END CASE;
  END LOOP;

  UTL_FILE.PUT_LINE (l_file, l_string);            --Printng table columns

  query_string := 'select ' || l_string || ' from DWHCO.' || p_table      
  --Execute immediate query_string into ??????????;
        --??????
  UTL_FILE.FCLOSE (l_file);  END LOOP;END;



回答4:


The Data Dump procedure is helpful for programmatically exporting many tables to simple formats like CSV.

First, install the package using the above link. The below code creates a directory, cycles through tables, and exports each table as CSV.

create or replace directory temp_dir as 'C:\temp';

begin
    for tables in
    (
        select
            owner||'_'||table_name||'.csv' file_name,
            'select * from "'||owner||'"."'||table_name||'"' v_sql
        from dba_tables
        where owner = 'XYZ'
            and table_name like 'XYZ%'
        order by 1
    ) loop
        data_dump
        (
            query_in        => tables.v_sql,
            file_in         => tables.file_name,
            directory_in    => 'TEMP_DIR',
            delimiter_in    => ',',
            header_row_in   => true
        );
    end loop;
end;
/


来源:https://stackoverflow.com/questions/54788833/how-to-export-data-from-around-300-tables-in-oracle-db-to-csv-or-txt-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!