--Freemarker Template

<#assign  files = [
  {"srcfile":"px_st_abschlussziele_amtstat_v.csv", "targettable":"costage_st_abschlussziele_amtstat", "targetfile":"costage_st_abschlussziele_amtstat.unl"},
  {"srcfile":"px_st_abschlussziele_lokal_v.csv", "targettable":"costage_st_abschlussziele_lokal", "targetfile":"costage_st_abschlussziele_lokal.unl"},
  {"srcfile":"px_st_fachsemester_v.csv", "targettable":"costage_st_fachsemester", "targetfile":"costage_st_fachsemester.unl"},
  {"srcfile":"px_st_fakultaeten_v.csv", "targettable":"costage_st_fakultaeten", "targetfile":"costage_st_fakultaeten.unl"},
  {"srcfile":"px_st_hauptstudien_v.csv", "targettable":"costage_st_hauptstudien", "targetfile":"costage_st_hauptstudien.unl"},
  {"srcfile":"px_st_hoererstatus_v.csv", "targettable":"costage_st_hoererstatus", "targetfile":"costage_st_hoererstatus.unl"},
  {"srcfile":"px_st_laender_v.csv", "targettable":"costage_st_laender", "targetfile":"costage_st_laender.unl"},
  {"srcfile":"px_st_land_regionen_v.csv", "targettable":"costage_st_land_regionen", "targetfile":"costage_st_land_regionen.unl"},
  {"srcfile":"px_st_semester_v.csv", "targettable":"costage_st_semester_neu", "targetfile":"costage_st_semester.unl"},
  {"srcfile":"px_st_studienarten_v.csv", "targettable":"costage_st_studienarten", "targetfile":"costage_st_studienarten.unl"},
  {"srcfile":"px_st_studienfaecher_v.csv", "targettable":"costage_st_studienfaecher", "targetfile":"costage_st_studienfaecher.unl"},
  {"srcfile":"px_st_studiengaenge_mtsg_v.csv", "targettable":"costage_st_studiengaenge_mtsg", "targetfile":"costage_st_studiengaenge_mtsg.unl"},
  {"srcfile":"px_st_studiengaenge_v.csv", "targettable":"costage_st_studiengaenge_neu", "targetfile":"costage_st_studiengaenge.unl"},
  {"srcfile":"px_st_studienkennzahlen_v.csv", "targettable":"costage_st_studienkennzahlen_neu", "targetfile":"costage_st_studienkennzahlen.unl", "specialrule":""},
  {"srcfile":"px_st_studienstatus_v.csv", "targettable":"costage_st_studienstatus", "targetfile":"costage_st_studienstatus.unl"},
  {"srcfile":"px_st_studien_spoversionen_v.csv", "targettable":"costage_st_studien_spoversionen", "targetfile":"costage_st_studien_spoversionen.unl"},
  {"srcfile":"px_st_studien_v.csv", "targettable":"costage_st_studien", "targetfile":"costage_st_studien.unl"},
  {"srcfile":"px_st_studierendenstammdaten_v.csv", "targettable":"costage_st_studierendenstammdaten_neu", "targetfile":"costage_st_studierendenstammdaten.unl", "specialrule":"update costage_st_studierendenstammdaten_neu set aufnahmedatum=now() where st_studstamm_nr=419856"},
  {"srcfile":"px_st_hzb_v.csv", "targettable":"costage_st_hzb", "targetfile":"costage_st_hzb.unl", "specialrule":""},
  {"srcfile":"px_bw_bewerbungen_v.csv", "targettable":"costage_bw_bewerbungen", "targetfile":"costage_bw_bewerbungen.unl"},
  {"srcfile":"px_bw_bewerbung_kombination_v.csv", "targettable":"costage_bw_bewerbung_kombination", "targetfile":"costage_bw_bewerbung_kombination.unl"},
  {"srcfile":"px_bw_bewerb_hzb_v.csv", "targettable":"costage_bw_bewerb_hzb", "targetfile":"costage_bw_bewerb_hzb.unl"},
  {"srcfile":"px_bw_bewerb_sb_status_v.csv", "targettable":"costage_bw_bewerb_sb_status", "targetfile":"costage_bw_bewerb_sb_status.unl"},
  {"srcfile":"px_bw_sb_status_v.csv", "targettable":"costage_bw_sb_status", "targetfile":"costage_bw_sb_status.unl"},
  {"srcfile":"px_bw_sb_status_werte_v.csv", "targettable":"costage_bw_sb_status_werte", "targetfile":"costage_bw_sb_status_werte.unl"},
  {"srcfile":"px_bw_sb_stat_vorl_zuordnung_v.csv", "targettable":"costage_bw_sb_stat_vorl_zuordnung", "targetfile":"costage_bw_sb_stat_vorl_zuordnung.unl"},
  {"srcfile":"px_bw_zulassungsarten_v.csv", "targettable":"costage_bw_zulassungsarten", "targetfile":"costage_bw_zulassungsarten.unl"},
  {"srcfile":"px_land_subregionen_v.csv", "targettable":"costage_land_subregionen", "targetfile":"costage_land_subregionen.unl"},
  {"srcfile":"px_st_studienformen_v.csv", "targettable":"costage_st_studienformen", "targetfile":"costage_st_studienformen.unl"},
  {"srcfile":"px_st_adressen_v.csv", "targettable":"costage_st_adressen", "targetfile":"costage_st_adressen.unl"},
  {"srcfile":"px_st_urlaube_v.csv", "targettable":"costage_st_urlaube", "targetfile":"costage_st_urlaube.unl"},
  {"srcfile":"px_st_urlaubs_typen_v.csv", "targettable":"costage_st_urlaubs_typen", "targetfile":"costage_st_urlaubs_typen.unl"},
  {"srcfile":"px_st_abmeldungsarten_v.csv", "targettable":"costage_st_abmeldungsarten", "targetfile":"costage_st_abmeldungsarten.unl"},
  {"srcfile":"px_st_personen_abmeldungen_v.csv", "targettable":"costage_st_personen_abmeldungen", "targetfile":"costage_st_personen_abmeldungen.unl"},
  {"srcfile":"px_st_ersthochschulen_v.csv", "targettable":"costage_st_ersthochschulen", "targetfile":"costage_st_ersthochschulen.unl"},
  {"srcfile":"sx_st_hochschulsemester_v.csv", "targettable":"costage_st_hochschulsemester", "targetfile":"costage_st_hochschulsemester.unl"},
  {"srcfile":"sx_leistungen_v.csv", "targettable":"costage_leistungen", "targetfile":"costage_leistungen.unl"},
  {"srcfile":"px_leistungen_detail_typen_v.csv", "targettable":"costage_leistungen_detail_typen", "targetfile":"costage_leistungen_detail_typen.unl"},
  {"srcfile":"px_leistungen_typen_v.csv", "targettable":"costage_leistungen_typen", "targetfile":"costage_leistungen_typen.unl"},
  {"srcfile":"px_antritte_v.csv", "targettable":"costage_antritte", "targetfile":"costage_antritte.unl"},
  {"srcfile":"px_leistungen_status_typen_v.csv", "targettable":"costage_leistungen_status_typen", "targetfile":"costage_leistungen_status_typen.unl"},
  {"srcfile":"px_bw_hzb_arten_v.csv", "targettable":"costage_bw_hzb_arten", "targetfile":"costage_bw_hzb_arten.unl"}
  ] />

\! echo "Folgende Dateien lassen sich laden:"
  
<#foreach file in files>
\! echo "${file.srcfile}"
</#foreach>
<#foreach file in files>

truncate table ${file.targettable};
vacuum full ${file.targettable};

--falls sie nicht existieren werden sie mit 0 bytes angelegt:
\! touch 'unl/${file.srcfile}'
\! sx_recode_iso2utf.x unl/${file.srcfile}

\copy ${file.targettable} from 'unl/${file.srcfile}' with delimiter ',' quote '"'  null as '' csv header encoding 'UTF-8'

<#if file.specialrule?exists>

${file.specialrule};

</#if>

\! sx_unload_table.x ${file.targettable} unl/${file.targetfile}
</#foreach>
