prog:plsql_spool_utf_8_file
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende Überarbeitung | |||
prog:plsql_spool_utf_8_file [2016/11/10 20:21] – [Export Code erstellen] gpipperr | prog:plsql_spool_utf_8_file [2016/11/22 20:42] (aktuell) – [Export Code erstellen] gpipperr | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | =====Oracle PL/SQL - Generisch aus einer Tabelle eine CSV Datei erstellen - UTL_FILE und DBMS_SQL einsetzen===== | ||
+ | **ab 10g** | ||
+ | |||
+ | Aufgabe: Aus mehreren Tabellen sollen separate CSV Dateien erstellt werden. Dazu soll aber nicht jedesmal einen neue Routine geschrieben werden. | ||
+ | |||
+ | ==== Lösung A - SQLcl ==== | ||
+ | |||
+ | In SQLcl ist das bereits integriert siehe => [[dba: | ||
+ | |||
+ | |||
+ | Beispiel für SQLcl: | ||
+ | <code sql> | ||
+ | |||
+ | spool emp.csv | ||
+ | select /*csv*/ from emp; | ||
+ | spool off | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ==== Lösung B - Pl/SQL ganz klassisch ==== | ||
+ | |||
+ | |||
+ | === Vorbereitung === | ||
+ | |||
+ | Export Directory von SYS anlegen lassen: | ||
+ | <code sql> | ||
+ | create directory EXPORT_DIR as ' | ||
+ | grant read on directory EXPORT_DIR to scott; | ||
+ | grant write on directory EXPORT_DIR to scott; | ||
+ | </ | ||
+ | |||
+ | |||
+ | === Export Code erstellen === | ||
+ | |||
+ | Mit Hilfe von DBMS_SQL und UTL_FILE (put_line_NCHAR für UTF8) wird ein SQL dynamisch auswertet und in eine Datei geschrieben. | ||
+ | |||
+ | Umsetzung: | ||
+ | <code plsql createCSVFile.sql > | ||
+ | create or replace procedure createCSVFile(p_filename | ||
+ | , p_sql | ||
+ | , p_headerline | ||
+ | , p_printTable_header boolean default false | ||
+ | , p_printHeader_line | ||
+ | , p_print_utf8 | ||
+ | is | ||
+ | | ||
+ | v_cur | ||
+ | v_cur_pointer | ||
+ | v_column_count | ||
+ | v_column_desc | ||
+ | v_column_value | ||
+ | | ||
+ | |||
+ | v_file utl_file.file_type; | ||
+ | v_rec_count | ||
+ | v_line | ||
+ | v_fetch | ||
+ | v_data_format | ||
+ | begin | ||
+ | |||
+ | -- open file | ||
+ | if p_print_utf8 then | ||
+ | v_file := UTL_FILE.FOPEN_NCHAR (' | ||
+ | else | ||
+ | v_file := UTL_FILE.FOPEN (' | ||
+ | end if; | ||
+ | | ||
+ | | ||
+ | --write header line | ||
+ | if p_printHeader_line then | ||
+ | if p_print_utf8 then | ||
+ | utl_file.PUT_line_NCHAR(v_file, | ||
+ | else | ||
+ | utl_file.PUT_line(v_file, | ||
+ | end if; | ||
+ | end if; | ||
+ | |||
+ | |||
+ | -- set the required data format | ||
+ | EXECUTE IMMEDIATE 'alter session set nls_date_format=''' | ||
+ | |||
+ | -- bild the sql | ||
+ | | ||
+ | dbms_output.put_line ('-- Info:: try to execute ' | ||
+ | | ||
+ | |||
+ | -- not in 10g | ||
+ | -- open the cursor | ||
+ | -- open v_cur for v_sql; | ||
+ | -- get pointer to the cursor | ||
+ | -- v_cur_pointer | ||
+ | | ||
+ | v_cur_pointer := DBMS_SQL.OPEN_CURSOR; | ||
+ | |||
+ | sys.dbms_sql.parse(v_cur_pointer, | ||
+ | |||
+ | |||
+ | --get the columnns of this cursor | ||
+ | sys.dbms_sql.describe_columns (c => v_cur_pointer, | ||
+ | | ||
+ | | ||
+ | for i in 1 .. v_column_count loop | ||
+ | v_line | ||
+ | if i < v_column_count then | ||
+ | | ||
+ | end if; | ||
+ | dbms_sql.define_column (v_cur_pointer, | ||
+ | | ||
+ | end loop; | ||
+ | | ||
+ | if p_printTable_header then | ||
+ | dbms_output.put_line ('-- Info:: Header Line ' | ||
+ | if p_print_utf8 then | ||
+ | utl_file.PUT_line_NCHAR(v_file, | ||
+ | else | ||
+ | utl_file.PUT_line(v_file, | ||
+ | end if; | ||
+ | end if; | ||
+ | | ||
+ | v_fetch: | ||
+ | | ||
+ | while (sys.dbms_sql.fetch_rows (v_cur_pointer) > 0) | ||
+ | loop | ||
+ | v_rec_count | ||
+ | v_line | ||
+ | |||
+ | for i in 1 .. v_column_count loop | ||
+ | dbms_sql.column_value (v_cur_pointer, | ||
+ | | ||
+ | v_line | ||
+ | | ||
+ | if i < v_column_count then | ||
+ | v_line | ||
+ | end if; | ||
+ | | ||
+ | end loop; | ||
+ | | ||
+ | --dbms_output.put_line (' | ||
+ | if p_print_utf8 then | ||
+ | utl_file.put_line_nchar(v_file, | ||
+ | else | ||
+ | utl_file.put_line(v_file, | ||
+ | end if; | ||
+ | | ||
+ | end loop; | ||
+ | |||
+ | if v_rec_count = 0 then | ||
+ | | ||
+ | else | ||
+ | | ||
+ | end if; | ||
+ | | ||
+ | -- close the cursor | ||
+ | if sys.dbms_sql.is_open(v_cur_pointer) then | ||
+ | sys.dbms_sql.close_cursor (v_cur_pointer); | ||
+ | end if; | ||
+ | | ||
+ | --close the file | ||
+ | if UTL_FILE.IS_OPEN (v_file) then | ||
+ | utl_file.fflush(v_file); | ||
+ | utl_file.fclose(v_file); | ||
+ | end if; | ||
+ | | ||
+ | exception | ||
+ | when others then | ||
+ | |||
+ | | ||
+ | if UTL_FILE.IS_OPEN (v_file) then | ||
+ | utl_file.fflush(v_file); | ||
+ | utl_file.fclose(v_file); | ||
+ | end if; | ||
+ | |||
+ | -- close the cursor | ||
+ | if sys.dbms_sql.is_open(v_cur_pointer) then | ||
+ | sys.dbms_sql.close_cursor (v_cur_pointer); | ||
+ | end if; | ||
+ | |||
+ | -- Error | ||
+ | | ||
+ | | ||
+ | end createcsvfile; | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | ==== Quellen ==== | ||
+ | |||
+ | Oracle: | ||
+ | * DBMS_SQL => https:// | ||
+ | |||
+ | Web: | ||
+ | * http:// |
prog/plsql_spool_utf_8_file.txt · Zuletzt geändert: 2016/11/22 20:42 von gpipperr