• oracle dump_csv


    1、首先提供函数dump_csv:

    1 create or replace function dump_csv(p_query in varchar2,
    2 p_separator in varchar2 default ',',
    3 p_dir in varchar2,
    4 p_filename in varchar2) return number
    5 AUTHID CURRENT_USER is
    6 l_output utl_file.file_type;
    7 l_theCursor integer default dbms_sql.open_cursor;
    8 l_columnValue varchar2(2000);
    9 l_status integer;
    10 l_colCnt number default 0;
    11 l_separator varchar2(10) default '';
    12 l_cnt number default 0;
    13  begin
    14 l_output := utl_file.fopen(p_dir, p_filename, 'w');
    15 dbms_sql.parse(l_theCursor, p_query, dbms_sql.native);
    16 for i in 1 .. 255 loop
    17 begin
    18 dbms_sql.define_column(l_theCursor, i, l_columnValue, 2000);
    19 l_colCnt := i;
    20 exception
    21 when others then
    22 if (sqlcode = -1007) then
    23 exit;
    24 else
    25 raise;
    26 end if;
    27 end;
    28 end loop;
    29 dbms_sql.define_column(l_theCursor, 1, l_columnValue, 2000);
    30 l_status := dbms_sql.execute(l_theCursor);
    31 loop
    32 exit when(dbms_sql.fetch_rows(l_theCursor) <= 0);
    33 l_separator := '';
    34 for i in 1 .. l_colCnt loop
    35 dbms_sql.column_value(l_theCursor, i, l_columnValue);
    36 utl_file.put(l_output, l_separator || '"' || l_columnValue || '"');
    37 l_separator := p_separator;
    38 end loop;
    39 utl_file.new_line(l_output);
    40 l_cnt := l_cnt + 1;
    41 end loop;
    42 dbms_sql.close_cursor(l_theCursor);
    43 utl_file.fclose(l_output);
    44 return l_cnt;
    45  end dump_csv;

    2、创建Directory并授权:

    conn sys/password as sysdba;

    create directory TEMP as 'c:\temp';

    grant read,write on directory TEMP to tt;

    3、使用:

    conn tt/password;

    select dump_csv('select * from scott.emp',',','TEMP','emp.csv') from dual;

  • 相关阅读:
    Python 多线程、进程
    Python网络编程 Socket编程
    Python基础7 面向对象编程进阶
    Python基础6 面向对象编程
    Python基础5 常用模块学习
    Python基础4 迭代器、装饰器、软件开发规范
    Python基础3 函数、递归、内置函数
    Python基础2 列表 字典 集合
    21-Python-多进程
    20-Python-queue队列
  • 原文地址:https://www.cnblogs.com/advocate/p/1957563.html
Copyright © 2020-2023  润新知