cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic select statement

Former Member
0 Kudos

Hi All,

Is there a way that I send the table name (Ex: ZPR_PHASE, ZPR_PROGRAM...) into a method or function and I get all the entries of the table.

A general method to get all the values.

Thanks and regards,

Bindiya

Accepted Solutions (1)

Accepted Solutions (1)

guillaume-hrc
Active Contributor
0 Kudos

Hi,

You can write things like :


FIELD-SYMBOLS : <ft_tab>   TYPE ANY TABLE,
                <fs_entry> TYPE ANY.

* Stores the name of the table into a variable
w_tabname = 'E070'.  " replace this by any input parameter

* Creates the object dynamically in the memory
CREATE DATA wt_tab TYPE TABLE OF (w_tabname).
ASSIGN wt_tab->* TO <ft_tab>.

* Fills the object with data
SELECT * FROM (w_tabname)
         INTO CORRESPONDING FIELDS OF TABLE <ft_tab>.

* Displays the data
LOOP AT <ft_tab> assigning <fs_entry>.
  WRITE : / <fs_entry>.
ENDLOOP.

...

Please, consider the overhead of manipulating data like this in a generic way.

This can become quickly hard to manage, but it sure is very powerful !

Best regards,

Guillaume

Former Member
0 Kudos

Hi Guillaume,

Thanks for the tip and I was able to try the following:

*******************************************

DATA lv_fieldname TYPE zpr_table_field_name.

DATA lv_tabname TYPE zpr_table_name.

lv_tabname = 'ZPR_PHASE'.

lv_fieldname = 'PHASE_ID'.

DATA lt_prgs TYPE zpr_tt_program.

SELECT (lv_fieldname) FROM (lv_tabname) INTO CORRESPONDING FIELDS OF TABLE lt_prgs.

*******************************************

Now, how can I make the internal table lt_prgs also dynamic?

Thanks and regards,

Bindiya

Former Member
0 Kudos

Hi,

You can use the following:

data: t_result type ref to data,
      cl_table type ref to cl_abap_tabledescr.
field-symbols: <ft_result> type standard table.

cl_table ?= cl_abap_tabledescr=>describe_by_name( lv_tabname ).
create data t_result type handle cl_table.
assign t_result->* to <ft_result>.

SELECT (lv_fieldname) FROM (lv_tabname) INTO CORRESPONDING FIELDS OF TABLE <ft_result>.

Regards,

Tanguy

Answers (0)