PL/SQL-generate CSV file

I was told that I was able to generate a CSV file via method UTL_FILE. Can anyone provide the skeleton of how should I create in PL/SQL?

I tried the google search, but it says in words and not code itself.

I have this PL/SQL which I want to generate a CSV file.
SET SERVEROUTPUT ON;
SET TIMING ON;

DECLARE
    CURSOR C1
    IS 
    select balance from contract ;
    
BEGIN
    FOR i IN C1 LOOP

 IF i.balance <> '0' THEN
      DBMS_OUTPUT.PUT_LINE('Amount is not 0 ');  
I will like to put the statement 'Amount is not 0 ' in a CSV file here

     else 
     DBMS_OUTPUT.PUT_LINE('Amount is 0 ');  
     END IF;
    
     end loop;
END;
/
How am I suppose to do?

Published by: JoannaLee on August 26, 2008 19:14

Your question is a bit confusing. In your subject line and once again in your question, you talk about generating a CSV file. This is a comma-separated values file (where the acryonym). But your question seems to ask about the generation of a file with a phrase in computing - that would be a flat file, but not a CSV file.

The links provided show you exactly how to generate a CSV file, based on an arbitrary query.

If you are not looking to generate a CSV file, and instead, you just want to generate a flat file to which you can write data, you can just use the UTL_FILE package. Assuming you're on a recent version of Oracle, you need to create a directory object, i.e.

CREATE DIRECTORY directory_name AS 'path_to_directory_on_database_server'

Then you would grant privileges on this directory for any user object will hold the procedure who wants to write to the directory

GRANT read, write ON directory_name TO some_user

In your code, you should just open the file and write

DECLARE
  l_file utl_file.file_type;
BEGIN
  l_file := utl_file.fopen( 'DIRECTORY_NAME', 'name_of_file', 'w' );

  <>

  utf_file.put_line( l_file, 'String you want to write to the file' );

  <>

  utl_file.fclose( l_file );
END;

Justin

Tags: Database

