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: 

Problem with Internal Table - Get Field/Column names

Former Member
0 Kudos

Hi all you ABAP experts!

I have an Internal Table with around 45 fields and want to concatenate all of them into a string separated by a character (specified by the user at selection screen). Well I was trying to get all the field names into an internal table then loop at that table with the field names and for each field get its value an concatenate it.

I tried to use the FM 'CACS_GET_TABLE_FIELDS' but unfortunely it only works with transparent tables so it is not working.

Here is the code I am using:


  " Get field names from extract internal table (it_extract)
  CALL FUNCTION 'CACS_GET_TABLE_FIELDS'
  EXPORTING
    i_tabname     = 'it_extract' " it_extract is the name of my internal table
  TABLES
    t_nonkeyfield = it_fields.

  LOOP AT it_extract INTO wa_extract. " For each record in extract table
    LOOP AT it_fields INTO wa_fields. " For each field of extract table
      CONCATENATE 'wa_extract' wa_fields-fieldname
        INTO sfield_name SEPARATED BY '-'.

      CLEAR cfield_value.
      WRITE (sfield_name) TO cfield_value.

      CONCATENATE wa_output_file-sline cfield_value
        INTO wa_output_file-sline
        SEPARATED BY cseparator.
    ENDLOOP.

    APPEND wa_output_file TO it_output_file. " Add to output file
  ENDLOOP.

That is basically the idea. If any of you have a better solution please let me know. I know I could concatenate field by field (for instance: wa_output-field1, wa_output-field2...) but I think there must be an easy way to do it without specifying the name of each field in hard code.

Many thanks in advance.

1 ACCEPTED SOLUTION

Former Member

You can try it as below method as well:


 FIELD-SYMBOLS: <lfs_table>,    " Internal table structure
                 <lfs_con>.      " Field Content
  DATA: l_con(50) TYPE c.        " Field Content in character format
 
* Columns to be tab delimeted
  LOOP AT it_extract ASSIGNING <lfs_table>.
    
    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE <lfs_table>
             TO <lfs_con>.

      CLEAR: l_con.
      MOVE <lfs_con> TO l_con.
      CONDENSE l_con.

      IF sy-index = 1.
         CLEAR: wa_output_file-sline.
         MOVE l_con TO wa_output_file-sline.
      ELSE.
         CONCATENATE wa_output_file-sline l_con INTO wa_output_file-sline
           SEPARATED BY cseparator.
      ENDIF.
      
    ENDDO.
    
    APPEND wa_output_file TO it_output_file. " Add to output file
    
  ENDLOOP.

Regards

Eswar

7 REPLIES 7

kesavadas_thekkillath
Active Contributor
0 Kudos

Do u want to concatenate only the itab field names or any thing other

0 Kudos

Hi Keshu!

I want to get all the fields name dynamically in order to concatenate the value of each field. I would like also to add that performance is critical for this program. Many thanks!

0 Kudos

Have you tried with the code i provided?

~Eswar

0 Kudos

Hi Eswar!

Many thanks for your help. I have tried with the code you suggessted me and it is working fine.

I just wanted to add that I had to limit the DO statement because it was running forever here is how the final code is:


    DESCRIBE FIELD <fs_it_table> TYPE cfstype COMPONENTS nfields. " To get number of fields

    DO nfields TIMES. " DO <number of fields gotten above> TIMES
      ASSIGN COMPONENT SY-INDEX OF STRUCTURE <lfs_table> TO <lfs_cont>.
      IF SY-SUBRC NE 0. " Just in case something goes wrong :P
        EXIT. 
      ENDIF.

Thank you so much for your great help!

0 Kudos

Glad could give you some lead

~Eswar

Former Member

You can try it as below method as well:


 FIELD-SYMBOLS: <lfs_table>,    " Internal table structure
                 <lfs_con>.      " Field Content
  DATA: l_con(50) TYPE c.        " Field Content in character format
 
* Columns to be tab delimeted
  LOOP AT it_extract ASSIGNING <lfs_table>.
    
    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE <lfs_table>
             TO <lfs_con>.

      CLEAR: l_con.
      MOVE <lfs_con> TO l_con.
      CONDENSE l_con.

      IF sy-index = 1.
         CLEAR: wa_output_file-sline.
         MOVE l_con TO wa_output_file-sline.
      ELSE.
         CONCATENATE wa_output_file-sline l_con INTO wa_output_file-sline
           SEPARATED BY cseparator.
      ENDIF.
      
    ENDDO.
    
    APPEND wa_output_file TO it_output_file. " Add to output file
    
  ENDLOOP.

Regards

Eswar

former_member194669
Active Contributor
0 Kudos

use fm


v_repid = sy-repid
CALL FUNCTION 'GET_COMPONENT_LIST'
EXPORTING
program = v_repid
fieldname = 'ITAB'
TABLES
components = components.