Oracle 8i说PLS-00382:表达式类型错误,但我没有看到它

Mop*_*lly 2 csv oracle plsql oracle8i

以下代码基于AskTom的一个流行线程,导出为CSV

我用内部循环中的一些代码修改了原始脚本来处理嵌入的逗号和引号字符.

我无法在Oracle 8i上编译它.

我使用DBA Studio设置utl_file_dir的值.然后我使用SQL*Worksheet运行脚本.报告的错误表明第46行是问题所在.

    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;
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 6

你错过了!= 0instr()46行的第二个电话:

if instr(l_columnValue, ',') != 0 or instr(l_columnValue, '"') then
Run Code Online (Sandbox Code Playgroud)

应该:

if instr(l_columnValue, ',') != 0 or instr(l_columnValue, '"') != 0 then
Run Code Online (Sandbox Code Playgroud)