Similar Questions

  • Generate CSV with pl.sql column names

    Hi guys,.

    Well, I am back after a long hiatus in this form, was occupied by SAP BI project.

    Now do some oralce - sap project, well here, I needed to generate a CSV of many many queries,

    I can do via sql, but I tot it would be better to write a package or a common procedure to generate csv files

    ex, if I pass the query to the proceudre it should generate csv on the server directory file, later I send these file to the users concerned.

    The e-mail part is later, but first of all is to have a package of generic to launch and run the query n generate the csv file.

    I guess this should be possible using native dynamic sql and utl_ package, but I don't know how to get the columns name,

    If one has implemented this please explain me the way,.

    ex:
    Select emp,mgr, empno from emp;
    
    emp mgr empno
     1    1    123
      2   1    234
    
    something like that
    hope my question is clear 

    Maybe try this

    http://asktom.Oracle.com/pls/asktom/asktom.download_file?p_file=6551135514501758779

  • Externalize strings not generating a CSV file translations

    Hi gurus,

    I need to do outsourcing of presentation layer, so doing it, I activated by clikcing to outsource the table on the area in question and after I have go to tools-> utilities-> externalize strings-> run

    the csv file, it creates is having only the English, I don't get the translations to other languages in the csv file. I m hit there.

    Pls help!


    Thank you
    Anthony

    Hi Anthony,.

    The generated CSV file will be in English only. If you try to just change in OBIEE Presenatation layer to another language, you need to perform operations of addition after the export of the CSV file on your machine.

    Look at this article: (I think that's what you're looking for);

    http://oraclebizint.WordPress.com/2007/10/29/Oracle-BI-EE-101332-presentation-layer-translation/

    Kind regards
    -Laurence.

  • CSV files

    How are Hello everyone, you?

    I'm working on a software to make measurements using my drone of railroad.

    My drone generate csv files (such as it is attached).

    The first column corresponds to the distance that generate the encoder, the second colum is generated by the inclinometer.

    But if I open data, for example Crosslevel, the distance on my VI, in the first graph called CrossLevel, the distance corresponds to the values of the CSV (first colum on excel), the distance on X showes how many lines form on Excel (crosslevel showes 54, should be starting 0.004 and finishing on 4 924)

    The distance on X software indicates how many lines on Excel chart

    the values are correct

    Basically, I need a XY Chart with correct values from csv files

    I tried to separate the values of the first and the second column of the csv files, but I'm

    Is there a way to make the graph shows the value I want?

    To read the data on my VI, open the VI and on these 3 fair trails enter the value corresponding to each path, then press the red button in addition to the STOP button. the software will only work if you enter all 3 csv files

    The VI and these 3 csv are attached.

    Thank you very much

    Sorry for my bad English

    Why do you use dynamic data?
    You turn table of DD and new table.
    Is there way to draw your data on the XY graph.

    Your transformation function is also a 2-channel signal but not xy-signal

  • create CSV file

    Hello friends,

    I want to generate .csv file. & write data inside.

    can someone suggest so any idea for it?

    Thank you & best regards

    Milan

    Try this...

    DataOutputStream os = null;
    FileConnection fconn = null;
    String FileName = "sample.csv;
    try {}
    fconn = (FileConnection) Connector.open ("file:///SDCard/BlackBerry/" + name of file, Connector.READ_WRITE);
    If (! fconn.exists ())
    fconn. Create();
    OS = fconn.openDataOutputStream ();
    String Contact_Title = "First name, family, Email1, Email2, organization, title, such work \n";
    OS. Write (Contact_Title.GetBytes ());
    } catch (Exception e) {}
    System.out.println ("Con:" + e.getMessage ());
    } {Finally
    try {}
    If (null! = os)
    OS. Close();
    If (null! = fconn)
    fconn. Close();
    } catch (IOException e) {}
    System.out.println (e.getMessage ());
    }
    }

  • ODI csv file

    I could turn my sql to CSV file tables in odi?

    If the answer is Yes, how can I do? with packages?

    I really appreciatte your help

    Best regards

    odisqlunload in the package to extract data from table in the file (csv)

  • Convert .xls in .csv file using pl/sql or any other method (oracle 9i)

    Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64 bit Production

    With partitioning, OLAP and Oracle Data Mining options

    Release 9.2.0.8.0 - Production jserver

    10.1.2.0.2 generator

    ORACLE Server Release 10.1.0.4.2

    Oracle 10.1.2.0.2 procedure generator

    Oracle ORACLE PL/SQL V10.1.0.4.2 - Production

    Oracle 10.1.0.4.0 Production CORE

    Integration Services 10.1.2.0.2 tools Oracle

    Space common tools Oracle 10.1.2.0.2

    Oracle Toolkit 2 for 32-bit Windows, 10.1.2.0.2 platforms

    Resource object 10.1.2.0.2 store

    Oracle Help 10.1.2.0.2

    Oracle Sqlmgr 10.1.2.0.2

    Query Oracle 10.1.2.0.2 - Production Designer

    PL/SQL Editor (c) WinMain Software (www.winmain.com), v1.0 (Production)

    Oracle ZRC 10.1.2.0.2

    Oracle XML Developers Kit 10.1.0.4.2 - Production

    Virtual graphics system Oracle 10.1.2.0.2

    Image of Oracle 10.1.2.0.2

    Widget media Oracle 10.1.2.0.2

    The GUI utility 10.1.2.0.2 tools Oracle

    I had seen a very old post of 2009.

    Convert .xls in .csv file using pl/sql

    I know that it s a long time but just wanted to know if there is a way to convert excel .csv.

    I have a directory say 'F:\purple_top\ '. I'll get the .xls file in this directory. I need to change to .csv and tables load external oracle. Any ideas or advance coding on it since 2009 so far?

    A few other options might be:

    • Java code, which can be used from within Oracle, which makes use of utility libraries (e.g. Aspose.Cells) open source (for example Apache POI) or "save under" CSV, or read the source cells and their output to a CSV file.
    • COM Automation, using available open source (for example, JACOB or j-interop) or commercial libraries, which can automate Excel to open a XLS file and "Save as" a CSV file. In addition, Oracle has a feature of COM Automation (available since version 8 but abandoned after Oracle 11g). I have successfully used this read (non-macro-enabled) Excel files in PL/SQL.
    • Other approaches using Perl or PHP can be found by googling 'convert xls to csv.

    Gerard

  • PL/Sql to generate CSV on daily basis

    Hello

    I am trying to generate a csv file daily base asfet my data gets updated through SQL Server to the oracle server.

    {code}

    create or replace

    procedure downloadxls (sysdate in date)

    IS

    s cursor is

    Select d.DEBTORNO, separate d.name.

    d.City | » '|| d.State |' ' || d.ZipCode city,

    substr (d.phone1, 0, 3). » -' || substr (d.phone1, 3, 3) | » -'|| substr (d.phone1, 6, 4) as phone,

    d.MOTORCARRNO, CREDEXPIREDATE, CREDEXPIREMOS

    DEBTOR d

    WHEN trunc (d.CREDEXPIREDATE) > = trunc (sysdate)-1

    and CREDEXPIREMOS in (3,6,9,12)

    and D.NOBUYDESC is null

    and (d.NOBUYDISPUTEKEY is null or d.NOBUYDISPUTEKEY = 0)

    order by 2;

    MSG Number (38);

    j_name DEBTORS.name%type: = null;

    j_DEBTORNO DEBTORS. % DEBTORNO type: = null;

    j_city DEBTORS.city%type: = null;

    j_state DEBTORS.state%type: = null;

    j_zip DEBTORS.zipcode%type: = null;

    j_phone1 varchar2 (12): = null;

    j_MOTORCARRNO DEBTORS. % MOTORCARRNO type: = null;

    j_CREDEXPIREDATE DEBTORS. % CREDEXPIREDATE type: = null;

    j_CREDEXPIREMOS DEBTORS. CREDEXPIREMOS % type: = null;

    function escape (s in varchar2) return varchar2 as

    Start

    Return (Replace(s,'"','""'));

    escape end;

    Start

    -dbms_output.put_line ('do something?');

    -owa_util.mime_header (' file/csv');

    owa_util.mime_header('file/csv',false);

    HTP.p ('Content-Disposition: attachment; filename = "CRE_expire_" | to_char (sysdate, 'MMDDYY') |'.) (CSV"');

    owa_util. HTTP_HEADER_CLOSE;

    HTP.p ('DEBTORNO, name, city, State, zip, numbers1, MOTORCARRNO, CREDEXPIREDATE, CREDEXPIREMOS');

    for rec in s loop

    j_DEBTORNO: REC = DEBTORNO;

    j_name: = rec.name;

    j_city: = rec.city;

    -j_state: = rec.state;

    -j_zip: = rec.zip;

    j_phone1: = rec.phone;

    j_MOTORCARRNO: REC = MOTORCARRNO;

    j_CREDEXPIREDATE: REC = CREDEXPIREDATE;

    j_CREDEXPIREMOS: REC = CREDEXPIREMOS;

    end loop;

    END downloadxls; {code}

    call {code of}

    DECLARE

    SYSDATE DATE;

    BEGIN

    SYSDATE: = NULL;

    DOWNLOADXLS (SYSDATE = > SYSDATE);

    END; {code}

    Errors

    Downloadxls compiled PROCEDURE

    WARNING: the execution is completed with warning

    Downloadxls compiled PROCEDURE

    WARNING: the execution is completed with warning

    Error from the 1 in the command line:

    DECLARE

    SYSDATE DATE;

    BEGIN

    SYSDATE: = NULL;

    DOWNLOADXLS (SYSDATE = > SYSDATE);

    END;

    Error report:

    ORA-06502: PL/SQL: digital error or value

    ORA-06512: at "SYS." OWA_UTIL", line 356

    ORA-06512: at "SYS." OWA_UTIL", line 415

    ORA-06512: at "TBSBI. DOWNLOADXLS', line 37

    ORA-06512: at line 5

    06502 00000 - "PL/SQL: digital error or the value of %s.

    * Cause:

    * Action:

    See this classic article from Tom Kyte where it shows several ways to do.

    http://tkyte.blogspot.com/2009/10/httpasktomoraclecomtkyteflat.html

  • Import from CSV file into oracle using pl/sql - sql loader 3.1

    Hello

    I am trying to learn how to import a CSV file in an oracle table. I tried the import utility in SQL Loader, but it generates specific lines of code to import each line. I would like a loop to go through the CSV file stored on my local disk and transferred it to my table.

    I tried the following, but not luck
    DOWNLOAD THE DATA
    INFILE 'sample_csv.csv '.
    IN THE TEST TABLE
    FIELDS TERMINATED BY ', '.
    ;

    951716 wrote:
    Hello

    I am trying to learn how to import a CSV file in an oracle table. I tried the import utility in SQL Loader, but it generates specific lines of code to import each line. I would like a loop to go through the CSV file stored on my local disk and transferred it to my table.

    line-by-line is slow by slow.
    PL/SQL will be NEVER faster than simple SQL
    INSERT INTO MYTABLE SELECT * FROM EXTERNAL_TBL;

    http://www.orafaq.com/wiki/External_table

  • Delete the name of the column in the CSV file generated by ODI

    Hello

    I want to remove the column names in the CSV file that I create the database.
    I tried to put "towards zero in the data store ', that I created for the csv file, but it does not work.

    Please help.

    Thank you
    Sudeep Nathalie

    Sudeep salvation,

    Ok.. If you don't want the column to be generated and then in your IKM (IKM SQL to THE grit file) choose the option GENERATE_HEADER to 'no '.

    Then run the interface. The target csv file will not now column header.

    Thank you
    Fati

  • SQL Loader loading data into two Tables using a single CSV file

    Dear all,

    I have a requirement where in I need to load the data into 2 tables using a simple csv file.

    So I wrote the following control file. But it loads only the first table and also there nothing in the debug log file.

    Please suggest how to achieve this.

    Examples of data

    Source_system_code,Record_type,Source_System_Vendor_number,$vendor_name,Vendor_site_code,Address_line1,Address_line2,Address_line3

    Victor, New, Ven001, Vinay, Vin001, abc, def, xyz

    Control file script

    ================

    OPTIONS (errors = 0, skip = 1)
    load data
    replace
    in the table1 table:
    fields ended by ',' optionally surrounded "" "
    (
    Char Source_system_code (1) POSITION "ltrim (rtrim (:Source_system_code)),"
    Record_type tank "ltrim (rtrim (:Record_type)),"
    Source_System_Vendor_number tank "ltrim (rtrim (:Source_System_Vendor_number)),"
    $vendor_name tank "ltrim (rtrim (:Vendor_name)),"
    )
    in the Table2 table
    1 = 1
    fields ended by ',' optionally surrounded "" "
    (
    $vendor_name tank "ltrim (rtrim (:Vendor_name)),"
    Vendor_site_code tank "ltrim (rtrim (:Vendor_site_code)),"
    Address_line1 tank "ltrim (rtrim (:Address_line1)),"
    Address_line2 tank "ltrim (rtrim (:Address_line2)),"
    Address_line3 tank "ltrim (rtrim (:Address_line3)).
    )

    the problem here is loading into a table, only the first. (Table 1)

    Please guide me.

    Thank you

    Kumar

    When you do not provide a starting position for the first field in table2, it starts with the following after a last referenced in table1 field, then it starts with vendor_site_code, instead of $vendor_name.  So what you need to do instead, is specify position (1) to the first field in table2 and use the fields to fill.  In addition, he dislikes when 1 = 1, and he didn't need anyway.  See the example including the corrected below control file.

    Scott@orcl12c > test.dat TYPE of HOST

    Source_system_code, Record_type, Source_System_Vendor_number, $vendor_name, Vendor_site_code, Address_line1, Address_line2, Address_line3

    Victor, New, Ven001, Vinay, Vin001, abc, def, xyz

    Scott@orcl12c > test.ctl TYPE of HOST

    OPTIONS (errors = 0, skip = 1)

    load data

    replace

    in the table1 table:

    fields ended by ',' optionally surrounded "" "

    (

    Char Source_system_code (1) POSITION "ltrim (rtrim (:Source_system_code)),"

    Record_type tank "ltrim (rtrim (:Record_type)),"

    Source_System_Vendor_number tank "ltrim (rtrim (:Source_System_Vendor_number)),"

    $vendor_name tank "ltrim (rtrim (:Vendor_name)).

    )

    in the Table2 table

    fields ended by ',' optionally surrounded "" "

    (

    source_system_code FILL (1) POSITION.

    record_type FILLING,

    source_system_vendor_number FILLING,

    $vendor_name tank "ltrim (rtrim (:Vendor_name)),"

    Vendor_site_code tank "ltrim (rtrim (:Vendor_site_code)),"

    Address_line1 tank "ltrim (rtrim (:Address_line1)),"

    Address_line2 tank "ltrim (rtrim (:Address_line2)),"

    Address_line3 tank "ltrim (rtrim (:Address_line3)).

    )

    Scott@orcl12c > CREATE TABLE table1:

    2 (Source_system_code VARCHAR2 (13),)

    3 Record_type VARCHAR2 (11),

    4 Source_System_Vendor_number VARCHAR2 (27),

    5 $vendor_name VARCHAR2 (11))

    6.

    Table created.

    Scott@orcl12c > CREATE TABLE table2

    2 ($vendor_name VARCHAR2 (11),)

    3 Vendor_site_code VARCHAR2 (16).

    4 Address_line1 VARCHAR2 (13),

    5 Address_line2 VARCHAR2 (13),

    Address_line3 6 VARCHAR2 (13))

    7.

    Table created.

    Scott@orcl12c > HOST SQLLDR scott/tiger CONTROL = test.ctl DATA = test.dat LOG = test.log

    SQL * Loader: release 12.1.0.1.0 - Production on Thu Mar 26 01:43:30 2015

    Copyright (c) 1982, 2013, Oracle and/or its affiliates.  All rights reserved.

    Path used: classics

    Commit the point reached - the number of logical records 1

    TABLE1 table:

    1 row loaded successfully.

    Table TABLE2:

    1 row loaded successfully.

    Check the log file:

    test.log

    For more information on the charge.

    Scott@orcl12c > SELECT * FROM table1

    2.

    RECORD_TYPE SOURCE_SYSTEM_VENDOR_NUMBER $VENDOR_NAME SOURCE_SYSTEM

    ------------- ----------- --------------------------- -----------

    Victor Ven001 new Vinay

    1 selected line.

    Scott@orcl12c > SELECT * FROM table2

    2.

    $VENDOR_NAME VENDOR_SITE_CODE ADDRESS_LINE1 ADDRESS_LINE2 ADDRESS_LINE3

    ----------- ---------------- ------------- ------------- -------------

    Vinay Vin001 abc def xyz

    1 selected line.

    Scott@orcl12c >

  • How to generate the CSV file by coil

    Hello
    While running the script file Unix spool, it will show the output in Unix.
    And the file is created with query results from nd.
    I don't want the result expected in CSV file.

    set pagesize 10000
    the value of colsep «»
    Set feedback off
    set the position
    trigger the echo
    coil /home/tata/time.csv
    Select * from emp;
    spool off
    set echo on
    topic on the value
    Set of feedback on

    Thank you
    Lony

    lony wrote:
    I tried the sub query but still, it shows the output
    How to stop the putput.

    set pagesize 10000
    the value of colsep «»
    termout off Set
    Set feedback off
    set the position
    trigger the echo

    coil /home/tata/time.csv

    spool off
    Set termout on
    set echo on
    topic on the value
    Set of feedback on

    Place these commands alongwith your query into a file and then run it from the SQLPlus command line.
    There is no way the query statement delete appear in file queued if stuck directly into the SQLplus command prompt.
    How to remove the sql statement in the output of the coil?

  • In SQL Developer Table CSV file import

    Hi all - I'm trying to use SQL Developer to import data into a CSV file in one of my paintings. (I'm trying to see if I can get out without using SQL Loader). Among the data elements must be cut before it will be insert in the database column, but I see no way to do it in the Import Wizard SQL Developer (step 3 of 4). I wonder if there is a way to do it.

    Thank you.

    I saw all the options on the CSV import, which will allow for the manipulation of file before importing.

  • Anyone has any experience of importing a csv file and store it in a database in MS SQL

    Anyone has any experience of importing a csv file and store it in a database MS SQL. Outside sql injection there is any another converns security?

    Draft of the steps.

    1. use to download the file from the client to the server see
    http://www.dennismiller.TV/index.cfm/2007/12/26/file-upload-using-ColdFusion-and-Flex

    2. use to read the contents of the file into a variable.

    3. perform a loop on the content of variables for each line, dealing with the variable as a list delimited by a newline rather then a comma

    4. use list functions on each line to get the data you need, and then to transmit these data to SQL with CFQUERY or CFPROCPARAM.

  • Can generate the file TKPROF without the SQL does not finished?

    Hi all:
    I want to generate the TKPROF to the trace file file, can generate the file TKPROF without the SQL does not finished? The file TKPROF can be used? because the report is too large. My environment is ORACLE 11.5.9





    Concerning
    Terry

    Yes, you can run a tkprof on one incomplete (or currently being created) trace file.

    HTH
    Srini

