ED Coder · Sep 17, 2020

Bulk load csv into global

Is there a way to bulk load csv into a global? I have a csv with 283 lines in the following pattern

123, first text

234, second text

456, third text

I want to load them into a global ^loader(code, text). Is there a way to upload them using code?

0 4 101 1


quick sketch:

  • open file 
  • start loop
  • read line
  • split pieces P1,P2
  • set ^global(p1,p2)=""
  • done  

Thank You Robert, got it in by the following code

set file=##class(%Stream.FileCharacter).New()

do file.LinkToFile("c:\location of csv")

for quit:file.AtEnd set ^GLOBAL($piece(file.ReadLine(),",",1),$piece(file.ReadLine(),",",*)=""

You'll need to do the ReadLine() just once in each iteration of the loop so that you don't get the P1 from one line pairing with the P2 from the next line, something like this:

set file=##class(%Stream.FileCharacter).%New()

do file.LinkToFile("c:\location of csv")

for  quit:file.AtEnd  set line=file.ReadLine() set ^GLOBAL($piece(line,",",1),$piece(line,",",*))=""