How to extract .csv for each table

Hi all

I have a query:

Select distinct ' SELECT * FROM BILL_M.' | Table_name | ';' from all_tab_columns t
where t.owner = 'BILL_M. '
and table_name like '% DICT '.

The query returns 80 s table... How can I extract these 80 to 80 csvs table?

Any help is appreciated.

Kind regards.

Just an example as a starting point...

As user sys:

CREATE OR REPLACE DIRECTORY TEST_DIR AS '\tmp\myfiles'
/
GRANT READ, WRITE ON DIRECTORY TEST_DIR TO myuser
/

As myuser:

CREATE OR REPLACE PROCEDURE run_query(p_sql IN VARCHAR2
                                     ,p_dir IN VARCHAR2
                                     ,p_header_file IN VARCHAR2
                                     ,p_data_file IN VARCHAR2 := NULL) IS
  v_finaltxt  VARCHAR2(4000);
  v_v_val     VARCHAR2(4000);
  v_n_val     NUMBER;
  v_d_val     DATE;
  v_ret       NUMBER;
  c           NUMBER;
  d           NUMBER;
  col_cnt     INTEGER;
  f           BOOLEAN;
  rec_tab     DBMS_SQL.DESC_TAB;
  col_num     NUMBER;
  v_fh        UTL_FILE.FILE_TYPE;
  v_samefile  BOOLEAN := (NVL(p_data_file,p_header_file) = p_header_file);
