请教oracle百万级表数据导出到excle2007中
答案:2 悬赏:20 手机版
解决时间 2021-02-22 18:00
- 提问者网友:伴风望海
- 2021-02-21 22:25
请教oracle百万级表数据导出到excle2007中
最佳答案
- 五星知识达人网友:夜风逐马
- 2021-02-22 00:00
可以使用plsql 导出为csv格式的文件中,再使用excel 2007打开即可。
示例内容:
CREATE OR REPLACE PROCEDURE SQL_TO_CSV2
(
P_QUERY IN VARCHAR2, -- PLSQL文
P_DIR IN VARCHAR2, -- 导出的文件放置目录
P_FILENAME IN VARCHAR2 -- CSV名
)
IS
L_THECURSOR INTEGER DEFAULT DBMS_SQL.OPEN_CURSOR;
L_COLCNT NUMBER := 0;
L_SEPARATOR VARCHAr2(1) := ',';
L_DESCTBL DBMS_SQL.DESC_TAB;
P_MAX_LINESIZE NUMBER := 32000;
lv_sql VARCHAr2(32000);
BEGIN
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_DATE_FORMAT=''YYYYMMDD HH24:MI:SS''';
lv_sql := '
DECLARE
L_OUTPUT UTL_FILE.FILE_TYPE;
L_ROW Varchar2(32000) := NULL;
CURSOR c IS '||P_QUERY||';
TYPE tp_rows IS TABLE OF c%ROWTYPE INDEX BY PLS_INTEGER;
r tp_rows;
BEGIN
L_OUTPUT := UTL_FILE.FOPEN('''||P_DIR||''', '''||P_FILENAME||'.tmp'', ''W'', '||P_MAX_LINESIZE||');
OPEN c;
LOOP
FETCH c BULK COLLECT INTO r LIMIT 2000;
FOR i IN 1..r.COUNT LOOP
L_ROW := ';
DBMS_SQL.PARSE( L_THECURSOR, P_QUERY, DBMS_SQL.NATIVE );
DBMS_SQL.DESCRIBE_COLUMNS( L_THECURSOR, L_COLCNT, L_DESCTBL );
FOR I IN 1 .. L_COLCNT LOOP
IF i>1 THEN
lv_sql := lv_sql||'||'''||L_SEPARATOR||'''||';
END IF;
lv_sql := lv_sql||'r(i).'||L_DESCTBL(I).COL_NAME;
END LOOP;
DBMS_SQL.CLOSE_CURSOR(L_THECURSOR);
lv_sql := lv_sql||';
UTL_FILE.put_line(L_OUTPUT,L_ROW);
END LOOP;
EXIT WHEN c%NOTFOUND;
END LOOP;
CLOSE c;
UTL_FILE.FCLOSE( L_OUTPUT );
EXCEPTION
WHEN OTHERS THEN
UTL_FILE.FCLOSE( L_OUTPUT );
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
RAISE;
END;';
--DBMS_OUTPUT.PUT_LINE(lv_sql);
EXECUTE IMMEDIATE lv_sql;
-- UTL_FILE.fremove(P_DIR,to_char(sysdate,'yyyymmdd_')|| P_FILENAME||'.csv');
-- UTL_FILE.frename(P_DIR,P_FILENAME||'.tmp',P_DIR,to_char(sysdate,'yyyymmdd_')|| P_FILENAME||'.csv');
END;
/
示例内容:
CREATE OR REPLACE PROCEDURE SQL_TO_CSV2
(
P_QUERY IN VARCHAR2, -- PLSQL文
P_DIR IN VARCHAR2, -- 导出的文件放置目录
P_FILENAME IN VARCHAR2 -- CSV名
)
IS
L_THECURSOR INTEGER DEFAULT DBMS_SQL.OPEN_CURSOR;
L_COLCNT NUMBER := 0;
L_SEPARATOR VARCHAr2(1) := ',';
L_DESCTBL DBMS_SQL.DESC_TAB;
P_MAX_LINESIZE NUMBER := 32000;
lv_sql VARCHAr2(32000);
BEGIN
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_DATE_FORMAT=''YYYYMMDD HH24:MI:SS''';
lv_sql := '
DECLARE
L_OUTPUT UTL_FILE.FILE_TYPE;
L_ROW Varchar2(32000) := NULL;
CURSOR c IS '||P_QUERY||';
TYPE tp_rows IS TABLE OF c%ROWTYPE INDEX BY PLS_INTEGER;
r tp_rows;
BEGIN
L_OUTPUT := UTL_FILE.FOPEN('''||P_DIR||''', '''||P_FILENAME||'.tmp'', ''W'', '||P_MAX_LINESIZE||');
OPEN c;
LOOP
FETCH c BULK COLLECT INTO r LIMIT 2000;
FOR i IN 1..r.COUNT LOOP
L_ROW := ';
DBMS_SQL.PARSE( L_THECURSOR, P_QUERY, DBMS_SQL.NATIVE );
DBMS_SQL.DESCRIBE_COLUMNS( L_THECURSOR, L_COLCNT, L_DESCTBL );
FOR I IN 1 .. L_COLCNT LOOP
IF i>1 THEN
lv_sql := lv_sql||'||'''||L_SEPARATOR||'''||';
END IF;
lv_sql := lv_sql||'r(i).'||L_DESCTBL(I).COL_NAME;
END LOOP;
DBMS_SQL.CLOSE_CURSOR(L_THECURSOR);
lv_sql := lv_sql||';
UTL_FILE.put_line(L_OUTPUT,L_ROW);
END LOOP;
EXIT WHEN c%NOTFOUND;
END LOOP;
CLOSE c;
UTL_FILE.FCLOSE( L_OUTPUT );
EXCEPTION
WHEN OTHERS THEN
UTL_FILE.FCLOSE( L_OUTPUT );
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
RAISE;
END;';
--DBMS_OUTPUT.PUT_LINE(lv_sql);
EXECUTE IMMEDIATE lv_sql;
-- UTL_FILE.fremove(P_DIR,to_char(sysdate,'yyyymmdd_')|| P_FILENAME||'.csv');
-- UTL_FILE.frename(P_DIR,P_FILENAME||'.tmp',P_DIR,to_char(sysdate,'yyyymmdd_')|| P_FILENAME||'.csv');
END;
/
全部回答
- 1楼网友:杯酒困英雄
- 2021-02-22 00:38
任务占坑
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