Created
May 15, 2019 08:37
-
-
Save RainerRoss/f6e304934f9e16b1365e5737cd22de58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ctl-opt dftactgrp(*no); | |
//------------------------------------------------------------------// | |
// // | |
// Einlesen einer CSV-Datei und Verarbeiten mit Split() // | |
// // | |
//----------------- // | |
// R.Ross 05.2019 * // | |
//------------------------------------------------------------------// | |
// SQL-Options // | |
//------------------------------------------------------------------// | |
exec sql set option datfmt=*iso, timfmt=*iso, commit=*chg, | |
closqlcsr=*endactgrp; | |
//------------------------------------------------------------------// | |
// Array Datei Kunde // | |
//------------------------------------------------------------------// | |
dcl-ds DsKUNDE extname('KUNDEN00') qualified end-ds; | |
//------------------------------------------------------------------// | |
// Array Kunde // | |
//------------------------------------------------------------------// | |
dcl-ds Kunde qualified inz; | |
Id like(DsKunde.Id); | |
Name like(DsKunde.Name); | |
PLZ like(DsKunde.PLZ); | |
Ort like(DsKunde.Ort); | |
Strasse like(DsKunde.Strasse); | |
end-ds; | |
//------------------------------------------------------------------// | |
// Array Records - wird mit SQL gefüllt // | |
//------------------------------------------------------------------// | |
dcl-ds DsRecords qualified dim(10000) inz; | |
Id int(10); | |
Text varchar(256); | |
end-ds; | |
//------------------------------------------------------------------// | |
// Array Data - wird mit SQL gefüllt // | |
//------------------------------------------------------------------// | |
dcl-ds DsData qualified dim(10) inz; | |
Pos int(10); | |
Text varchar(50); | |
end-ds; | |
//------------------------------------------------------------------// | |
// Verarbeitung // | |
//------------------------------------------------------------------// | |
main(); | |
*inlr = *on; | |
//------------------------------------------------------------------// | |
// Main // | |
//------------------------------------------------------------------// | |
dcl-proc main; | |
dcl-s LocFile varchar(256); | |
dcl-s LocIndex uns(10); | |
dcl-s LocCounter uns(10); | |
LocFile = '/home/import/csv/adressen.csv'; | |
exec sql declare cursor01 cursor for | |
select Ordinal_Position, Element | |
from table ( | |
systools.split(get_clob_from_file(:LocFile), chr(10))); | |
exec sql open cursor01; | |
exec sql fetch cursor01 for 10000 rows // Fetch into Array | |
into :DsRecords; | |
if sqlcode >= *zero; | |
exec sql GET DIAGNOSTICS :LocCounter = ROW_COUNT; | |
if LocCounter > *zero; | |
for LocIndex = 1 to LocCounter; | |
splitten(DsRecords(LocIndex).Text); | |
endfor; | |
endif; | |
endif; | |
exec sql close cursor01; | |
end-proc; | |
//------------------------------------------------------------------// | |
// Datensatz in Felder splitten // | |
//------------------------------------------------------------------// | |
dcl-proc splitten; | |
dcl-pi *n; | |
PiRecord varchar(256) const options(*varsize:*trim); | |
end-pi; | |
dcl-s LocCounter uns(10); | |
exec sql declare cursor02 cursor for | |
select Ordinal_Position, Element | |
from table (systools.split(:PiRecord, ',')); | |
exec sql open cursor02; | |
exec sql fetch cursor02 for 10 rows // Fetch into Array | |
into :DsData; | |
if sqlcode >= *zero; | |
exec sql GET DIAGNOSTICS :LocCounter = ROW_COUNT; | |
If LocCounter = 5; // 5 Felder | |
clear Kunde; | |
Kunde.Id = %dec(DsData(1).Text:10:0); | |
Kunde.Name = DsData(2).Text; | |
Kunde.PLZ = DsData(3).Text; | |
Kunde.Ort = DsData(4).Text; | |
Kunde.Strasse = DsData(5).Text; | |
kundeSchreiben(Kunde); | |
endif; | |
endif; | |
exec sql close cursor02; | |
end-proc; | |
//------------------------------------------------------------------// | |
// Datensatz in Kundendatei schreiben // | |
//------------------------------------------------------------------// | |
dcl-proc kundeSchreiben; | |
dcl-pi *n; | |
PiKunde likeds(Kunde) const; | |
end-pi; | |
exec sql | |
insert into Kunden00 values(:PiKunde) | |
with nc; | |
end-proc; | |
//------------------------------------------------------------------// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment