3

I have made a script that does an import of an CSV file.

It work perfect however when I import it into a temp table the last record in my temp table is an empty one. I've tripled checked the CSV and it doesn't contain any empty lines.

This is my code:

INPUT STREAM sEaImport FROM VALUE(cCsvEaFileLocation).
/* Skip first line to remove the headers */
IMPORT STREAM sEaImport DELIMITER ";" vcline.
REPEAT:
    CREATE ttEa.
    IMPORT STREAM sEaImport DELIMITER ";" ttEa.
END.
INPUT CLOSE.

FOR EACH ttEa:
    DISPLAY ttEa.
END. 

When I display the ttEa outside of the REPEAT block I get an empty record like this:

enter image description here

When I display ttEa inside the REPEAT block I don't get an empty record.

Can somebody please help me?

Using this still gives me the same result:

INPUT STREAM sEaImport FROM VALUE(cCsvEaFileLocation).
/* Skip first line to remove the headers */
IMPORT STREAM sEaImport UNFORMATTED vcline.
REPEAT on error undo,leave on endkey undo,leave:
    CREATE ttEa .
    IMPORT STREAM sEaImport DELIMITER ";" ttEa. 
END.

INPUT STREAM sEaImport CLOSE.  
Gaetano Herman
  • 524
  • 6
  • 22
  • I think you need an ON ERROR UNDO, LEAVE phrase in your repeat, since I think you're creating a blank record and the loop is being broken out of by the IMPORT raising an EOF. However, by the time the error occurs you've already created the blank record, and unless you undo that last create, you'll get a blank record. (It might be ENDKEY rather than ERROR - but you get the idea) – Screwtape Nov 07 '17 at 15:14
  • I have this right now and I still get the same result... I have updated the question – Gaetano Herman Nov 07 '17 at 15:28
  • Confusing. You haven't defined the temp table as no-undo have you? – Screwtape Nov 07 '17 at 15:31
  • I have. However after removing NO-UNDO I still get the same result. – Gaetano Herman Nov 07 '17 at 15:33

2 Answers2

2

Make sure your temp table does NOT have the NO-UNDO statement on it. Then change the REPEAT statement to REPEAT TRANSACTION. The last iteration will try to create a ttEa record but fail. Since it's now in a transaction, the create will be undone (which is why you can't have NO-UNDO on the temp table) and the blank record will be gone.

TheDrooper
  • 1,182
  • 1
  • 7
  • 14
1

Go to the end of your CSV file and hit ENTER, save it, run again. It's a known bug for Progress to not import the last line, so make sure it's a blank one. If your file doesn't have a carriage return at the end, you're going to lose the last record, which is probably what's happening here.

Edit: Since you mentioned the problem was the empty record at the very end, rather than not importing the last, I added a transaction to the repeat and in my tests, the null record is gone.

repeat transaction:
    create ttEa.
    import stream sEaImport delimiter ';' ttEa.

end.
bupereira
  • 1,463
  • 8
  • 13
  • I may have asked it wrong but the last line imported isn't one I need... It imports one line to much. – Gaetano Herman Nov 07 '17 at 14:37
  • It imports everything I need but at the end of the csv he imports another line which returns no information whatsoever. – Gaetano Herman Nov 07 '17 at 14:41
  • 1
    That is because you're creating prior to importing. I enclosed the import in a do transaction and I seem to get rid of the faulty record now. I'm editing my answer. – bupereira Nov 07 '17 at 16:48