Oracle将数据导出到CSV文件

Oracle将数据导出到CSV文件 

https://blog.csdn.net/huangzhijie3918/article/details/72732816

工作中有需要将线上数据导出到excel给客户分析/查看的情况,如下是方法介绍情况:
utl_file读写文件包 ,1分钟导出的数据量 大概是300万 适用于大量导出时
spool 循环打印 ,适用小型数据量时。    

利用utl_file导出.csv文件.  --.csv逗号分隔值格式文件,可用excel工具打开,显示格式和excel一样..
首先在oracle数据库中建立sql_to_csv存储过程,代码如下:

 
  1. CREATE OR REPLACE PROCEDURE SQL_TO_CSV

  2. (

  3. P_QUERY IN VARCHAR2, -- PLSQL文

  4. P_DIR IN VARCHAR2, -- 导出的文件放置目录

  5. P_FILENAME IN VARCHAR2 -- CSV名

  6. )

  7. IS

  8. L_OUTPUT UTL_FILE.FILE_TYPE;

  9. L_THECURSOR INTEGER DEFAULT DBMS_SQL.OPEN_CURSOR;

  10. L_COLUMNVALUE VARCHAR2(4000);

  11. L_STATUS INTEGER;

  12. L_COLCNT NUMBER := 0;

  13. L_SEPARATOR VARCHAR2(1);

  14. L_DESCTBL DBMS_SQL.DESC_TAB;

  15. P_MAX_LINESIZE NUMBER := 32000;

  16. BEGIN

  17. --OPEN FILE

  18. L_OUTPUT := UTL_FILE.FOPEN(P_DIR, P_FILENAME, 'W', P_MAX_LINESIZE);

  19. --DEFINE DATE FORMAT

  20. EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_DATE_FORMAT=''YYYY-MM-DD HH24:MI:SS''';

  21. --OPEN CURSOR

  22. DBMS_SQL.PARSE(L_THECURSOR, P_QUERY, DBMS_SQL.NATIVE);

  23. DBMS_SQL.DESCRIBE_COLUMNS(L_THECURSOR, L_COLCNT, L_DESCTBL);

  24. --DUMP TABLE COLUMN NAME

  25. FOR I IN 1 .. L_COLCNT LOOP

  26. UTL_FILE.PUT(L_OUTPUT,L_SEPARATOR || '"' || L_DESCTBL(I).COL_NAME || '"'); --输出表字段

  27. DBMS_SQL.DEFINE_COLUMN(L_THECURSOR, I, L_COLUMNVALUE, 4000);

  28. L_SEPARATOR := ',';

  29. END LOOP;

  30. UTL_FILE.NEW_LINE(L_OUTPUT); --输出表字段

  31. --EXECUTE THE QUERY STATEMENT

  32. L_STATUS := DBMS_SQL.EXECUTE(L_THECURSOR);

  33.  
  34. --DUMP TABLE COLUMN VALUE

  35. WHILE (DBMS_SQL.FETCH_ROWS(L_THECURSOR) > 0) LOOP

  36. L_SEPARATOR := '';

  37. FOR I IN 1 .. L_COLCNT LOOP

  38. DBMS_SQL.COLUMN_VALUE(L_THECURSOR, I, L_COLUMNVALUE);

  39. UTL_FILE.PUT(L_OUTPUT,

  40. L_SEPARATOR || '"' ||

  41. TRIM(BOTH ' ' FROM REPLACE(L_COLUMNVALUE, '"', '""')) || '"');

  42. L_SEPARATOR := ',';

  43. END LOOP;

  44. UTL_FILE.NEW_LINE(L_OUTPUT);

  45. END LOOP;

  46. --CLOSE CURSOR

  47. DBMS_SQL.CLOSE_CURSOR(L_THECURSOR);

  48. --CLOSE FILE

  49. UTL_FILE.FCLOSE(L_OUTPUT);

  50. EXCEPTION

  51. WHEN OTHERS THEN

  52. RAISE;

  53. END;

  54.  
  55. /





然后在命令行中执行:

sql>create or replace directory OUT_PATH as 'D:\';


其中OUT_PATH为指定的目标文件存放路径,这是windows系统下的写法,linux下的路径写法请自行脑补。

然后在需要做导出的用户下执行如下查询sql,将拼出数据库表导出语句:

SELECT 'EXEC sql_to_csv(''select * from ' ||T.TABLE_NAME ||''',''OUT_PATH''' || ',''ODS_MDS.' || T.TABLE_NAME ||'.csv'');' FROM user_TABLES T where t.TABLE_NAME='表名'


 

 
  1. 1 EXEC sql_to_csv('select * from TD_DIS_BATCH_CHECK','OUT_PATH','ODS_MDS.TD_DIS_BATCH_CHECK.csv');

  2. 2 EXEC sql_to_csv('select * from TD_DIS_CONTROLL','OUT_PATH','ODS_MDS.TD_DIS_CONTROLL.csv');

  3. 3 EXEC sql_to_csv('select * from TD_DIS_TASK_DATASOURCE','OUT_PATH','ODS_MDS.TD_DIS_TASK_DATASOURCE.csv');

  4. 4 EXEC sql_to_csv('select * from TD_DIS_TASK_DEPENDENT','OUT_PATH','ODS_MDS.TD_DIS_TASK_DEPENDENT.csv');

  5. 5 EXEC sql_to_csv('select * from TD_DIS_TASK_GROUP','OUT_PATH','ODS_MDS.TD_DIS_TASK_GROUP.csv');

  6. 6 EXEC sql_to_csv('select * from TD_DIS_TASK_GROUP_DEPENDENT','OUT_PATH','ODS_MDS.TD_DIS_TASK_GROUP_DEPENDENT.csv');

  7. 7 EXEC sql_to_csv('select * from TD_DIS_TASK_INFO','OUT_PATH','ODS_MDS.TD_DIS_TASK_INFO.csv');

  8. 8 EXEC sql_to_csv('select * from TD_DIS_TASK_PARAMS','OUT_PATH','ODS_MDS.TD_DIS_TASK_PARAMS.csv');

  9. 9 EXEC sql_to_csv('select * from TD_DIS_TASK_TRACK','OUT_PATH','ODS_MDS.TD_DIS_TASK_TRACK.csv');

  10. 10 EXEC sql_to_csv('select * from TD_DIS_TASK_TYPE','OUT_PATH','ODS_MDS.TD_DIS_TASK_TYPE.csv');

  11. 11 EXEC sql_to_csv('select * from TD_OPER','OUT_PATH','ODS_MDS.TD_OPER.csv');

  12. 12 EXEC sql_to_csv('select * from TD_ORGAN','OUT_PATH','ODS_MDS.TD_ORGAN.csv');

  13. 13 EXEC sql_to_csv('select * from TD_PUB_DIC','OUT_PATH','ODS_MDS.TD_PUB_DIC.csv');

  14. 14 EXEC sql_to_csv('select * from TD_PUB_DIC_SUBSU','OUT_PATH','ODS_MDS.TD_PUB_DIC_SUBSU.csv');

  15. 15 EXEC sql_to_csv('select * from TD_SYS_MENUS','OUT_PATH','ODS_MDS.TD_SYS_MENUS.csv');

  16. 16 EXEC sql_to_csv('select * from TD_SYS_ONLINE_USERS','OUT_PATH','ODS_MDS.TD_SYS_ONLINE_USERS.csv');

  17. 17 EXEC sql_to_csv('select * from TD_SYS_POSITION','OUT_PATH','ODS_MDS.TD_SYS_POSITION.csv');

  18. 18 EXEC sql_to_csv('select * from TD_SYS_ROLE_POS','OUT_PATH','ODS_MDS.TD_SYS_ROLE_POS.csv');

  19. 19 EXEC sql_to_csv('select * from TD_SYS_SECURITY_EMP_ROLE_RES','OUT_PATH','ODS_MDS.TD_SYS_SECURITY_EMP_ROLE_RES.csv');

  20. 20 EXEC sql_to_csv('select * from TD_SYS_SECURITY_REL_ROLE_RES','OUT_PATH','ODS_MDS.TD_SYS_SECURITY_REL_ROLE_RES.csv');

  21. 21 EXEC sql_to_csv('select * from TD_SYS_SECURITY_REL_USER_POS','OUT_PATH','ODS_MDS.TD_SYS_SECURITY_REL_USER_POS.csv');

  22. 22 EXEC sql_to_csv('select * from TD_SYS_SECURITY_RESOURCES','OUT_PATH','ODS_MDS.TD_SYS_SECURITY_RESOURCES.csv');

  23. 23 EXEC sql_to_csv('select * from TD_DIS_APP_OPER','OUT_PATH','ODS_MDS.TD_DIS_APP_OPER.csv');

  24. 24 EXEC sql_to_csv('select * from GF_PROD_SELLFEE_MONTH','OUT_PATH','ODS_MDS.GF_PROD_SELLFEE_MONTH.csv');

  25. 25 EXEC sql_to_csv('select * from GF_PROD_TRUSTEEFEE_DAILY','OUT_PATH','ODS_MDS.GF_PROD_TRUSTEEFEE_DAILY.csv');

  26. 26 EXEC sql_to_csv('select * from GF_PROD_TRUSTEEFEE_MONTH','OUT_PATH','ODS_MDS.GF_PROD_TRUSTEEFEE_MONTH.csv');

  27. 27 EXEC sql_to_csv('select * from GF_RETAILER_DAILY_TRADE_NOTE','OUT_PATH','ODS_MDS.GF_RETAILER_DAILY_TRADE_NOTE.csv');

  28. 28 EXEC sql_to_csv('select * from TA_ENDDAY_CTL','OUT_PATH','ODS_MDS.TA_ENDDAY_CTL.csv');

  29. 29 EXEC sql_to_csv('select * from TA_DISPOSABLE_TASK_CTL','OUT_PATH','ODS_MDS.TA_DISPOSABLE_TASK_CTL.csv');

  30. 30 EXEC sql_to_csv('select * from TD_DIS_TASK_INFO_A','OUT_PATH','ODS_MDS.TD_DIS_TASK_INFO_A.csv');

  31. 31 EXEC sql_to_csv('select * from GF_PROD_SELLFEE_DAILY','OUT_PATH','ODS_MDS.GF_PROD_SELLFEE_DAILY.csv');

  32.  





将上面生成的语句在命令行中执行,即可生成对应表的csv文件:

 
  1. SQL> EXEC sql_to_csv('select * from TD_DIS_TASK_INFO','OUT_PATH','ODS_MDS.TD_DIS_TASK_INFO.csv');

  2. PL/SQL procedure successfully completed

  3.  
  4. SQL> EXEC sql_to_csv('select * from TD_DIS_TASK_INFO','OUT_PATH','ODS_MDS.TD_DIS_TASK_INFO.csv');

  5. PL/SQL procedure successfully completed

  6.  
  7. SQL> EXEC sql_to_csv('select * from TD_DIS_TASK_INFO','OUT_PATH','ODS_MDS.TD_DIS_TASK_INFO.csv');

  8. PL/SQL procedure successfully completed

  9.  
  10. SQL> EXEC sql_to_csv('select * from TD_DIS_TASK_INFO_a','OUT_PATH','ODS_MDS.TD_DIS_TASK_INFO_a.csv');

你可能感兴趣的:(Oracle将数据导出到CSV文件)