1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| spool exp_utl
prompt 'export blabla'
set serveroutput on
DECLARE
-- Declaration des variables
err_num number;
err_msg varchar2(200);
wobs varchar2(200); -- Observation
wout utl_file.file_type; -- type de fichier genere
wrep_fic varchar2(255) := null; -- repertoire d'ecriture
wnom_fic varchar2(255) := null; -- fichier d'ecriture
wligne varchar2(250); -- ligne à insérer dans le fichier
cursor c_lst is
select champs1, chamsp2 from matable;
BEGIN
--Definition des parametres du fichier
wobs := 'Rech rep';
wrep_fic := '/doss1/doss2';
wnom_fic := 'Lst_exp_utl.csv';
wobs := 'Ouverture du fichier.doc';
wout := utl_file.fopen(wrep_fic, wnom_fic, 'w');
wobs := 'Ligne d''entête';
wligne := 'champs 1'||';'||'champs 2';
utl_file.put_line(wout,wligne);
wobs := 'Ecriture des lignes dans fichier';
for r_lt in c_lst loop
wobs := 'Build ligne';
wligne := r_lt.champs1||';'||champs2;
wobs := 'Ecriture de la ligne dans fichier';
utl_file.put_line(wout,wligne);
end loop;
wobs := 'Fermeture du fichier';
utl_file.fclose(wout);
dbms_output.put_line('nb acteur csv = '||to_char(wcnt_act));
EXCEPTION
when utl_file.invalid_operation then
dbms_output.put_line('obs : ' ||wobs);
utl_file.fclose(wout);
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 60);
-- si le fichier est ouvert, on le ferme
if utl_file.is_open(wout) = TRUE then
utl_file.fclose(wout) ;
end if ;
raise_application_error(-20001,'ERREUR:'|| err_msg);
when utl_file.invalid_filehandle then
dbms_output.put_line('obs : ' ||wobs);
utl_file.fclose(wout);
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 60);
-- si le fichier est ouvert, on le ferme
if utl_file.is_open(wout) = TRUE then
utl_file.fclose(wout) ;
end if ;
raise_application_error(-20001,'ERREUR:'|| err_msg);
when utl_file.write_error then
dbms_output.put_line('obs : ' ||wobs);
utl_file.fclose(wout);
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 60);
-- si le fichier est ouvert, on le ferme
if utl_file.is_open(wout) = TRUE then
utl_file.fclose(wout) ;
end if ;
raise_application_error(-20001,'ERREUR:'|| err_msg);
when others then
dbms_output.put_line('obs : ' ||wobs);
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 60);
-- si le fichier est ouvert, on le ferme
if utl_file.is_open(wout) = TRUE then
utl_file.fclose(wout) ;
end if ;
raise_application_error(-20001,'ERREUR:'|| err_msg);
END;
/
show err
spool off |
Partager