Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Reading TXT file to SAP table.

former_member246634
Active Participant
0 Kudos

Hi everyone,

my task is to read data from chosen text file into table I created.

Text file look like this:


ID_ASK|DATA_OD|DATA_DO|

1234|20140517|20140824|

ID_MAG|ID_ASO|ID_KAT_ASO|SOURCE|NAME|

12|23|34|600156|TEST NAME1|

ID_MAG|ID_ASO|ID_KAT_ASO|SOURCE|NAME|

23|24|54|600156|TEST_NUMR2|

Header "ID_ASK|DATA_OD|DATA_DO|" occurs only once, while header "ID_MAG|ID_ASO|ID_KAT_ASO|SOURCE|NAME|" can occur many times.


I want to insert those values (except of headers, of course) into my SAP table:


Table FieldsFields from file
ZIDASKID_ASK
ZIDMAGID_MAG
ZIDASOID_ASO
ZIDKATID_KAT_ASO
ZZRCELSOURCE
ZDAODDATA_OD
ZDADODATA_DO
ZKATNAMNAME

I want user to choose file from browser and I am using F4_FILENAME function.

1. To avoid reading header into table I think I'll use something like this (in loop, of course):

    IF (lv_linecounter MOD 2) = 1.

          CONTINUE.

    ELSE.

          SPLIT something AT '|' INTO those_fields.

2. What should be next function to call?

   

3. Is declaring separate types for those two headers a good idea?


TYPES: BEGIN OF ty_output_hdr,

   id_ask type zask-zidask,

   data_od TYPE zask-zdaod,

   data_do TYPE zask-zdado,

END OF ty_output_hdr.

TYPES: BEGIN OF ty_output_pos,

   id_mag TYPE zask-zidmag,

   id_aso TYPE zask-zidaso,

   id_kat_aso TYPE zask-zidkat,

   source TYPE zask-zzrcel,

   name TYPE zask-zkatnam,

END OF ty_output_pos.

With kind regards,

Bartlomiej

1 ACCEPTED SOLUTION

renatobertizini
Explorer
0 Kudos

Hello Bartlomiej,

His logic for processing (item 1) is correct and you can do the processing in this way without any problems.

Regarding item 2, after the process of SPLIT, you can assign information to a structure and then insert the contents in the table individually.

SPLIT gs_line

    AT '|'

  INTO gv_fieldA

      gv_fieldB

      gv_fieldC.

gs_data-fieldA = gv_fieldA.

gs_data-fieldB = gv_fieldB.

gs_data-FieldC = gv_fieldC.

INSERT FROM ZTABLE gs_data.

Regarding item 3, a separate statement, however it manages more lines, is the best method that can be applied, allowing a subsequent maintenance will not affect other component.

Regarding F4_FILENAME object, you can use it on AT SELECTION-SCREEN event of report:

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  CALL FUNCTION 'F4_FILENAME'

    EXPORTING

      program_name = syst-cprog

      dynpro_number = syst-dynnr

      field_name = '

    IMPORTING

      file_name = p_file.

Tks

Renato Bertizini

5 REPLIES 5

renatobertizini
Explorer
0 Kudos

Hello Bartlomiej,

His logic for processing (item 1) is correct and you can do the processing in this way without any problems.

Regarding item 2, after the process of SPLIT, you can assign information to a structure and then insert the contents in the table individually.

SPLIT gs_line

    AT '|'

  INTO gv_fieldA

      gv_fieldB

      gv_fieldC.

gs_data-fieldA = gv_fieldA.

gs_data-fieldB = gv_fieldB.

gs_data-FieldC = gv_fieldC.

INSERT FROM ZTABLE gs_data.

Regarding item 3, a separate statement, however it manages more lines, is the best method that can be applied, allowing a subsequent maintenance will not affect other component.

Regarding F4_FILENAME object, you can use it on AT SELECTION-SCREEN event of report:

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  CALL FUNCTION 'F4_FILENAME'

    EXPORTING

      program_name = syst-cprog

      dynpro_number = syst-dynnr

      field_name = '

    IMPORTING

      file_name = p_file.

Tks

Renato Bertizini

former_member216168
Active Participant
0 Kudos

Try this one:

http://christopherstoll.org/2011/07/abap-class-to-import-csv-files.html

It worked perfectly for me. In my case, I used to import different kind of CSV files into an internal table...    

fcorodriguezl
Contributor
0 Kudos

Hi, for example, your file csv contains.

BT001 0002 00020010 410010101 410010101

BT001 0002 00020010 410010105 410010105

BT001 0002 00020020 410040101 410040101

BT001 0002 00020020 410110001 410110001

DATA: BEGIN OF it_data OCCURS 0, 

BUKRS  TYPE bukrs, 

KEYR TYPE ZKEYREP, 

KEYC TYPE ZKEYC, 

VALF TYPE SAKNR, 

VALT TYPE SAKNR, 

END OF it_data.

DATA: w_name TYPE string. 

w_name = p_ruta.  

CALL FUNCTION 'GUI_UPLOAD'    

EXPORTING      

filename                = w_name      

filetype                = 'ASC'      

has_field_separator    = 'X'   

TABLES     

data_tab                = it_data 

DESCRIBE TABLE it_data LINES tot_data. 

LOOP AT it_data.   

MOVE: it_data-bukrs TO wa_zfistb002-bukrs,         

it_data-keyrep TO wa_zfistb002-keyrep,         

it_data-keyc TO wa_zfistb002-keyc.         

INSERT INTO zfis VALUES wa_zfistb002. 

ENDLOOP Regards.

Former Member
0 Kudos

Hello Bartlomiej,


Step 1 is fine, it should work well.


2. Create a structure and a table from ty_output_pos and Insert the data to your table.


     DATA: ls_item TYPE ty_output_pos,

                 lt_item TYPE TABLE OF ty_output_pos.


     ls_item-mag = vl_field_1.

     ls_item-aso = vl_field_2.

     ls_item-kat = vl_field_3.

     ls_item-source = vl_field_4.

     ls_item-name = vl_field_5.

    

INSERT ls_item INTO TABLE lt_item.


3. that's correct.

Regards,

Genaro.

former_member246634
Active Participant
0 Kudos

Thank you, guys. Your solutions really helped me, now everything works fine!