-
1
- #1
Dear Experts,
I Had a problem with inserting a csv file that a got from a unix server in utf-8 into an sql server table with 30 columns all text
I tried is several times with a bulk insert instruction but that did not work
There were twoo problems : text was utf-8 and eof line was only LF
Column delimiter is ;
So i made this simple code for a class
this.tb_tekst.value = "Leads"
this.tb_tijd.value=time()
sql = "select * FROM " + m.y_db1 + "f_par "
retcode = SQLEXEC(handle,sql,"t_par")
IF retcode > 0
SELECT t_par
GO top
m.xxextdir = T_par.extdir
m.xxprogdir = T_par.progdir
SET defau TO &xxextdir
gnextern = ADIR(gaDatabase, '*.csv')
FOR nCount = 1 TO gnextern
m.bestand = "\\server02\data\merlijn\aanvragen\" + gaDatabase(nCount,1)
gnFileHandle = FOPEN(m.bestand)
nSize = FSEEK(gnFileHandle, 0, 2)
IF nSize <= 0
WAIT WINDOW "This file is empty!" NOWAIT
RETURN
ELSE
= FSEEK(gnFileHandle, 0, 0)
m.lf = CHR(10)
m.flag1 = 0
m.tel = 0
m.teller = 0
m.veldnr = 1
cString = FREAD(gnFileHandle, nSize)
SELECT test
APPEND blank
FOR m.i = 1 TO nSize
IF SUBSTR(cString,m.i,1) = m.lf
SELECT test
APPEND BLANK
m.veldnr = 1
endif
IF SUBSTR(cString,m.i,1) <> ";"
naam = "test.veld" +STR(m.veldnr,2,0)
m.zone = SUBSTR(cString,m.i,1)
naam= CHRTRAN(naam,' ','0')
replace &naam WITH &naam+STRCONV(sUBSTR(cString,m.i,1),11)
ELSE
m.veldnr=m.veldnr+1
ENDIF
endfor
ENDIF
= FCLOSE(gnFileHandle)
SELECT test
GO top
SCAN
IF ALLTRIM(UPPER(veld01)) = "TYPE"
LOOP
ENDIF
IF EMPTY(ALLTRIM(UPPER(veld01)))
LOOP
ENDIF
sql = "insert into " + m.y_db1 + "f_extern (veld1,veld2,veld3,veld4,veld5,veld6,veld7,veld8,veld9,veld10,veld11,veld12,veld13,veld14,veld15,veld16,veld17,veld18,veld19,veld20,veld21,veld22,veld23,veld24,veld25,veld26,veld27,veld28,veld29,veld30)"+CHR(13) +;
"values ("+;
"'" + CHRTRAN(test.veld01,"'","´") + "'," +;
"'" + CHRTRAN(test.veld02,"'","´") + "'," +;
"'" + CHRTRAN(test.veld03,"'","´") + "'," +;
"'" + CHRTRAN(test.veld04,"'","´") + "'," +;
"'" + CHRTRAN(test.veld05,"'","´") + "'," +;
"'" + CHRTRAN(test.veld06,"'","´") + "'," +;
"'" + CHRTRAN(test.veld07,"'","´") + "'," +;
"'" + CHRTRAN(test.veld08,"'","´") + "'," +;
"'" + CHRTRAN(test.veld09,"'","´") + "'," +;
"'" + CHRTRAN(test.veld10,"'","´") + "'," +;
"'" + CHRTRAN(test.veld11,"'","´") + "'," +;
"'" + CHRTRAN(test.veld12,"'","´") + "'," +;
"'" + CHRTRAN(test.veld13,"'","´") + "'," +;
"'" + CHRTRAN(test.veld14,"'","´") + "'," +;
"'" + CHRTRAN(test.veld15,"'","´") + "'," +;
"'" + CHRTRAN(test.veld16,"'","´") + "'," +;
"'" + CHRTRAN(test.veld17,"'","´") + "'," +;
"'" + CHRTRAN(test.veld18,"'","´") + "'," +;
"'" + CHRTRAN(test.veld19,"'","´") + "'," +;
"'" + CHRTRAN(test.veld20,"'","´") + "'," +;
"'" + CHRTRAN(test.veld21,"'","´") + "'," +;
"'" + CHRTRAN(test.veld22,"'","´") + "'," +;
"'" + CHRTRAN(test.veld23,"'","´") + "'," +;
"'" + CHRTRAN(test.veld24,"'","´") + "'," +;
"'" + CHRTRAN(test.veld25,"'","´") + "'," +;
"'" + CHRTRAN(test.veld26,"'","´") + "'," +;
"'" + CHRTRAN(test.veld27,"'","´") + "'," +;
"'" + CHRTRAN(test.veld28,"'","´") + "'," +;
"'" + CHRTRAN(test.veld29,"'","´") + "'," +;
"'" + CHRTRAN(test.veld30,"'","´") + "')"
retcode = SQLEXEC(handle,sql,"")
IF retcode > 0
SELECT test
DELETE NEXT 1
ELSE
= AERROR(aErrorArray) && Data from most recent error
wait window str(aErrorArray(1),8,0) + chr(13) +;
aErrorArray(2) + chr(13) +;
'insert f_extern'
return
ENDIF
ENDSCAN
DELETE FILE gaDatabase(nCount,1)
endfor
SET defau TO &xxprogdir
ELSE
= AERROR(aErrorArray) && Data from most recent error
wait window str(aErrorArray(1),8,0) + chr(13) +;
aErrorArray(2) + chr(13) +;
'02 SELECT T_PAR'
return
ENDIF
I Had a problem with inserting a csv file that a got from a unix server in utf-8 into an sql server table with 30 columns all text
I tried is several times with a bulk insert instruction but that did not work
There were twoo problems : text was utf-8 and eof line was only LF
Column delimiter is ;
So i made this simple code for a class
this.tb_tekst.value = "Leads"
this.tb_tijd.value=time()
sql = "select * FROM " + m.y_db1 + "f_par "
retcode = SQLEXEC(handle,sql,"t_par")
IF retcode > 0
SELECT t_par
GO top
m.xxextdir = T_par.extdir
m.xxprogdir = T_par.progdir
SET defau TO &xxextdir
gnextern = ADIR(gaDatabase, '*.csv')
FOR nCount = 1 TO gnextern
m.bestand = "\\server02\data\merlijn\aanvragen\" + gaDatabase(nCount,1)
gnFileHandle = FOPEN(m.bestand)
nSize = FSEEK(gnFileHandle, 0, 2)
IF nSize <= 0
WAIT WINDOW "This file is empty!" NOWAIT
RETURN
ELSE
= FSEEK(gnFileHandle, 0, 0)
m.lf = CHR(10)
m.flag1 = 0
m.tel = 0
m.teller = 0
m.veldnr = 1
cString = FREAD(gnFileHandle, nSize)
SELECT test
APPEND blank
FOR m.i = 1 TO nSize
IF SUBSTR(cString,m.i,1) = m.lf
SELECT test
APPEND BLANK
m.veldnr = 1
endif
IF SUBSTR(cString,m.i,1) <> ";"
naam = "test.veld" +STR(m.veldnr,2,0)
m.zone = SUBSTR(cString,m.i,1)
naam= CHRTRAN(naam,' ','0')
replace &naam WITH &naam+STRCONV(sUBSTR(cString,m.i,1),11)
ELSE
m.veldnr=m.veldnr+1
ENDIF
endfor
ENDIF
= FCLOSE(gnFileHandle)
SELECT test
GO top
SCAN
IF ALLTRIM(UPPER(veld01)) = "TYPE"
LOOP
ENDIF
IF EMPTY(ALLTRIM(UPPER(veld01)))
LOOP
ENDIF
sql = "insert into " + m.y_db1 + "f_extern (veld1,veld2,veld3,veld4,veld5,veld6,veld7,veld8,veld9,veld10,veld11,veld12,veld13,veld14,veld15,veld16,veld17,veld18,veld19,veld20,veld21,veld22,veld23,veld24,veld25,veld26,veld27,veld28,veld29,veld30)"+CHR(13) +;
"values ("+;
"'" + CHRTRAN(test.veld01,"'","´") + "'," +;
"'" + CHRTRAN(test.veld02,"'","´") + "'," +;
"'" + CHRTRAN(test.veld03,"'","´") + "'," +;
"'" + CHRTRAN(test.veld04,"'","´") + "'," +;
"'" + CHRTRAN(test.veld05,"'","´") + "'," +;
"'" + CHRTRAN(test.veld06,"'","´") + "'," +;
"'" + CHRTRAN(test.veld07,"'","´") + "'," +;
"'" + CHRTRAN(test.veld08,"'","´") + "'," +;
"'" + CHRTRAN(test.veld09,"'","´") + "'," +;
"'" + CHRTRAN(test.veld10,"'","´") + "'," +;
"'" + CHRTRAN(test.veld11,"'","´") + "'," +;
"'" + CHRTRAN(test.veld12,"'","´") + "'," +;
"'" + CHRTRAN(test.veld13,"'","´") + "'," +;
"'" + CHRTRAN(test.veld14,"'","´") + "'," +;
"'" + CHRTRAN(test.veld15,"'","´") + "'," +;
"'" + CHRTRAN(test.veld16,"'","´") + "'," +;
"'" + CHRTRAN(test.veld17,"'","´") + "'," +;
"'" + CHRTRAN(test.veld18,"'","´") + "'," +;
"'" + CHRTRAN(test.veld19,"'","´") + "'," +;
"'" + CHRTRAN(test.veld20,"'","´") + "'," +;
"'" + CHRTRAN(test.veld21,"'","´") + "'," +;
"'" + CHRTRAN(test.veld22,"'","´") + "'," +;
"'" + CHRTRAN(test.veld23,"'","´") + "'," +;
"'" + CHRTRAN(test.veld24,"'","´") + "'," +;
"'" + CHRTRAN(test.veld25,"'","´") + "'," +;
"'" + CHRTRAN(test.veld26,"'","´") + "'," +;
"'" + CHRTRAN(test.veld27,"'","´") + "'," +;
"'" + CHRTRAN(test.veld28,"'","´") + "'," +;
"'" + CHRTRAN(test.veld29,"'","´") + "'," +;
"'" + CHRTRAN(test.veld30,"'","´") + "')"
retcode = SQLEXEC(handle,sql,"")
IF retcode > 0
SELECT test
DELETE NEXT 1
ELSE
= AERROR(aErrorArray) && Data from most recent error
wait window str(aErrorArray(1),8,0) + chr(13) +;
aErrorArray(2) + chr(13) +;
'insert f_extern'
return
ENDIF
ENDSCAN
DELETE FILE gaDatabase(nCount,1)
endfor
SET defau TO &xxprogdir
ELSE
= AERROR(aErrorArray) && Data from most recent error
wait window str(aErrorArray(1),8,0) + chr(13) +;
aErrorArray(2) + chr(13) +;
'02 SELECT T_PAR'
return
ENDIF