Question

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;
Was it helpful?

Solution

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

OTHER TIPS

What line is Oracle raising an error on?

The first error that jumps out at me is the line

IF INSTR(l_columnValue, ',') != 0 OR INSTR(l_columnValue, '"') THEN

INSTR returns a number. The first part of your condition compares the output of INSTR to a number. The second does not-- that would only be valid if INSTR returned a boolean. It sounds like you want

IF INSTR(l_columnValue, ',') != 0 OR INSTR(l_columnValue, '"') != 0 THEN
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top