Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7803657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:39:20+00:00 2026-06-02T01:39:20+00:00

The following code is based on a popular thread from AskTom, Export to CSV

  • 0

The following code is based on a popular thread from AskTom, Export to CSV

I did amend the original script with some code in the inner loop to handle embedded commas and quote characters.

I cannot get it to compile on Oracle 8i.

I set the value of utl_file_dir using DBA Studio. Then I ran the script using SQL*Worksheet. The error reported indicates that line 46 is where the problem is.

    CREATE OR REPLACE PROCEDURE dump_table_to_csv
            ( p_tname IN VARCHAR2,
            p_dir   IN VARCHAR2,
            p_filename IN VARCHAR2 )
    IS

    l_output        utl_file.file_type;
    l_theCursor     INTEGER DEFAULT dbms_sql.open_cursor;
    l_columnValue   VARCHAR2(4000);
    l_status        INTEGER;
    l_query         VARCHAR2(1000) DEFAULT 'select * from ' || p_tname;
    l_colCnt        NUMBER := 0;
    l_separator     VARCHAR2(1);
    l_descTbl       dbms_sql.desc_tab;
    l_quote    VARCHAR2(1);

    BEGIN
            l_output := utl_file.fopen( p_dir, p_filename, 'w', 32760 );
            -- adjust date so the format is compatible with the target system
            -- in this case, PostgreSQL 9
            EXECUTE IMMEDIATE 'alter session set nls_date_format=''yyyy-mm-dd hh24:mi:ss'' ';
            -- the original version used the "rr" moniker for year.
            -- execute immediate 'alter session set nls_date_format=''rr-mm-dd hh24:mi:ss'' ';

            -- set up first row with column names
            dbms_sql.parse(  l_theCursor,  l_query, dbms_sql.native );
            dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

            FOR i IN 1 .. l_colCnt LOOP
                    utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
                    dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
                    l_separator := ',';
            END LOOP;

            utl_file.new_line( l_output );
            l_status := dbms_sql.EXECUTE(l_theCursor);

            -- iterate through the data, for each row check each column
            WHILE ( dbms_sql.fetch_rows(l_theCursor) > 0 ) LOOP
                    l_separator := '';

                    FOR i IN 1 .. l_colCnt LOOP
                            dbms_sql.column_value( l_theCursor, i, l_columnValue );
                            --
                            -- if the separator or quote is embedded in the value then enclose in double-quotes
                            IF INSTR(l_columnValue, ',') != 0 OR INSTR(l_columnValue, '"') THEN
                                    l_quote := '"';
                                    -- double any/all embedded quotes
                                    l_columnValue := REPLACE(l_columnValue,'"','""');
                            ELSE
                                    l_quote := '';
                            END IF;
                            l_columnValue := l_separator || l_quote || l_columnValue || l_quote;
                            --
                            -- write the value to disk
                            utl_file.put( l_output, l_separator || l_columnValue );
                            l_separator := ',';
                    END LOOP;

                    utl_file.new_line( l_output );
            END LOOP;

            dbms_sql.close_cursor(l_theCursor);
            utl_file.fclose( l_output );

            EXECUTE IMMEDIATE 'alter session set nls_date_format=''dd-MON-yy'' ';

    EXCEPTION
            WHEN OTHERS THEN
                    EXECUTE IMMEDIATE 'alter session set nls_date_format=''dd-MON-yy'' ';
                    RAISE;
    END;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:39:21+00:00Added an answer on June 2, 2026 at 1:39 am

    You are missing the != 0 for the second instr() call in line 46:

    if instr(l_columnValue, ',') != 0 or instr(l_columnValue, '"') then
    

    should be:

    if instr(l_columnValue, ',') != 0 or instr(l_columnValue, '"') != 0 then
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following is based on the code that I posted on this thread .
i am using the following code(based on this http://goo.gl/5HhSx ) to calculate the difference
I have the following HTML code based on a tab menu format, i.e: <div
I have the following code that creates a submenu based on current document. The
In the following code, the stack-based variable 'ex' is thrown and caught in a
I have the following code to create a music playlist based on files in
I'm using the following code to cause a ul based dropdown menu to open,
Gday All, The following code displays a div based on the option that has
My question is concerning SQL connection status, load, etc. based on the following code:
I have the following test code (based on standard monodroid HelloWorld) namespace TestGREF {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.