Consultor Eletrônico



Kbase P95634: How to import a comma separated file into a dynamic TEMP-TABLE?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   11/10/2010
Status: Unverified

GOAL:

How to import a comma separated file into a dynamic TEMP-TABLE?

FACT(s) (Environment):

Progress 9.x
OpenEdge 10.x
All Supported Operating Systems

FIX:

The following solution makes the following assumptions:

1. The data in the comma separated file Customer.csv does not have any embedded commas.
2. The file's last line of data actually ends with a carriage return.
3. The Progress Demo Database Sports2000 is connected.
4. The data fields in the Customer.csv file are in the same order as the fields in the Customer Buffer.


DEFINE VARIABLE hDynamicTempTable AS HANDLE NO-UNDO.
DEFINE VARIABLE hRecordBuffer AS HANDLE NO-UNDO.
DEFINE VARIABLE hFieldBuffer AS HANDLE NO-UNDO.
DEFINE VARIABLE iConter AS INTEGER NO-UNDO.
DEFINE VARIABLE cRecord AS CHARACTER NO-UNDO.
DEFINE VARIABLE cDelimiter AS CHARACTER NO-UNDO INITIAL ",".
DEFINE VARIABLE cTrimCharacter AS CHARACTER NO-UNDO INITIAL " ".

ASSIGN
cDelimiter = CHR(44) /* comma */
cTrimCharacter = CHR(32). /* Space */

CREATE TEMP-TABLE hDynamicTempTable.
hDynamicTempTable:CREATE-LIKE("customer").
hDynamicTempTable:TEMP-TABLE-PREPARE("hDynTTName").
hRecordBuffer = hDynamicTempTable:DEFAULT-BUFFER-HANDLE.

INPUT FROM Customer.csv.

OuterRepeat:
REPEAT:
IMPORT UNFORMATTED cRecord.
DO TRANSACTION:
hRecordBuffer:BUFFER-CREATE().
REPEAT iConter = 1 TO NUM-ENTRIES(cRecord, cDelimiter) ON ERROR UNDO, LEAVE OuterRepeat:
ASSIGN
hFieldBuffer = hRecordBuffer:BUFFER-FIELD(iConter)
hFieldBuffer:BUFFER-VALUE = TRIM(ENTRY(iConter, cRecord, cDelimiter), cTrimCharacter).
END.
END.
END.