02-22-2012 8:27 PM
Hi experts!
I'm stuck on a big problem here, which I'm hoping someone can help with. I'm a bit of a newbie at ABAP.
The below ABAP attempts to:
1) Extract data from a .CSV file and place it into an internal table.
2) Then the contents of the internal table are copied into a permanent database table. (Don't worry about advance deletions in the permanent table, I've already got that covered.)
The above ABAP works fine if all the fields in the permanent table have the data type CHAR or LANG. However I run into a major problem if a field in the permanent table is INT4 (Integer). Unfortunately I can't change the data types in the permanent table, so I'm stuck with trying to import into an integer field.
The code I have is below.
"----
REPORT Z_MYTESTPROGRAM.
" Types declarations:
"Columns:
types: begin of ttab,
REC(1000) type c,
end of ttab.
"Rows:
types: begin of tdat,
TEST(20) type c,
LAST_NAME(20) type c,
LANG(1) type c,
INDEXCODE(10) TYPE c,
end of tdat.
" Data declarations:
data: itab type table of ttab with header line.
data: idat type table of tdat with header line.
data: file_str type string.
" FILE PATH:
file_str = 'C:\TempCSVFile.csv'.
" Run code to import from Comma-delim file:
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = file_str
TABLES
data_tab = itab
EXCEPTIONS
others = 17.
" Populate temporary (internal) table idat:
loop at itab.
clear idat.
split itab-REC at ',' into
idat-TEST
idat-LAST_NAME
idat-LANG
idat-INDEXCODE.
append idat.
endloop.
" Now populate permanent table with data from temporary table:
insert ZMM_FIRST_TABLE from table idat. " <- This is the row where the Unicode convertible error get's reported.
"----
The error I get when a field is an integer is:
"The type of the database table and work area (or internal table) "IDAT" are not Unicode convertible."
The field that has the problem is the INDEXCODE field and it only occurs when that field becomes an integer-type field in the permanent table. I have also tried changing the data type in the ABAP of to INDEXCODE TYPE I, but then I get an error message
""INDEXCODE" must be a character-type data object (data type C, N, D, T or STRING)."
Basically SAP's not letting me change the data type to an integer in the Types declaration, yet at the same time it's looking for an Integer data type. - Big clash.
Has anyone else come across this? Can anyone think of how I can resolve this problem?
Cheers!
- Andy
02-22-2012 8:41 PM
Simply handle the integer separately. You can use the character structure for reading and parsing the file, but then move the values to a different structure/table that has the same type as the database table.
Rob
02-22-2012 8:41 PM
Simply handle the integer separately. You can use the character structure for reading and parsing the file, but then move the values to a different structure/table that has the same type as the database table.
Rob
02-22-2012 8:46 PM
Hi Rob,
Thanks for that.
Would you be able to provide a sample of the code to do this?
Cheers,
- Andy
02-22-2012 8:49 PM
Please try it yourself and get back to the forum if you have a specific problem.
Rob
02-22-2012 9:06 PM
Ok Rob, I think this is how it's done. I've tagged the following code onto the end of my code. I still however get the same error message (the one about Unicode conversion). Am I transferring the data structure, and changing the field type correctly below?
DATA: BEGIN OF cdat occurs 0,
TESTB(20) TYPE c,
LAST_NAMEB(20) TYPE c,
LANGB(4) TYPE c,
INDEXCODEB TYPE I,
END OF cdat.
MOVE-CORRESPONDING idat TO cdat.
" Now populate permanent table with data from temporary table:
insert ZMM_FIRST_TABLE from table cdat.
Cheers, - Andy
02-22-2012 9:30 PM
And the error I get is:
The type of the database table and work area (or internal table) "CDAT" are not Unicode convertible.
Therefore the transfer is happening from IDAT to CDAT.
Cheers.
02-22-2012 9:32 PM
CDAT has to be defined exactly the same as the database table. Use the structure in the definition, not the individual fields.
Rob
02-22-2012 10:15 PM
Hi Rob!
All I can say is Pure Genius! It all works fine now!
For everyone's information, below is the (working) code that I tagged onto the bottom of my code:
DATA: BEGIN OF cdat occurs 0.
INCLUDE STRUCTURE ZMM_FIRST_TABLE.
DATA END OF cdat.
loop at idat.
MOVE-CORRESPONDING idat TO cdat.
append cdat.
endloop.
delete from ZMM_FIRST_TABLE.
" Now populate permanent table with data from temporary table:
insert ZMM_FIRST_TABLE from table cdat.
Thanks very much for your help Rob!
- Andy