Application Development and Automation 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: 
Read only

ABAP - Failed to download internal table (Loop error)

0 Likes
899

Hey,

I tried to download the internal table, which also worked, but the whole content of the database was copied and not just the one i specified in my internal table (in my loop).

My Code:

DATA: lt_lekud    TYPE TABLE OF lekud,      ls_lekud    TYPE lekud.

...

LOOP AT lt_lekud INTO ls_lekud.
  WRITE: (20) ls_lekud-name, (20) ls_lekud-number, (20) ls_lekud-plz, (20)  ls_lekud-description.
  NEW-LINE.
ENDLOOP.

IF sy-subrc = 0.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      default_extension = 'csv'
      default_file_name = 'member'
      file_filter       = |Excel-File (*.csv)\|*.csv\|{ cl_gui_frontend_services=>filetype_all }|
    CHANGING
      filename          = lv_filename
      path              = lv_path
      fullpath          = lv_fullpath.

  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      filename              = lv_filename
      filetype              = 'ASC'
      write_field_separator = 'X'
      bin_filesize          = lv_size
    CHANGING
      data_tab              = lt_lekud
    EXCEPTIONS
      file_write_error      = 1
      OTHERS                = 22.

ENDIF.

The download works fine, but not only the name, number, plz, and description are shown, the complete lekud table, although I refer to my internal table.

Thanks in advance 😉

1 ACCEPTED SOLUTION
Read only

michael_piesche
Active Contributor
768

You are under the impression, that the following statements has any impact on the File Download?

LOOP AT lt_lekud INTO ls_lekud.
  WRITE:(20) ls_lekud-name,(20) ls_lekud-number,(20) ls_lekud-plz,(20)  ls_lekud-description.
  NEW-LINE.
ENDLOOP.

The above statements merely loop over an internal table and write the part of the conten on the screen. One line per record. This has nothing to do with what is actually being downloaded later.

This is were your download happens, and it happens for your internal table lt_lekud, with all its records and all its colomns.

CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      filename              = lv_filename
      filetype              = 'ASC'
      write_field_separator = 'X'
      bin_filesize          = lv_size
    CHANGING
      data_tab              = lt_lekud
    EXCEPTIONS
      file_write_error      = 1
      OTHERS                = 22.

If you want to restrict the columns to just name, number, plz and description, the easiest way would be, to create another internal table but with only the above fields and copy the records from lt_lekud to the new table:

DATA: lt_lekud    TYPE TABLE OF lekud,
      ls_lekud    TYPE lekud,
      begin of ls_lekud_short,                      " your new structure with selective columns
       name LIKE ls_lekud-name,
       number LIKE ls_lekud-number,
       plz    LIKE ls_lekud-plz,
       description LIKE ls_lekud-description,
     end of ls_lekud_short,
     lt_lekud_short LIKE TABLE OF ls_lekud_short. " your new table with structure

....

LOOP AT lt_lekud INTO ls_lekud.
  WRITE:(20) ls_lekud-name,(20) ls_lekud-number,(20) ls_lekud-plz,(20)  ls_lekud-description.
  NEW-LINE.

  MOVE-CORRESPONDING ls_lekud TO ls_lekud_short.    " copy row content for selective columns
  INSERT ls_lekud_short INTO TABLE lt_lekud_short.  " insert row content to new table
ENDLOOP.

IF sy-subrc = 0.

 CALLMETHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      default_extension ='csv'
      default_file_name ='member'
      file_filter       =|Excel-File (*.csv)\|*.csv\|{ cl_gui_frontend_services=>filetype_all }|CHANGING
      filename          = lv_filename
      path              = lv_path
      fullpath          = lv_fullpath.

  CALLMETHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      filename              = lv_filename
      filetype              ='ASC'
      write_field_separator ='X'
      bin_filesize          = lv_size
    CHANGING
      data_tab              = lt_lekud_short   " now you pass only the columns you want
    EXCEPTIONS
      file_write_error      = 1
      OTHERS                = 22.

ENDIF.

Hey,

