oracle使用存儲過程將表數(shù)據(jù)以excel格式導(dǎo)出的操作方法
雖然目前pl/sql developer等數(shù)據(jù)庫客戶端軟件都支持將表數(shù)據(jù)以excel格式導(dǎo)出,但是如果數(shù)據(jù)量大,需要等客戶端加載表數(shù)據(jù)等待很久。而且,可能會遇到定時以excel格式導(dǎo)出數(shù)據(jù)的要求。因此我自己寫了一個使用存儲過程將表數(shù)據(jù)以excel格式導(dǎo)出的存儲過程。
- 服務(wù)端新建目錄
create directory DIR_EXCEL as 'D:\DIR_EXCEL'; - 新建存儲過程
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; --定義一個文件類型變量
str1 varchar2(20000); --定義一個字符串變量,用于存儲表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; --定義一個文件類型變量
BEGIN
--打開一個文件,指定目錄對象、文件名和寫入模式
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)用存儲過程call pr_export_to_excel('TEST','NAME='''123''');
4.去目錄'D:\DIR_EXCEL'取出TEST.xls文件
到此這篇關(guān)于oracle使用存儲過程將表數(shù)據(jù)以excel格式導(dǎo)出的文章就介紹到這了,更多相關(guān)oracle 表數(shù)據(jù)excel格式導(dǎo)出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Oracle查詢優(yōu)化之高效實(shí)現(xiàn)僅查詢前10條記錄的方法與實(shí)踐
這篇文章主要介紹了Oracle查詢優(yōu)化之高效實(shí)現(xiàn)僅查詢前10條記錄的相關(guān)資料,包括使用ROWNUM、ROW_NUMBER()函數(shù)、FETCH?FIRST(適用于Oracle12c及以上版本)以及LIMIT風(fēng)格(僅適用于兼容模式),需要的朋友可以參考下2025-01-01
oracle誤刪數(shù)據(jù)恢復(fù)方法小結(jié)
最近幫客戶維護(hù)一個數(shù)據(jù)庫,數(shù)據(jù)庫中的數(shù)據(jù)被他誤刪了,下面就為大家分享下我是通過什么方法來實(shí)現(xiàn)的2015-08-08
Oracle 數(shù)據(jù)庫中創(chuàng)建合理的數(shù)據(jù)庫索引
在Oracle數(shù)據(jù)庫中,創(chuàng)建索引雖然比較簡單。但是要合理的創(chuàng)建索引則比較困難了。2009-06-06
解決The?Network?Adapter?could?not?establish?the?conn問題
這篇文章主要介紹了解決The?Network?Adapter?could?not?establish?the?conn問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
oracle數(shù)據(jù)與文本導(dǎo)入導(dǎo)出源碼示例
這篇文章主要介紹了oracle數(shù)據(jù)與文本導(dǎo)入導(dǎo)出源碼示例,具有一定參考價值,需要的朋友可以了解下。2017-10-10
PLSQL?Developer13.0.4最新注冊碼和使用教程詳解
PL/SQL?Developer?13是一個集成開發(fā)環(huán)境,專門用于開發(fā)?Oracle?數(shù)據(jù)庫的存儲程序單元。這篇文章主要介紹了PLSQL?Developer13.0.4注冊和使用教程,需要的朋友可以參考下2021-12-12

