oracle 如何把表导出csv

前端之家收集整理的这篇文章主要介绍了oracle 如何把表导出csv前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
CREATE OR REPLACE PROCEDURE sql_TO_CSV
(
P_QUERY IN VARCHAR2, --PLsql
P_DIR IN VARCHAR2, --导出的文件放置目录
P_FILENAME IN VARCHAR2 --CSV名
)
IS
L_OUTPUTUTL_FILE.FILE_TYPE;
L_THECURSOR INTEGER DEFAULT DBMS_sql.OPEN_CURSOR;
L_COLUMNVALUEVARCHAR2(4000);
L_STATUS INTEGER ;
L_COLCNTNUMBER:=0;
L_SEPARATORVARCHAR2(1);
L_DESCTBLDBMS_sql.DESC_TAB;
P_MAX_LINESIZENUMBER:=32000;
BEGIN
--OPENFILE
L_OUTPUT:=UTL_FILE.FOPEN(P_DIR,P_FILENAME,‘W’,P_MAX_LINESIZE);
--DEFINEDATEFORMAT
EXECUTE IMMEDIATE‘ ALTER SESSION SET NLS_DATE_FORMAT=’’YYYY-MM-DDHH24:MI:SS’’’;
--OPENCURSOR
DBMS_sql.PARSE(L_THECURSOR,P_QUERY,DBMS_sql.NATIVE);
DBMS_sql.DESCRIBE_COLUMNS(L_THECURSOR,L_COLCNT,L_DESCTBL);
--DUMPTABLECOLUMNNAME
FOR I IN 1..L_COLCNTLOOP
UTL_FILE.PUT(L_OUTPUT,L_SEPARATOR||‘”’||L_DESCTBL(I).COL_NAME||‘”’);
DBMS_sql.DEFINE_COLUMN(L_THECURSOR,I,L_COLUMNVALUE,4000);
L_SEPARATOR:=‘,’;
END LOOP;
UTL_FILE.NEW_LINE(L_OUTPUT);
--EXECUTETHEQUERYSTATEMENT
L_STATUS:=DBMS_sql. EXECUTE (L_THECURSOR);
--DUMPTABLECOLUMNVALUE
WHILE(DBMS_sql.FETCH_ROWS(L_THECURSOR)>0)LOOP
L_SEPARATOR:=‘’;
FOR I IN 1..L_COLCNTLOOP
DBMS_sql.COLUMN_VALUE(L_THECURSOR,L_COLUMNVALUE);
UTL_FILE.PUT(L_OUTPUT,L_SEPARATOR||‘”’||
TRIM(BOTH‘‘ FROM REPLACE (L_COLUMNVALUE,’”’,’””’))||‘”’);
L_SEPARATOR:=‘,’;
END LOOP;
UTL_FILE.NEW_LINE(L_OUTPUT);
END LOOP;
--CLOSECURSOR
DBMS_sql.CLOSE_CURSOR(L_THECURSOR);
--CLOSEFILE
UTL_FILE.FCLOSE(L_OUTPUT);
EXCEPTION
WHEN OTHERS THEN
RAISE;
END ;
原文链接:https://www.f2er.com/oracle/211761.html

猜你在找的Oracle相关文章