0

I have a Dropdown List in my Program in which I entered the names of different tables. So I worked with the IF-Statement. Basically:

if wa_list-key = '1'.
(replace name of table with the one choosen from the dropdown list)
endif.

I have this kind of selection:

select * from customer into table lt_customer.

Whats the syntax of replacing names of tables? I know replace-statements only work with strings but is there a way around?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
sharpnado
  • 79
  • 1
  • 1
  • 15
  • I dont get it - what are you trying to achieve? Why would you work with an REPLACE statement? Type REPLACE in SE80 and Press F1... – dotchuZ Apr 30 '18 at 12:58
  • what would you prefer? I want my dropdown list to work so I can choose from different tables. – sharpnado Apr 30 '18 at 13:00
  • Useless because of different table types... – dotchuZ Apr 30 '18 at 16:13
  • One example: KNA1 and LFA1 (and related structures) are really similar and might have identical processing for some data pulling. However, the practical utility of this rarely useful because of the amount of extra work involved. It usually easier to make 2 selections or use `if,elseif..` to branch process logic. With that said, some context as to what you are trying to achieve would definitely help us give you an answer. Dynamic selection alone will not help you much, because you also need dynamic data handling. – Zero May 16 '18 at 13:21

2 Answers2

3

You can dynamically select from a table:

DATA: lv_table TYPE tabname.

    SELECT * 
           INTO TABLE lt_table
           FROM (lv_table).

However the lt_table you select into, has to have the same structure like the database table you select from, otherwise it will dump. To overcome this you can use INTO COORESPONDING FIELDS OF lt_table (instead of INTO TABLE...). You can also declare the WHERE conditions dynamically: WHERE (lv_where) It all depends on your exact needs.

József Szikszai
  • 4,791
  • 3
  • 14
  • 24
  • 1
    If one needs a lot more generic access, then check this [link to prevent SQL injection attacks](https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abensql_injections_scrty.htm). – Sandra Rossi May 01 '18 at 07:02
2

In JozsefSzikszai answer you'll get dump when structure and database table will be different. So, you can try this-

DATA: lv_tabname TYPE tabname.

DATA: lo_tabtype     TYPE REF TO cl_abap_tabledescr,
      lo_struct_type TYPE REF TO cl_abap_structdescr,
      lr_data        TYPE REF TO data,
      lt_comp_tab    TYPE cl_abap_structdescr=>component_table,
      ls_comp_fld    TYPE cl_abap_structdescr=>component.

FIELD-SYMBOLS: <fs_tab> TYPE ANY TABLE,
               <fs_struct> TYPE ANY.

lv_tabname = 'ZTEST_DIV'.  " Give tab name from your dropdown select

lo_struct_type ?= cl_abap_typedescr=>describe_by_name( lv_tabname ).
lt_comp_tab  = lo_struct_type->get_components( ).

lo_struct_type = cl_abap_structdescr=>create( lt_comp_tab ).
lo_tabtype     = cl_abap_tabledescr=>create( lo_struct_type ).

CREATE DATA lr_data TYPE HANDLE lo_tabtype.
ASSIGN lr_data->* TO <fs_tab>.

*CREATE DATA lr_data TYPE HANDLE lo_struct_type. " Use this when you want same table structure
*ASSIGN lr_data->* TO <fs_struct>.

* dynamic select
SELECT *
  FROM (lv_tabname)
  INTO CORRESPONDING FIELDS OF TABLE <fs_tab>.

It will be more generic. It will create dynamic internal table using lv_tabname. So, on Select statement you won't get dump.

divScorp
  • 498
  • 1
  • 8
  • 17
  • 1
    or shorter code for creating a reference to an internal table, without using any RTTS class : `CREATE DATA lr_data TYPE TABLE OF (lv_tabname).` (instead of your code TYPE HANDLE lo_tabtype) ... and to a structure, that will be : `CREATE DATA lr_data TYPE (lv_tabname).` (instead of your code TYPE HANDLE lo_struct_type) – Sandra Rossi May 01 '18 at 11:46
  • @SandraRossi Thanks for information. But i prefer using RTTS class because if `lv_tabname` is containing valid table name or not. I think in standard classes will handle that exception. – divScorp May 01 '18 at 12:10
  • If lv_tabname is invalid, CREATE DATA returns CX_SY_CREATE_DATA_ERROR with KERNEL_ERRID attribute equal to the constant CREATE_DATA_UNKNOWN_TYPE. But yes, I understand that there are some reasons to use RTTS instead. – Sandra Rossi May 01 '18 at 12:50