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 tables dynamically

david_escofettrenado
Participant
0 Kudos

Hi,

We need to read several tables to determine if they are empty. We have create a program that read all tables from a text file. But <ft_comp> is not assigned with header structure to read table.

DATA: BEGIN OF i_table OCCURS 500,

table(30),

END OF i_table.

DATA: tabname(30).

FIELD-SYMBOLS <fs_comp> type any.

PARAMETERS p_file(128).

START-OF-SELECTION.

  • Obtains file with tables

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

filename = p_file

filetype = 'DAT'

TABLES

data_tab = i_table

EXCEPTIONS

conversion_error = 01

file_open_error = 02.

  • Read all tables

loop at i_table.

tabname = i_table-table.

assign (tabname) to <fs_comp>.

SELECT SINGLE * into <fs_comp>

FROM (tabname).

if sy-subrc = 0.

  • Exist registers.

endif.

endloop.

1 ACCEPTED SOLUTION

andrea_olivieri
Contributor
0 Kudos

Hi, try this (your code adapted):

.DATA: BEGIN OF i_table OCCURS 500,
table(30),
END OF i_table.
DATA: tabname(30).
*FIELD-SYMBOLS <fs_comp> type any.

PARAMETERS p_file(128).

START-OF-SELECTION.

* Obtains file with tables
  CALL FUNCTION 'WS_UPLOAD'
    EXPORTING
      filename         = p_file
      filetype         = 'DAT'
    TABLES
      data_tab         = i_table
    EXCEPTIONS
      conversion_error = 01
      file_open_error  = 02.

* Read all tables
  LOOP AT i_table.

    tabname = i_table-table.

*assign (tabname) to <fs_comp>.
    SELECT COUNT( * ) FROM (tabname).
    IF sy-dbcnt > 0.
      WRITE:/ tabname, sy-dbcnt.
* Exist registers.
    ENDIF.

  ENDLOOP.

.

This should works.

Andrea

4 REPLIES 4

Former Member
0 Kudos

hi there.... you need not have header line.... infact you need not select anything into a header....

simply use :

select single * from <table name > where <conditions>.

if even a single value is returned, then subrc will be 0, which can be checked easily. if the subrc is not zero, no value was returned and you can easily make out which table is empty. Moreover it will improve the performance also as only one record will be fetched instead of the entire table contents.

hope this helps....

cheers,

Prem Sharma

0 Kudos

This is not running because I can assing dinamically value to <table name>. Can you write your abap code?

0 Kudos

Hi,

Try the below code.

DATA dref TYPE REF TO data. 
loop at i_table.

tabname = i_table-table.
    CREATE DATA dref TYPE (tabname). 
    ASSIGN dref->* TO <fs_comp>. 

SELECT SINGLE * into <fs_comp>
FROM (tabname).
if sy-subrc = 0.
* Exist registers.
endif.

endloop.

Regards,

Sesh

andrea_olivieri
Contributor
0 Kudos

Hi, try this (your code adapted):

.DATA: BEGIN OF i_table OCCURS 500,
table(30),
END OF i_table.
DATA: tabname(30).
*FIELD-SYMBOLS <fs_comp> type any.

PARAMETERS p_file(128).

START-OF-SELECTION.

* Obtains file with tables
  CALL FUNCTION 'WS_UPLOAD'
    EXPORTING
      filename         = p_file
      filetype         = 'DAT'
    TABLES
      data_tab         = i_table
    EXCEPTIONS
      conversion_error = 01
      file_open_error  = 02.

* Read all tables
  LOOP AT i_table.

    tabname = i_table-table.

*assign (tabname) to <fs_comp>.
    SELECT COUNT( * ) FROM (tabname).
    IF sy-dbcnt > 0.
      WRITE:/ tabname, sy-dbcnt.
* Exist registers.
    ENDIF.

  ENDLOOP.

.

This should works.

Andrea