一、创建存储过程
ps:将需要导出数据的sql语句替换下面的sql语句和下面的字段
1 CREATE OR REPLACE PROCEDURE Export_GRID_SERV_MON_02(in_cycle_id in number) is 2 3 -- 变量说明 4 v_file_path constant varchar2(30):='DIR1'; 5 v_file_name varchar2(50); 6 v_File_Handle UTL_FILE.FILE_TYPE; 7 v_line varchar2(2000); 8 d_file_name varchar2(50); 9 10 11 icnt number(10); 12 v_start_date varchar(12); 13 v_end_date varchar(12); 14 15 -- 日志变量 16 v_begin_date date := sysdate; 17 v_step varchar2(200) := null; 18 con_run constant number(4) := 9999; 19 v_procedure_id constant number := 37; 20 21 22 cursor c_cust_msg is 23 select 24 BAZ001, 25 BAZ002, 26 BAB221, 27 AAC001, 28 AAB001, 29 AAE002, 30 AAE003, 31 AAE140, 32 BAE060, 33 AAA157, 34 AAA115, 35 AAC084, 36 BAE151, 37 AAE180, 38 AAE020, 39 BAE152, 40 to_char(AAB191,'yyyymmddhh24mi') as AAB191, 41 BAE162, 42 BAB222, 43 BAE205, 44 AAE202, 45 BAE181, 46 BAZ003, 47 BAZ004, 48 AAE011, 49 to_char(AAE036,'yyyymmddhh24mi') as AAE036, 50 AAB034, 51 AAA027, 52 BAE513, 53 BAE519 54 from 55 ncsi.ac43 where aae140='11' and aae002=201802 ; 56 57 begin 58 v_step:='v_step_1'; 59 60 d_file_name :='ncsi_ac43.dat'; 61 DBMS_OUTPUT.enable; 62 DBMS_OUTPUT.put(v_file_path); 63 v_step:='v_step_1_2'; 64 v_File_Handle:=UTL_FILE.FOPEN(v_file_path,d_file_name,'w'); 65 v_step:='v_step_1_3'; 66 67 select to_char(sysdate,'yyyymmddhh24mi') into v_start_date from dual; 68 v_step:='v_step_2'; 69 70 for rec in c_cust_msg loop 71 v_line:=rec.BAZ001||','||rec.BAZ002||','||rec.BAB221||','||rec.AAC001||','||rec.AAB001||','||rec.AAE002||','||rec.AAE003||','||rec.AAE140||','||rec.BAE060||','||rec.AAA157||','||rec.AAA115||','||rec.AAC084||','||rec.BAE151||','||rec.AAE180||','||rec.AAE020||','||rec.BAE152||','||rec.AAB191||','||rec.BAE162||','||rec.BAB222||','||rec.BAE205||','||rec.AAE202||','||rec.BAE181||','||rec.BAZ003||','||rec.BAZ004||','||rec.AAE011||','||rec.AAE036||','||rec.AAB034||','||rec.AAA027||','||rec.BAE513||','||rec.BAE519; 72 73 74 UTL_FILE.PUT_LINE(v_file_handle,v_line); 75 end loop; 76 77 UTL_FILE.FCLOSE(v_file_handle); 78 79 80 81 exception when others then 82 rollback; 83 end;
二、创建DIR1目录
1 create directory DIR1 as '/home/oracle/csv'
三、执行调用存储过程
1 EXEC Export_GRID_SERV_MON_02(20180102);
ps: 大数据导出时间长,建议创建脚本后台执行
1 #!/bin/sh 2 export PATH 3 export ORACLE_BASE=/u01/app/oracle 4 export ORACLE_HOME=$ORACLE_BASE/product/11.2/dbhome_1 5 export ORACLE_SID=auditdb 6 LD_LIBRARY_PATH=$ORACLE_HOME/lib 7 PATH=$PATH:$ORACLE_HOME/bin:$HOME/bin 8 export ORACLE_BASE ORACLE_HOME ORACLE_SID LD_LIBRARY_PATH 9 sqlplus "/ as sysdba"<<EOF 10 @pl_ac43.sql 11 EOF 12 13 [oracle@exchangedb ~]$ more pl_ac43.sql 14 EXEC Export_GRID_SERV_MON_02(20180102); 15 exit 16 17 nohup ./pl_ac43.sql >> ac43.log & #执行shell脚本后台运行