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: 

Internal table of type KNA1 to String type Internal table.

Former Member
0 Kudos

Hi Experts,

I have a requirement to convert an internal table of type KNA1 to String type internal table with a seperator.

One option I have is to use Concatenate statement as per code below:

loop at i_kna1 into wa_kna1.

concatenate wa_kna1-field1 wa_kna1-field2 ...

into wa_string seperated by '|'.

append wa_string to I_string.

endloop.

This methos will be very cumbersome as there are many fields involved that will have to be concatenated.

Is tghera ny other shorter method to do it?

Thanks in advance!!

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

I need the string file to be seperated by '|\ delimiter.

Thanks!!

8 REPLIES 8

Former Member
0 Kudos

Hi reetwika,

try this class this will convert abap work area to string.



       CALL METHOD cl_abap_container_utilities=>fill_container_c
          EXPORTING
            im_value               = wa_kna1
          IMPORTING
            ex_container           = l_string
          EXCEPTIONS
            illegal_parameter_type = 1
            OTHERS                 = 2.

Regards,

Prabhudas

Former Member
0 Kudos

Hi,

I need the string file to be seperated by '|\ delimiter.

Thanks!!

0 Kudos

Hi,

Have a look at this FM SAP_CONVERT_TO_TEX_FORMAT.

Here is sample code :

* Convert data in internal table to a delimited text data
  call function 'SAP_CONVERT_TO_TEX_FORMAT'
    exporting
      i_field_seperator    = '|'
    tables
      i_tab_sap_data       = i_ekko
    changing
      i_tab_converted_data = i_text[]
    exceptions
      conversion_failed    = 1
      others               = 2.
  if sy-subrc <> 0.
    write: / 'Program failed to Convert data.'.
  else.

KR

Veeranji Reddy P.

Edited by: Veeranji Reddy on Aug 10, 2010 4:20 PM

0 Kudos

What about something like this?

DATA:
  ls_kna1             TYPE kna1,
  lt_kna1             TYPE STANDARD TABLE OF kna1,
  lv_string_field     TYPE text100,
  lv_string           TYPE string,
  lt_string           TYPE STANDARD TABLE OF string.

FIELD-SYMBOLS:
  <lv_field>      TYPE ANY.

SELECT * FROM kna1
  UP TO 5 ROWS
  INTO TABLE lt_kna1.

LOOP AT lt_kna1 INTO ls_kna1.

  " init
  CLEAR lv_string.

  DO.

    " assign next field of structure, if assign fails the end is reached
    ASSIGN COMPONENT sy-index OF STRUCTURE ls_kna1 TO <lv_field>.
    IF sy-subrc EQ 0.
      " add record to string, if it's the first field don't use separator
      IF lv_string IS INITIAL.
        WRITE <lv_field> TO lv_string_field.
        lv_string = lv_string_field.
      ELSE.
        WRITE <lv_field> TO lv_string_field.
        CONCATENATE lv_string '|' lv_string_field INTO lv_string.
      ENDIF.
    ELSE.
      " add string to our table and leave the DO-ENDDO-loop for this KNA1 record
      APPEND lv_string TO lt_string.
      EXIT.
    ENDIF.

  ENDDO.

ENDLOOP.

BREAK-POINT.

Former Member
0 Kudos

Thanks Veeranji,

The FM works. But the output length is limited to 4096.

Is it any way I can extend it?

Thanks again.

0 Kudos

Hi Reetwika,

If possible try copying the Fm to ZFM(Custom) and change the size of the CHANGING parameter

I_TAB_CONVERTED_DATA to other ASSOCIATED TYPE(>4096 chars)....

KR

Veeranji Reddy P.

Edited by: Veeranji Reddy on Aug 10, 2010 5:39 PM

Former Member
0 Kudos

Hello,

Try like this.

TYPES text TYPE c LENGTH 10.

DATA itab TYPE TABLE OF text.

DATA result TYPE string.

APPEND 'When' TO itab.

APPEND 'the' TO itab.

APPEND 'music' TO itab.

APPEND 'is' TO itab.

APPEND 'over' TO itab.

CONCATENATE LINES OF itab INTO result SEPARATED BY '|'.

Thanks.

Ramya.

Edited by: sri Ramya Goparaju on Aug 10, 2010 2:39 PM

kesavadas_thekkillath
Active Contributor
0 Kudos

Hi,

One more option is create a char01 field between each field which holds '|' , and then populate the values in itab.