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: 

Read unknown (at design-time) table contents

Former Member
0 Kudos

Hello experts,

I would like to write a program which performs the following:

1. Get user input specifying the name of a table (not a problem on how to get that; my doubts starts at item 2. ...)

2. Once I have got the (random) name of the user-selected table to be able to read the given table's contents

3. And even more... I want to go through the table records and write the corresponding values on screen or even play/operate with them from the code

I suppose this could be achieved by using FIELD-SYMBOLS and/or Dynamic Internal Tables; however I've tried with some coding without any success, therefore I'm not sure how to start. I've seen links like http://scn.sap.com/docs/DOC-42525 but not sure I understand how to implement all of this.

Do you have any clues, samples, etc.? Any help will be much appreciated.

Thanks in advance. Best regards,

Bernardo

8 REPLIES 8

nikolayevstigneev
Contributor
0 Kudos

Hi, Bernardo!

It's not that hard to do that.

1. Dynamic SELECTs are described in help docs and there is a huge number of posts here on scn.

2. Reading table contents is actually done in the 1st point. Describing internal table structure is covered in the post that you've mentioned. You can also refer to the table DD03L for the table fields info (just for you to know that such info exists in one place, it's much better to use RTTS).

3. If you want to have a look how such programs are built, please check programs /1BCDWB/DB<TABLE NAME>. They are dynamically generated when you use SE16 to view table records.

Mohamed_Mukhtar
Active Contributor
0 Kudos

Hi Bernardo,

Please find  below sample program :-


REPORT  ZTEST_SCN no standard page heading.

parameters : p_tname type TABNAME.

data it_dyntab type ref to data.

field-symbols <fs_any> type any table.

start-of-selection.

    create data it_dyntab type standard table of (p_tname).

    assign it_dyntab->* to <fs_any>.

    select * from (p_tname) into table <fs_any> up to 100 rows.

    break-point.

    IF SY-SUBRC = 0.

      CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
        EXPORTING
          FILENAME = 'C:\test_scn.xls'
        CHANGING
          DATA_TAB = <fs_any>.
    ENDIF.

Thanks

0 Kudos

Thanks to you Always Learner,

I have tried the code you provided and it "almost" satisfy my needs, I would like to be able to go through each record without having to export them to an xls... additionally I have tried something like this:

REPORT  ZTEST_SCN no standard page heading.

parameters : p_tname type TABNAME.

data it_dyntab type ref to data.

field-symbols <fs_any> type any table.

field-symbols <fs> type any.     "Added by me

start-of-selection.

    create data it_dyntab type standard table of (p_tname).

    assign it_dyntab->* to <fs_any>.

    select * from (p_tname) into table <fs_any> up to 100 rows.

    break-point.

*Following lines commented by me

*    IF SY-SUBRC = 0.

*      CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
*        EXPORTING
*          FILENAME = 'C:\test_scn.xls'
*        CHANGING
*          DATA_TAB = <fs_any>.
*    ENDIF.

loop at <fs_any> assigning <fs>.  "This works well, but... what if I... (?)


     write: / <fs>-  "... Would like to do something like this, but, how to reference the (e.g.: 1st) field?


endloop.

Cheers,

Bernardo

0 Kudos

Hi,

You can do it by accessing each field in the structure based on their index.

(the first field has index 1 and so on).

Example:

data: lv_field_counter type i.
field-symbols: <f1> type any.

* process each field in turn
  do.


* get the next field value in new_line
    add 1 to lv_field_counter.
    assign component lv_field_counter
         of structure iv_new_line to <f1>.

* end of the line. Everybody out.

    if not ( <f1> is assigned )

      exit.

    endif.

..

endo.

cheers

Paul

0 Kudos

Thanks Paul,

After a few tweaks and basically taking into account the unassign keyword I was able to go through all the fields record by record and display them conveniently. This is the coding as it worked:

#####

REPORT  ZTEST_SCN no standard page heading.

  parameters : p_tname type TABNAME.

  data it_dyntab type ref to data.

  field-symbols <fs_any> type any table.

  field-symbols <fs> type any.     "Added by me

  field-symbols <fsa> type any.   "Added by me

  data lv_fld_cnt type i.               "Added by me

  create data it_dyntab type standard table of (p_tname).

  assign it_dyntab->* to <fs_any>.

  select * from (p_tname) into table <fs_any> up to 100 rows.

  break-point.

  loop at <fs_any> assigning <fs>.

*     Lines added by me as per Paul comments --->

      lv_fld_cnt = 0.

      write: /.

      do.

          add 1 to lv_fld_cnt.

          assign component lv_fld_cnt of structure <fs> to <fsa>.

          if not <fsa> is assigned.

              exit.                      "Next record if exists

          else.

              write: <fsa>.

              unassign <fsa>.    "Important to unassign for next step

          endif.

      enddo.

*     Lines added by me as per Paul comments <---

endloop.

#####

Thank you all for your quick help and assistance.

Kind regards,

Bernardo

0 Kudos

Hi,

  FYI, There's no need to unassign. The next loop iteration changes the assignment of the field symbol.

cheers

Paul

0 Kudos

Hey Paul,

When I tested that way it didn't work, and that's the way I addressed it; it seems that the value for the last field in the record remains unchanged and the DO..ENDDO cycle gets repeated even though I execute the:

assign component lv_fld_cnt of structure <fs> to <fsa>


sentence with lv_fld_cnt = <total nmbr of fields> + 1..n


therefore the DO..ENDDO -in consequence the program- never ends. The only way it worked is by unassigning and reasigning for each existent field.


Cheers,


Bernardo

0 Kudos

Oops! My mistake.

cheers

Paul