Maybe you are looking for

  • Why Airport Extreme disappeared from Airport utility, after adding to the mac feature?

    10.11 OS, with the version of the airport, 6.3.6.  I can see is no longer my Airport Extreme Airport utility after I activated to Mac. I have Airport Extreme base ME91899/A and two Airport Express units model A1392 bolt torque.  I set up the Airport

  • Extreme Seting

    I have a time capsule as the main, extreme router as an extension of another wifi extend, but there are two new Express extended to the extreme, worked well, two days before the discovery of wifi slow down, after the discovery of two Express is not e

  • Password Protection for hard drive on the Libretto U100

    I want to protect my data in the event that I should lose my U100. Initially, I asked a password to the Bios, which is quite easy using Toshiba Assist/user password. But even better would be to apply the same password on the hard drive as well. On th

  • Screens of friends do not show up on my watch - and other questions

    I just got a 42mm Apple Watch Sports yesterday and it works for the most part very well. A couple of problems so far- I have an iPhone 6s more and it is common on iOS. I have associated my watch with her. Under the terms of the app shows I have imple

  • HP Pavilion Notebook 15-p004nx: problem in the camera into my laptop

    day support: I have problem in this laptop: http://support.HP.com/us-en/drivers/selfservice/HP-Pavilion-15-P000-notebook-PC-series/6936226/model... My laptop was have windwos 8 instilled on it then I want windwos 7 and all work well only the camera d