Oracle 8i 显示 PLS-00382 : expression is of wrong type, 但我没有看到

标签 oracle csv plsql oracle8i

以下代码基于 AskTom 的热门话题,Export to 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;

最佳答案

第 46 行中的第二个 instr() 调用缺少 != 0:

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

应该是:

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

关于Oracle 8i 显示 PLS-00382 : expression is of wrong type, 但我没有看到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10205442/

相关文章:

java - 将对象数组从 Java 传递到 PLSQL 存储过程

database - 为 Oracle 中的特定用户设置新密码时需要旧密码

python - 使用python在django中将数据库转换为csv

java - 如何使用java从.csv文件中获取最后两条记录?

csv - 通过打印每列中出现的前 5 个数据元素来增强 awk 脚本

javascript - 如何循环这个 PL/SQL 语句,以便它在我的 Google map 上绘制多个标记?

oracle - 您可以使用 PL/SQL 在 Oracle APEX 中动态生成区域吗?

sql - 嵌入式 SQL 与动态 SQL

mysql - SQL 行返回顺序

c# - 驱动程序和提供程序之间的区别