Application Development and Automation 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 only

Dynamic selection need from a table

Former Member
0 Likes
1,242

Hi,

I have a requirement to select the data from a table dynamically. Only during run time I will know the structure of that table & table name. In this case can anyone give any tips/sample code how it can be accomplished. Help appreciated.

Thanks,

Abhi

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,178

Hi Abhi,

You can try this program. This program approach is similar somewhat but it works for MARA too.

report zsharad.
type-pools : abap.

data: w_line type ref to data.
data : it_details type abap_compdescr_tab,
       wa_details type abap_compdescr.

data : ref_descr type ref to cl_abap_structdescr.


field-symbols  : <fs> type any,
                 <fval> type any,
                 <fnam> type any.

parameters: p_table(30) type c.

start-of-selection.
  ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
  it_details[] = ref_descr->components[].


  create data w_line type (p_table).
  assign w_line->* to <fs>.

  select *  from mara into <fs>
     where matkl = '01'.

    do.

      assign component sy-index of structure <fs> to <fval>.
      if sy-subrc <> 0.
        exit.
      endif.
      read table it_details into wa_details 
           index sy-index.

      write :/01 wa_details-name,
                 <fval>.
      free <fval>.

    enddo.

  endselect.

11 REPLIES 11
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,178

Sure, check out this sample program. It is a dynamic table read.



report zrich_0002.

type-pools: slis.

field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.

data: alv_fldcat type slis_t_fieldcat_alv,
      it_fldcat type lvc_t_fcat.

type-pools : abap.

data : it_details type abap_compdescr_tab,
       wa_details type abap_compdescr.

data : ref_descr type ref to cl_abap_structdescr.

data: new_table type ref to data,
      new_line  type ref to data,
      wa_it_fldcat type lvc_s_fcat.

selection-screen begin of block b1 with frame title text .
parameters: p_table(30) type c.
selection-screen end of block b1.


* Get the structure of the table.
ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details[] = ref_descr->components[].

loop at it_details into wa_details.
  clear wa_it_fldcat.
  wa_it_fldcat-fieldname = wa_details-name .
  wa_it_fldcat-datatype = wa_details-type_kind.
  wa_it_fldcat-inttype = wa_details-type_kind. 
  wa_it_fldcat-intlen = wa_details-length.
  wa_it_fldcat-decimals = wa_details-decimals.
  append wa_it_fldcat to it_fldcat .
endloop.

* Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
             exporting
                it_fieldcatalog = it_fldcat
             importing
                ep_table        = new_table.

assign new_table->* to <dyn_table>.

* Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.

* Select Data from table.
select * into table <dyn_table>
           from (p_table).

* Write out data from table.
loop at <dyn_table> into <dyn_wa>.
  do.
    assign component  sy-index  of structure <dyn_wa> to <dyn_field>.
    if sy-subrc <> 0.
      exit.
    endif.
    if sy-index = 1.
      write:/ <dyn_field>.
    else.
      write: <dyn_field>.
    endif.
  enddo.
endloop.


Welcome to SDN. Please remember to award points for helpful answers and mark you post as solved when solved completely. Thanks.

Regards,

Rich Heilman

Fixed Code

Message was edited by: Rich Heilman

Read only

0 Likes
1,178

Hi Rich,

I am thankful & really appreciate your help, However I am getting some errors, when I used your code I am getting dump at the select statement with the error 'SAPSQL_SELECT_TAB_TOO_SMALL' i.e With ABAP/4 Open SQL array select, the output table is too small.

I will try to resolve this & keep you posted but in case if your are already aware of this problem, can you please suggest to get rid of it.

Thanks again for your help & time.

regards.Abhi

Read only

0 Likes
1,178

I'm not really sure what the problem is. What is the table that you are entering in the selection screen. I've used T001, it runs fine.

Regards,

Rich Heilman

Read only

0 Likes
1,178

As an example I used MARA table.

Regards..Abhi

Read only

0 Likes
1,178

Use this code instead of previouse:


* Dynamic table and structure declaration
  DATA:
        lv_db_table_name TYPE string,
        dy_table TYPE REF TO data.

  FIELD-SYMBOLS:
                 <lt_data> TYPE STANDARD TABLE.


* Prepare
lv_db_table_name = 'RSZCALC'.
TRANSLATE lv_db_table_name TO UPPER CASE.


*---------------------------------------------------------------------
* Create dynamical table based on existing one
*---------------------------------------------------------------------
  CREATE DATA dy_table TYPE TABLE OF (lv_db_table_name).
  ASSIGN dy_table->* TO <lt_data>.


*---------------------------------------------------------------------
* Select content of a table
*---------------------------------------------------------------------
  REFRESH <lt_data>.
  SELECT * FROM (lv_db_table_name) INTO TABLE <lt_data>.

Regards Pavel.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,178

I am seeing the same. I believe it has to do with the field types in the internal table. They are all type C, which is not how it is in the table itself.

Regards,

Rich Heilman

Read only

0 Likes
1,178

It is definitly because of the types of the fields. Particularly the type P fields. I'm looking at it.

Regards,

Rich Heilman

Read only

0 Likes
1,178

Its weird, its like the client field is shifting everything to the left. I don't understand it.

Anyway, the fix for the types, is as follows.



loop at it_details into wa_details.
  clear wa_it_fldcat.
  wa_it_fldcat-fieldname = wa_details-name .
  wa_it_fldcat-datatype = wa_details-type_kind.
<b>  wa_it_fldcat-inttype = wa_details-type_kind.</b>
  wa_it_fldcat-intlen = wa_details-length.
  wa_it_fldcat-decimals = wa_details-decimals.
  append wa_it_fldcat to it_fldcat .
endloop.


Regards,

Rich Heilman

Read only

0 Likes
1,178

I'm stumped, guess this code only works for most tables. Mara is not one of them. I'm on a 46c box, so I'm limited to the 46c technology. Maybe there is something better in newer releases.

Regards,

Rich Heilman

Read only

0 Likes
1,178

Thanks, Rich/Sharad.

I appreciate your help.

Regards, Abhi

Read only

Former Member
0 Likes
1,179

Hi Abhi,

You can try this program. This program approach is similar somewhat but it works for MARA too.

report zsharad.
type-pools : abap.

data: w_line type ref to data.
data : it_details type abap_compdescr_tab,
       wa_details type abap_compdescr.

data : ref_descr type ref to cl_abap_structdescr.


field-symbols  : <fs> type any,
                 <fval> type any,
                 <fnam> type any.

parameters: p_table(30) type c.

start-of-selection.
  ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
  it_details[] = ref_descr->components[].


  create data w_line type (p_table).
  assign w_line->* to <fs>.

  select *  from mara into <fs>
     where matkl = '01'.

    do.

      assign component sy-index of structure <fs> to <fval>.
      if sy-subrc <> 0.
        exit.
      endif.
      read table it_details into wa_details 
           index sy-index.

      write :/01 wa_details-name,
                 <fval>.
      free <fval>.

    enddo.

  endselect.