Skip to Content

Read tables dynamically

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Apr 14, 2009 at 10:41 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 14, 2009 at 10:24 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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