欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

oracle使用存儲(chǔ)過程將表數(shù)據(jù)以excel格式導(dǎo)出的操作方法

 更新時(shí)間:2024年07月25日 09:56:57   作者:追求完美9196  
雖然目前pl/sql developer等數(shù)據(jù)庫客戶端軟件都支持將表數(shù)據(jù)以excel格式導(dǎo)出,但是如果數(shù)據(jù)量大,需要等客戶端加載表數(shù)據(jù)等待很久,這篇文章給大家分享oracle使用存儲(chǔ)過程將表數(shù)據(jù)以excel格式導(dǎo)出的操作方法,需要的朋友可以參考下

雖然目前pl/sql developer等數(shù)據(jù)庫客戶端軟件都支持將表數(shù)據(jù)以excel格式導(dǎo)出,但是如果數(shù)據(jù)量大,需要等客戶端加載表數(shù)據(jù)等待很久。而且,可能會(huì)遇到定時(shí)以excel格式導(dǎo)出數(shù)據(jù)的要求。因此我自己寫了一個(gè)使用存儲(chǔ)過程將表數(shù)據(jù)以excel格式導(dǎo)出的存儲(chǔ)過程。

  • 服務(wù)端新建目錄
    create directory DIR_EXCEL as 'D:\DIR_EXCEL';
  • 新建存儲(chǔ)過程
create or replace procedure pr_export_to_excel(p_table_name      varchar2,
                                               p_where_predicate varchar2 default null) is
  /*
  propose:根據(jù)表名和where條件生成excel
  p_where_predicate:where條件語句
  */
  out_file          utl_file.file_type; --定義一個(gè)文件類型變量
  str1              varchar2(20000); --定義一個(gè)字符串變量,用于存儲(chǔ)表1的字段名
  str1_chr          varchar2(30000);
  l_sql             varchar2(20000);
  l_where_predicate varchar2(30000) default 'where ' || p_where_predicate;
begin
  if p_where_predicate is null then
    l_where_predicate := null;
  end if;
  --查詢表1的字段名,用制表符分隔,并賦值給str1
  select listagg(column_name, chr(9)) within group(order by column_id)
    into str1
    from user_tab_columns
   where table_name = upper(p_table_name);
  --查詢表1的字段名,用制表符分隔,并賦值給str1_chr
  select listagg(case
                   when t.DATA_TYPE = 'DATE' OR t.DATA_TYPE LIKE 'TIMESTAMP%' THEN
                    'to_char(f_cur.' || column_name || ',''YYYYMMDD HH24:MI:SS'')'
                   else
                    'f_cur.' || column_name
                 END,
                 '||chr(9)||') within group(order by column_id)
    into str1_chr
    from user_tab_columns t
   where table_name = upper(p_table_name);
  l_sql := '
  declare
    out_file utl_file.file_type; --定義一個(gè)文件類型變量
  BEGIN
    --打開一個(gè)文件,指定目錄對(duì)象、文件名和寫入模式
    out_file := utl_file.fopen('' DIR_EXCEL '',
                               ''' || p_table_name ||
           '.xls '',
                               '' W '',
                               32767);
    utl_file.put_line(out_file,
                      ''' || str1 ||
           '''); --寫入字段名,換行
    for f_cur in (select *
                    from ' || p_table_name || ' t ' ||
           l_where_predicate || ') loop
      utl_file.put_line(out_file, ' || str1_chr || ');
    end loop;
    utl_file.fclose(out_file);
  exception
    when others then
      utl_file.fclose(out_file); --關(guān)閉文件,防止異常關(guān)閉
      dbms_output.put_line(SQLERRM);
      dbms_output.put_line(dbms_utility.format_error_backtrace);
      raise; --拋出異常信息
  end;
  ';
  dbms_output.put_line(l_sql);
  --dbms_output.put_line(l_sql);
  execute immediate l_sql;
exception
  when others then
    utl_file.fclose(out_file); --關(guān)閉文件,防止異常關(guān)閉
    dbms_output.put_line(SQLERRM);
    dbms_output.put_line(dbms_utility.format_error_backtrace);
    raise; --拋出異常信息
end pr_export_to_excel;

3.調(diào)用存儲(chǔ)過程
call pr_export_to_excel('TEST','NAME='''123''');

4.去目錄'D:\DIR_EXCEL'取出TEST.xls文件

到此這篇關(guān)于oracle使用存儲(chǔ)過程將表數(shù)據(jù)以excel格式導(dǎo)出的文章就介紹到這了,更多相關(guān)oracle 表數(shù)據(jù)excel格式導(dǎo)出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論