BEGIN
  c := DBMS_SQL.OPEN_CURSOR;
  DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
  d := DBMS_SQL.EXECUTE(c);
  DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
  FOR j in 1..col_cnt
  LOOP
    CASE rec_tab(j).col_type
      WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
      WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val);
      WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val);
    ELSE
      DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
    END CASE;
  END LOOP;
  -- This part outputs the HEADER
  v_fh := UTL_FILE.FOPEN(upper(p_dir),p_header_file,'w',32767);
  FOR j in 1..col_cnt
  LOOP
    v_finaltxt := ltrim(v_finaltxt||','||lower(rec_tab(j).col_name),',');
  END LOOP;
  --  DBMS_OUTPUT.PUT_LINE(v_finaltxt);
  UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
  IF NOT v_samefile THEN
    UTL_FILE.FCLOSE(v_fh);
  END IF;
  --
  -- This part outputs the DATA
  IF NOT v_samefile THEN
    v_fh := UTL_FILE.FOPEN(upper(p_dir),p_data_file,'w',32767);
  END IF;
  LOOP
    v_ret := DBMS_SQL.FETCH_ROWS(c);
    EXIT WHEN v_ret = 0;
    v_finaltxt := NULL;
    FOR j in 1..col_cnt
    LOOP
      CASE rec_tab(j).col_type
        WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
                    v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
        WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
                    v_finaltxt := ltrim(v_finaltxt||','||v_n_val,',');
        WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
                    v_finaltxt := ltrim(v_finaltxt||','||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'),',');
      ELSE
        DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
        v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
      END CASE;
    END LOOP;
  --  DBMS_OUTPUT.PUT_LINE(v_finaltxt);
    UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
  END LOOP;
  UTL_FILE.FCLOSE(v_fh);
  DBMS_SQL.CLOSE_CURSOR(c);
END;

This allows the header line and the data to write into files separate if necessary.

for example

SQL> exec run_query('select * from emp','TEST_DIR','output.txt');

PL/SQL procedure successfully completed.

Output.txt file contains:

empno,ename,job,mgr,hiredate,sal,comm,deptno
7369,"SMITH","CLERK",7902,17/12/1980 00:00:00,800,,20
7499,"ALLEN","SALESMAN",7698,20/02/1981 00:00:00,1600,300,30
7521,"WARD","SALESMAN",7698,22/02/1981 00:00:00,1250,500,30
7566,"JONES","MANAGER",7839,02/04/1981 00:00:00,2975,,20
7654,"MARTIN","SALESMAN",7698,28/09/1981 00:00:00,1250,1400,30
7698,"BLAKE","MANAGER",7839,01/05/1981 00:00:00,2850,,30
7782,"CLARK","MANAGER",7839,09/06/1981 00:00:00,2450,,10
7788,"SCOTT","ANALYST",7566,19/04/1987 00:00:00,3000,,20
7839,"KING","PRESIDENT",,17/11/1981 00:00:00,5000,,10
7844,"TURNER","SALESMAN",7698,08/09/1981 00:00:00,1500,0,30
7876,"ADAMS","CLERK",7788,23/05/1987 00:00:00,1100,,20
7900,"JAMES","CLERK",7698,03/12/1981 00:00:00,950,,30
7902,"FORD","ANALYST",7566,03/12/1981 00:00:00,3000,,20
7934,"MILLER","CLERK",7782,23/01/1982 00:00:00,1300,,10

The procedure allows for the header and the data to separate files if necessary. Just by specifying the file name "header" will put the header and the data in a single file.

Adapt to the exit of styles and different types of data are needed.

You can then call this procedure for all the tables that you want to extract, with names appropriate for each.

Tags: Database

Similar Questions

  • How to find the child level for each table in a relational model?

    Earthlings,

    I need your help, and I know that, "Yes, we can change." Change this thread to a question answered.

    So: How to find the child level for each table in a relational model?

    I have a database of relacional (9.2), all right?
    .
         O /* This is a child who makes N references to each of the follow N parent tables (here: three), and so on. */
        /↑\ Fks
       O"O O" <-- level 2 for first table (circle)
      /↑\ Fks
    "o"o"o" <-- level 1 for middle table (circle)
       ↑ Fk
      "º"
    Tips:
    -Each circle represents a table;
    -Red no tables have foreign key
    -the picture on the front line of tree, for example, a level 3, but when 3 becomes N? How is N? That is the question.

    I started to think about the following:

    First of all, I need to know how to take the kids:
    select distinct child.table_name child
      from all_cons_columns father
      join all_cons_columns child
     using (owner, position)
      join (select child.owner,
                   child.constraint_name fk,
                   child.table_name child,
                   child.r_constraint_name pk,
                   father.table_name father
              from all_constraints father, all_constraints child
             where child.r_owner = father.owner
               and child.r_constraint_name = father.constraint_name
               and father.constraint_type in ('P', 'U')
               and child.constraint_type = 'R'
               and child.owner = 'OWNER') aux
     using (owner)
     where child.constraint_name = aux.fk
       and child.table_name = aux.child
       and father.constraint_name = aux.pk
       and father.table_name = aux.father;
    Thought...
    We will share!

    Thanks in advance,
    Philips

    Published by: BluShadow on April 1st, 2011 15:08
    formatting of code and hierarchy for readbility

    Have you looked to see if there is a cycle in the graph of dependence? Is there a table that has a foreign key to B and B has a back of A foreign key?

    SQL> create table my_emp (
      2    emp_id number primary key,
      3    emp_name varchar2(10),
      4    manager_id number
      5  );
    
    Table created.
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  create table my_mgr (
      2    manager_id number primary key,
      3    employee_id number references my_emp( emp_id ),
      4    purchasing_authority number
      5* )
    SQL> /
    
    Table created.
    
    SQL> alter table my_emp
      2    add constraint fk_emp_mgr foreign key( manager_id )
      3         references my_mgr( manager_id );
    
    Table altered.
    
    SQL> ed
    Wrote file afiedt.buf
    
      1   select level lvl,
      2          child_table_name,
      3          sys_connect_by_path( child_table_name, '/' ) path
      4     from (select parent.table_name      parent_table_name,
      5                  parent.constraint_name parent_constraint_name,
      6                  child.table_name        child_table_name,
      7                  child.constraint_name   child_constraint_name
      8             from user_constraints parent,
      9                  user_constraints child
     10            where child.constraint_type = 'R'
     11              and parent.constraint_type = 'P'
     12              and child.r_constraint_name = parent.constraint_name
     13           union all
     14           select null,
     15                  null,
     16                  table_name,
     17                  constraint_name
     18             from user_constraints
     19            where constraint_type = 'P')
     20    start with child_table_name = 'MY_EMP'
     21*  connect by prior child_table_name = parent_table_name
    SQL> /
    ERROR:
    ORA-01436: CONNECT BY loop in user data
    

    If you have a cycle, you have some problems.

    (1) it is a NOCYCLE keyword does not cause the error, but that probably requires an Oracle version which is not so far off support. I don't think it was available at the time 9.2 but I don't have anything old enough to test on

    SQL> ed
    Wrote file afiedt.buf
    
      1   select level lvl,
      2          child_table_name,
      3          sys_connect_by_path( child_table_name, '/' ) path
      4     from (select parent.table_name      parent_table_name,
      5                  parent.constraint_name parent_constraint_name,
      6                  child.table_name        child_table_name,
      7                  child.constraint_name   child_constraint_name
      8             from user_constraints parent,
      9                  user_constraints child
     10            where child.constraint_type = 'R'
     11              and parent.constraint_type = 'P'
     12              and child.r_constraint_name = parent.constraint_name
     13           union all
     14           select null,
     15                  null,
     16                  table_name,
     17                  constraint_name
     18             from user_constraints
     19            where constraint_type = 'P')
     20    start with child_table_name = 'MY_EMP'
     21*  connect by nocycle prior child_table_name = parent_table_name
    SQL> /
    
           LVL CHILD_TABLE_NAME               PATH
    ---------- ------------------------------ --------------------
             1 MY_EMP                         /MY_EMP
             2 MY_MGR                         /MY_EMP/MY_MGR
             1 MY_EMP                         /MY_EMP
             2 MY_MGR                         /MY_EMP/MY_MGR
    

    (2) If you try to write on a table and all of its constraints in a file and do it in a valid order, the entire solution is probably wrong. It is impossible, for example, to generate the DDL for MY_EMP and MY_DEPT such as all instructions for a table come first, and all the instructions for the other are generated second. So even if NOCYCLE to avoid the error, you would end up with an invalid DDL script. If that's the problem, I would rethink the approach.

    -Generate the DDL for all tables without constraint
    -Can generate the DDL for all primary key constraints
    -Can generate the DDL for all unique key constraints
    -Can generate the DDL for all foreign key constraints

    This is not solidarity all the DOF for a given in the file object. But the SQL will be radically simpler writing - there will be no need to even look at the dependency graph.

    Justin

  • How to get LASTDAY for each month between data dates...

    Hi friend

    I have a doubt, how to get LASTDAY for each month between data dates...

    for ex:
    My contribution will be like this
    date = 01/12/2011
    To date = 14/04/2011

    And I need an output like
    31/01/2011
    28/02/2011
    31/03/2011

    is it possible to achieve through sql query in oracle
    Thanks in advance for all friends to help him

    Hello

    Something like this (assuming that the dates are originally VARCHAR2s):

    SQL> var dt_start varchar2(10)
    SQL> var dt_end varchar2(10)
    SQL> exec :dt_start := '12-01-2011'
    
    PL/SQL procedure successfully completed
    
    SQL> exec :dt_end := '14-04-2011'
    
    PL/SQL procedure successfully completed
    
    SQL>
    SQL> select last_day(
      2           add_months(
      3             trunc(to_date(:dt_start,'DD-MM-YYYY'),'MM'),
      4             level-1
      5           )
      6         ) as result
      7  from dual
      8  connect by level <= months_between(to_date(:dt_end,'DD-MM-YYYY'), to_date(:dt_start,'DD-MM-YYYY'))
      9  ;
    
    RESULT
    -----------
    31/01/2011
    28/02/2011
    31/03/2011
     
    

    If the dates are already of the date data type, simply remove the to_date function.

  • Are there records of the sys the timestamp of change for each table?

    I noticed that the sys "object" records just the creation and the timestamp of DDL operation.
    But I need the timestamp of change (for example, insert, update, delete, etc.) for my paintings. Otherwise, I fear that I must create the trigger for each table it's hard work.

    You can use scn_to_timestamp (max (ora_rowscn)) for the dml changes

  • Requirement of Beeping - how to get subtotals for each combination of 2 columns val

    Hi gurus, Experts and all,.

    Help me please with my reporting requirement, on how to code this in PIF. Thank you.

    Requirement: I need to get the subtotals for each value of the combination of two columns.

    Columns example: team group AMOUNT

    Sample column values: placed Team1 100
    Group b... Team2... 200

    GroupA - Team1 Subtotals = 500 (value of the sample only)
    GroupA - Team2 Subtotals = 400 (value of the sample only)
    Group b - Team1 Subtotals = 600 (value of the sample only)
    Group b - Team2 Subtotals = 200 (value of the sample only)

    GrandTotal = 1700

    Thank you.

    Jean Paul
    BEEP newbie

    Published by: user10955574 on October 28, 2010 05:40
    
     -  - 
    
    

    output for data

    GROUPA - TEAM1 - 200
    GROUPB - TEAM1 - 300
    GROUPC - TEAM2 - 400
    GROUPD - TEAM2 - 500
    GROUPA - TEAM3 - 600
    GROUPB - TEAM3 - 700
    GROUPC - TEAM4 - 800
    GROUPD - TEAM4 - 900
    GROUPA - TEAM5 - 1000
    
  • How to create buttons for each slide in the slide show

    I currently have a slideshow with prev and next buttons.

    but I would add buttons numbered for each slide to image-base.

    with a static number for the current slide has a circle colorful around this number.

    I can create static number with circle colored in Photoshop, and add a layer,

    but it's a lot to do this for all the numbers work.

    Is there a better way to create the numbers highlighted for the current slide?

    For the numbered buttons that link to the slides not active, I can create numbers in Photoshop and import images into Flash.

    then convert to symbols and assigns actions to them,

    But it's also plenty to do for all the numbers work.

    Is there a better way to create numbered slide buttons inactive?

    I would also like to have a function to hover over the numbered buttons,

    It changes from plain image from number to number with circle colorful as I want to use for the current slide.

    How can I assign a hover button symbol function, which will load another image or symbol?

    Try to think in terms of having reusable symbols.  Think what it through before you try it.

    Use a movieclip instead of a button symbol.  Have a dynamic textfield in this movieclip to the number and value of the textfield object to not be selectable.  Have a picture of this movieclip where it highlighted as one (selected) and the movieclip go there where you are on this slide.

    Use the textfield for the number - assign dynamically so that you don't have to import an image for it.  Use as many of these clips that you need for all your buttons.

    Have functions that reset all buttons at the same time so that when you move to a new slide, selected earlier dates back to mormal... following with setting one newly selected in his "selected" frame

  • How to give rownumber for each group

    Hai All

    I use the function group to separate and in this group, I need to give rownumber to each departure of members of Group 1 and when a group is on the needs in number to start new 1


    For example

    There are 3 Department and it is separated by the function of group by Department and here I need to give the line number. And the line number must be separated for each group

    Thanks and greetings

    Srikkanth.M

    How about using...

    ROW_NUMBER() OVER (partition by GROUP order by X)
    

    If this is not the case, how about post you some sample data and the output you want from these data.

    In addition, your version of Oracle

    select * from v$version;
    
  • How to give rownum for each group in forms6i

    Hai All

    I have to use the line for each individual group number if it is possible in oracle forms6i

    I used this feature and get an error, the function is

    ROW_NUMBER() over (partition by bar code, order by code bardate bar)


    and if it is possible to use decode function in oracle forms6i

    Thanks for giving good solutions

    Thanks and greetings

    Srikkanth.M

    Same question but different title :)

    Yes, you can use function in 6i to decode

    Bangoura
    [My Oracle Blog | http://baigsorcl.blogspot.com/]

  • How to use partitioning for large table

    Hello

    ******************
    Oracle 10 g 2/Redhat4
    RAC database
    ASM
    *******************

    I have a TRACE table that will also grow very fast, in fact, I 15,000,000 lines.

    TRACES (IDENTIFICATION NUMBER,
    NUMBER OF COUNTRY_NUM
    Timestampe NUMBER,
    MESSAGE VARCHAR2 (300),
    type_of_action varchar (20),
    CREATED_TIME DATE,
    DATE OF UPDATE_DATE)

    Querys who asked for this table are and make a lot of discs!

    --------------------------------------------------------
    Select count (*) as y0_
    TRACES this_
    where this_. COUNTRY_NUM =: 1
    and this_. TIMESTAMP between: 2 and: 3
    and lower (this_. This MESSAGE) as: 4;
    ---------------------------------------------------------
    SELECT *.
    FROM (SELECT this_.id,
    this_. TIMESTAMP
    Traces this_
    WHERE this_. COUNTRY_NUM =: 1
    AND this_. TIMESTAMP BETWEEN: 2 AND: 3
    AND this_.type_of_action =: 4
    AND LOWER (this_. MESSAGE) AS: 5
    ORDER BY this_. TIMESTAMP DESC)
    WHERE ROWNUM < =: 6;
    -----------------------------------------------------------

    I have 16 distinct COUNTRY_NUM in the table and the TIMESTAMPE is a number that the application is inserted into the table.

    My question is the best solution to resolve this table is to use partitioninig for a smal parts?
    I have need of a partitioning using a list by date (YEAR/month) and COUNTRY_NUM, is there a better way for her?

    NB: for an example of TRACES in my test database

    1 Select COUNTR_NUM, count (*) traces
    Group 2 by COUNTR_NUM
    3 * order by COUNTR_NUM
    SQL > /.

    COUNTR_NUM COUNT (*)

    194716 1
    3 1796581
    4 1429393
    5 1536092
    6 151820
    7 148431
    8 76452
    9 91456
    10 91044
    11 186370
    13 76
    15 29317
    16 33470

    Hello

    Yes and part is that you can remove the whole score once you don't need "of them. I think you should be good to go. Let me know if you have any other questions.

    Concerning

  • How to enter comments for each photo?  Preparation of photos for sale at the auction house.

    I have a historical photo collection b & w preparation for the auction.  How to catalogue each photo with custom comments?  I have LR 4.4

    You will need to type this info in this field in the metadata Panel.  I assume that you want to use the legend, perhaps Title fields, two fields must be empty for you to enter custom text. If you select all images and enter the text, all the images Gets the same data. So think about what you want to pop over there and if you can do so at the same time, or if you will need to do frame-by-frame.

  • [JS CS3] how to extract information from scriptsList table

    Hi all,

    just a silly question: How can I extract scriptList property filePath and fileName and display it in an alert box?

    Ah, that should have been

    for (i = 0; i< f.length;="">

    f instead of s. You will see the path names starting with a dash of turnings (~), which is js pointing to the folder of the user. You can learn more about records and their names in the JS tool guide, see in respect of aid in the ESTK Help menu.

    app.scriptPreferences.scriptsList returns the scripts in the folder of the user and the application folder. app.scriptPreferences.scriptFolder returns only the scripts in the user folder. Mmm - I think that this is the case, anyway. Try before you buy.

    Peter

  • How to add folio for each starting line of page

    Dear friend,

    I've been trying to script.

    In the Document every pages of first line and the first word before that I want add folio (page number).  Please suggest a friend. How to complete this script coding.

    Please see my requirement in the screenshot below.

    Screen Shot 2016-04-29 at 4.11.24 PM.png

    Please help friends.

    Thanks in advance

    Try this

    for (p = 0; p

    {

    try {}

    app.activeDocument.pages [f] .textFrames [0] .lines [0] .insertionPoints [0] .silence = "";

    }

    {catch (e)}

    }

  • How to use RIDC for query Tables content WebCenter

    Hi all

    Can we use content query API RIDC WebCenter 11g Tables.if yes then how can I achieve this?

    I'm newbie

    is always found in the oracle doc

    http://docs.Oracle.com/CD/E21764_01/doc.1111/e11011/c05_core.htm#BABBABBF
    and this also

    http://jonathanhult.com/blog/2012/07/RIDC-versus-CIS/

  • How to make replace() for each symbol in the string instead of the first meeting only?

    Hello!

    This is my simple test script:

    var str = ' re | PLA | ce »;

    var strRep str.toString () = replace (' | ', '-');

    Alert (strRep);

    I'd get a re-pla-this result string.

    But I didn't get my only first symbol replaced:

    replace.png

    Can you advise me please, how to get all the symbols replaced?

    Thank you!

    Thank you very much, Xavier!

    Just for reference, this line will work perfectly:

    Alert (str.toString () replace (new RegExp ("|", "g"), '-'));

  • Count for each table

    Hello

    I try to count (*) for all the table owned by a user
    I have the following error

    What's wrong?
    Thank you
    declare
    2 i the number;
    3 t_owner varchar2 (20);
    4 t_table_name varchar2 (20);
    5. start
    6 t in (select * from all_tables where owner = "MISAMM")
    7 loop
    8 t_owner: = t.owner;
    9 t_table_name: = t.table_name;
    10 dbms_output.put_line (' :'|| Table t_table_name);
    11 dbms_output.put_line (' owner :'|| t_owner);
    12 select count (*) in I of t_owner.t_table_name;
    14 dbms_output.put_line ('Table :'|| t.table_name |) "lines :'|| TO_CHAR (i));
    15 end loop;
    16 end;
    17.
    Select count (*) in I of t_owner.t_table_name;
    *
    ERROR on line 12:
    ORA-06550: line 12, column 37:
    PL/SQL: ORA-00942: table or view does not exist
    ORA-06550: line 12, column 1:
    PL/SQL: SQL statement ignored


    Elapsed time: 00:00:00.01

    Hello

    The names of the tables come display of metadata dynamically. Thus, you should use "immediate execution".

    declare
      i            number;
      t_owner      varchar2(20);
      t_table_name varchar2(20);
    begin
      for t in (select * from all_tables where owner = 'MISAMM')
      loop
        t_owner      := t.owner;
        t_table_name := t.table_name;
        dbms_output.put_line('Table:' || t_table_name);
        dbms_output.put_line('Owner:' || t_owner);
        execute immediate 'select count(*) from ' || t_table_name
          into i;
        dbms_output.put_line('Table:' || t.table_name || ' rows:' || to_char(i));
      end loop;
    end;
    /
    

    Also, I wonder why you are not satisfied with the "num_rows" user_tables and all_tables column?

Maybe you are looking for

  • Measures of current between 4071 and 2575 sometimes not correct

    I use mode of handshake with a PXI-4071 scan list and an SMU-2575. The DMM is current and the MUX is in mode 1 x 196. When running the scan list, I have rarely (randomly) get a very small extent to the nA au (when to 4-20 MA). I'll run the scan list

  • Maximum rates in servers Windows CPU and RAM

    Hi all I would like to know the maximum limit of CPU and physical and virtual RAM in the below mention Windows Server.Anyone help out me? Windows Server 2012 R2 Essentials Windows Server 2012 R2 Standard Windows Server 2012 R2 Datacenter Windows Serv

  • How can I fix back web version id invalid 'Runner Error'?

    I get an error "runner" when I, or reset, my computer. This error reads... Web version of invalid return ID 6.3.2.116 - 9972322. What should I do about it?

  • Installation of Microsoft Forefront Endpoint Protection 0x8007007e error.

    I am trying to install Microsoft Forefront Endpoint Protection 2010 on a Windows XP SP3 machine but I get an error 0x8007007e installation. If anyone can help? I completely deleted the previous machine 'McAfee' anti-virus software using the McAfee re

  • He dies

    I just connected my mp3 player to the speakers when im at the gym and w/o a warning or anything like that.  the mp3 player is dead.  And I thought is I could have sourn I have just loaded. so I listened to the radio and home and plugged.  and he appa