I tried to download the internal table, which also worked, but the whole content of the database was copied and not just the one i specified in my internal table (in my loop).

My Code:

DATA: lt_lekud    TYPE TABLE OF lekud,      ls_lekud    TYPE lekud.

...

LOOP AT lt_lekud INTO ls_lekud.
  WRITE: (20) ls_lekud-name, (20) ls_lekud-number, (20) ls_lekud-plz, (20)  ls_lekud-description.
  NEW-LINE.
ENDLOOP.

IF sy-subrc = 0.

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      default_extension = 'csv'
      default_file_name = 'member'
      file_filter       = |Excel-File (*.csv)\|*.csv\|{ cl_gui_frontend_services=>filetype_all }|
    CHANGING
      filename          = lv_filename
      path              = lv_path
      fullpath          = lv_fullpath.

  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      filename              = lv_filename
      filetype              = 'ASC'
      write_field_separator = 'X'
      bin_filesize          = lv_size
    CHANGING
      data_tab              = lt_lekud
    EXCEPTIONS
      file_write_error      = 1
      OTHERS                = 22.

ENDIF.

The download works fine, but not only the name, number, plz, and description are shown, the complete lekud table, although I refer to my internal table.

Thanks in advance 😉

3 REPLIES 3
Read only

MateuszAdamus
Active Contributor
0 Likes
768

Hello

What is LT_ZEKUD?

Kind regards,
Mateusz
Read only

michael_piesche
Active Contributor
769

You are under the impression, that the following statements has any impact on the File Download?

LOOP AT lt_lekud INTO ls_lekud.
  WRITE:(20) ls_lekud-name,(20) ls_lekud-number,(20) ls_lekud-plz,(20)  ls_lekud-description.
  NEW-LINE.
ENDLOOP.

The above statements merely loop over an internal table and write the part of the conten on the screen. One line per record. This has nothing to do with what is actually being downloaded later.

This is were your download happens, and it happens for your internal table lt_lekud, with all its records and all its colomns.

CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      filename              = lv_filename
      filetype              = 'ASC'
      write_field_separator = 'X'
      bin_filesize          = lv_size
    CHANGING
      data_tab              = lt_lekud
    EXCEPTIONS
      file_write_error      = 1
      OTHERS                = 22.

If you want to restrict the columns to just name, number, plz and description, the easiest way would be, to create another internal table but with only the above fields and copy the records from lt_lekud to the new table:

DATA: lt_lekud    TYPE TABLE OF lekud,
      ls_lekud    TYPE lekud,
      begin of ls_lekud_short,                      " your new structure with selective columns
       name LIKE ls_lekud-name,
       number LIKE ls_lekud-number,
       plz    LIKE ls_lekud-plz,
       description LIKE ls_lekud-description,
     end of ls_lekud_short,
     lt_lekud_short LIKE TABLE OF ls_lekud_short. " your new table with structure

....

LOOP AT lt_lekud INTO ls_lekud.
  WRITE:(20) ls_lekud-name,(20) ls_lekud-number,(20) ls_lekud-plz,(20)  ls_lekud-description.
  NEW-LINE.

  MOVE-CORRESPONDING ls_lekud TO ls_lekud_short.    " copy row content for selective columns
  INSERT ls_lekud_short INTO TABLE lt_lekud_short.  " insert row content to new table
ENDLOOP.

IF sy-subrc = 0.

 CALLMETHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      default_extension ='csv'
      default_file_name ='member'
      file_filter       =|Excel-File (*.csv)\|*.csv\|{ cl_gui_frontend_services=>filetype_all }|CHANGING
      filename          = lv_filename
      path              = lv_path
      fullpath          = lv_fullpath.

  CALLMETHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      filename              = lv_filename
      filetype              ='ASC'
      write_field_separator ='X'
      bin_filesize          = lv_size
    CHANGING
      data_tab              = lt_lekud_short   " now you pass only the columns you want
    EXCEPTIONS
      file_write_error      = 1
      OTHERS                = 22.

ENDIF.
Read only

768

Thank you very much, I really appreciate that 😉