clob取sql

declare
  l_cursor   integer := dbms_sql.open_cursor;
  l_n        number;
  l_long_val varchar2(32767);
  l_long_len number;
  l_curpos   number;
  r_path     varchar2(100) := 'SQL_DIR';  /*需要在DB创建此directory用于存放输出的sql文本*/
  r_file     utl_file.file_type;
  l_the_rest varchar2(2000);
begin
  dbms_sql.parse(l_cursor,
                 'select sql_text
                                from sql_temp20120921
                               where hash_value = :x',
                 dbms_sql.native);
/*sql_temp20120921表为存放抓取到的sql的表,sql_text为包含clob的字段,请修改为实际表和实际字段*/
  dbms_sql.define_column_long(l_cursor, 1);

  for cc1 in (select hash_value from sql_temp20120921 order by hash_value) loop
  /*hash value或者sql id根据实际修改*/
    if (utl_file.is_open(r_file)) then
      utl_file.fclose(r_file);
    end if;
    r_file := utl_file.fopen(r_path, cc1.hash_value || '.sql', 'w', 32765);
 
    utl_file.new_line(r_file);
    dbms_sql.bind_variable(l_cursor, ':x', cc1.hash_value);
 
    l_curpos := 0;
    l_n      := dbms_sql.execute(l_cursor);
    if dbms_sql.fetch_rows(l_cursor) > 0 then
      loop
        dbms_sql.column_value_long(c            => l_cursor,
                                   position     => 1,
                                   length       => 30000,
                                   offset       => l_curpos,
                                   value        => l_long_val,
                                   value_length => l_long_len);
        l_long_val := l_the_rest || l_long_val;
        loop
          l_n := instr(l_long_val, ',');
          exit when nvl(l_n, 0) = 0;
          utl_file.put_line(r_file, substr(l_long_val, 1, l_n - 1));
          l_long_val := substr(l_long_val, l_n + 1);
       
        end loop;
        l_the_rest := l_long_val;
     
        l_curpos := l_curpos + l_long_len;
        exit when l_long_len = 0;
      end loop;
   
      utl_file.put_line(r_file, substr(l_long_val, 1));
      l_the_rest := '';
    end if;
  end loop;

  if (utl_file.is_open(r_file)) then
    utl_file.fclose(r_file);
  end if;
  dbms_sql.close_cursor(l_cursor);
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值