zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    搭建企业级Docker Registry -- Harbor
    搭建私服-docker registry
    CentOS 7.2修改网卡名称
    tomcat错误日志监控脚本
    Openresty+Lua+Redis灰度发布
    Jenkins权限控制-Role Strategy Plugin插件使用
    Rsyslog日志服务搭建
    awk4.0对数组value排序
    Spring-IOC 在非 web 环境下优雅关闭容器
    Spring-IOC bean 生命周期之 Lifecycle 钩子
  • 原文地址:https://www.cnblogs.com/advocate/p/1957563.html
Copyright © 2011-2022 走看看