oracle - Dump table containing CLOB to csv using PL/SQL proc -


i have assembled procedure dump query containing clob columns csv file. seems working fine until encounter query containing dates.

ora-00932: inconsistent datatypes: expected clob got date

is there way dynamically convert dates default string format able use procedure now. or how can refactor if necessary?

create or replace procedure export_query_csv(       p_query     in varchar2,       p_filename  in varchar2)           l_separator   varchar2 (10 char) := ';';     l_dir         varchar2 (128 char)  := 'my_dir';     l_output      utl_file.file_type;     l_thecursor   integer default dbms_sql.open_cursor;     l_columnvalue clob;     l_status      integer;     l_colcnt      number default 0;     l_cnt         number default 0;     l_desctbl     dbms_sql.desc_tab;       l_substrval varchar2(4000) ;     l_offset number :=1;     l_amount number := 3000;     l_cloblen number :=0;   begin     execute immediate 'alter session set nls_date_format = ''dd-mon-yyyy hh24:mi:ss''';      l_output := utl_file.fopen(l_dir, p_filename, 'wb');      dbms_sql.parse(l_thecursor, p_query, dbms_sql.native);      in 1 .. 1000     loop       begin         dbms_sql.define_column(l_thecursor, i, l_columnvalue);         l_colcnt := i;       exception       when others         if ( sqlcode = -1007 )           exit;         else           raise;         end if;       end;     end loop;      dbms_sql.describe_columns( l_thecursor, l_colcnt, l_desctbl );       in 1 .. l_colcnt      loop       utl_file.put_raw(l_output,utl_raw.cast_to_raw('"'));       utl_file.put_raw(l_output,utl_raw.cast_to_raw(l_desctbl(i).col_name));       utl_file.put_raw(l_output,utl_raw.cast_to_raw('"'));       if < l_colcnt         utl_file.put_raw(l_output,utl_raw.cast_to_raw(l_separator));       end if;     end loop;      utl_file.put_raw(l_output,utl_raw.cast_to_raw(chr(13) || chr(10)));      l_status := dbms_sql.execute(l_thecursor);     loop       exit when (dbms_sql.fetch_rows(l_thecursor) <= 0);       in 1 .. l_colcnt       loop         dbms_sql.column_value(l_thecursor, i, l_columnvalue);          l_cloblen := dbms_lob.getlength(l_columnvalue);         while l_offset <= l_cloblen         loop           l_substrval := dbms_lob.substr(l_columnvalue,l_amount,l_offset);           utl_file.put_raw(l_output,utl_raw.cast_to_raw('"'));           utl_file.put_raw(l_output,utl_raw.cast_to_raw(l_substrval));           utl_file.put_raw(l_output,utl_raw.cast_to_raw('"'));           l_offset:=l_offset+l_amount;         end loop;         l_offset := 1;          if < l_colcnt           utl_file.put_raw(l_output,utl_raw.cast_to_raw(l_separator));         end if;       end loop;       utl_file.put_raw(l_output,utl_raw.cast_to_raw(chr(13) || chr(10)));       l_cnt := l_cnt + 1;     end loop;      dbms_sql.close_cursor(l_thecursor);     utl_file.fclose(l_output);  end; 

found myself, following pattern:

-- define columns in 1 .. colcnt loop     if desctab(i).col_type = 2        dbms_sql.define_column(curid, i, numvar);     elsif desctab(i).col_type = 12         dbms_sql.define_column(curid, i, datevar); ......      else         dbms_sql.define_column(curid, i, namevar);      end if; end loop; 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -