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 select query

Former Member
0 Likes
938

Hi all,

I need to have a select query in dynamically for eg . i need to select the fields of the table dynamically using the select query.

8 REPLIES 8
Read only

GauthamV
Active Contributor
0 Likes
889

Don't violate forum rules with duplicate posts

Read only

Former Member
0 Likes
889

Your requirement is not clear.

Put in more details for an appropriate suggestion or solution.

Regards

Abhinab Mishra

Read only

0 Likes
889

Hi Abhinab,

I have two parameters in which you give a number and table name as input. Watever this number in the parameter is, i need to fetch those many fields from the relevant table. (eg. if the number entered is 5 and table entered is mara i need to display the the first 5 fields of the mara table)

Read only

0 Likes
889

Hi Abhees,

Hope this code helps.


parameters: p_fld1 type fieldname,
            p_tab1 type tabname.

types: begin of tp_table,
        field type string,
       end of tp_table.

data: t_table type standard table of tp_table.

select (p_fld1)
  from (p_tab1)
  into table t_table
  up to 1 rows.

Regards.

Read only

0 Likes
889

Use this code

TABLES:
  DD02L, DD03L.
PARAMETERS:
  P_I TYPE I,
  P_TABle(30) TYPE C.
type-pools : abap.
  field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.
  data: dy_table type ref to data,
      dy_line  type ref to data,
      xfc type lvc_s_fcat,
      ifc type lvc_t_fcat.
DATA:
  BEGIN OF TABSTRUC OCCURS 0,
  FIELDNAME LIKE DD03L-FIELDNAME,
  POSITION LIKE DD03L-POSITION,
  END OF TABSTRUC.
DATA: FIELDSTR TYPE STRING.
DATA: T_TABLE TYPE STANDARD TABLE OF TP_TABLE.
data : idetails type abap_compdescr_tab,
       xdetails type abap_compdescr.
  data : ref_table_des type ref to cl_abap_structdescr.
* Get the structure of the table.
  ref_table_des ?=
      cl_abap_typedescr=>describe_by_name( p_table ).
  idetails[[] = ref_table_des->components[]].
    loop at idetails into xdetails.
    clear xfc.
    xfc-fieldname = xdetails-name .
    xfc-datatype = xdetails-type_kind.
    xfc-inttype = xdetails-type_kind.
    xfc-intlen = xdetails-length.
    xfc-decimals = xdetails-decimals.
    append xfc to ifc.
  endloop.
SELECT FIELDNAME POSITION INTO TABLE TABSTRUC FROM DD03L
  WHERE TABNAME = P_TABle
    AND ROLLNAME NE SPACE.
SORT TABSTRUC BY POSITION.
P_I = P_I + 1.
DELETE TABSTRUC FROM P_I.
LOOP AT TABSTRUC.
CONCATENATE FIELDSTR TABSTRUC-FIELDNAME INTO FIELDSTR SEPARATED BY
SPACE.
ENDLOOP.
delete ifc from p_i.
* Create dynamic internal table and assign to FS
  call method cl_alv_table_create=>create_dynamic_table
               exporting
                  it_fieldcatalog = ifc
               importing
                  ep_table        = dy_table.
    assign dy_table->* to <dyn_table>.
* Create dynamic work area and assign to FS
  create data dy_line like line of <dyn_table>.
  assign dy_line->* to <dyn_wa>.
* Select Data from table.
  select (FIELDSTR) 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.

i have modified the original Source Code to satisfy your requirement : https://www.sdn.sap.com/irj/scn/wiki?path=/display/abap/dynamicInternaltable

Read only

faisalatsap
Active Contributor
0 Likes
889

Hi, Abeehs

Please Don't Post Duplicate Posts and also Search Before Posting

Check [Dynamic Select Statement|;

Regards,

Faisal

Read only

Former Member
0 Likes
889

Hi Abhinab,

I have two parameters in which you give a number and table name as input. Watever this number in the parameter is, i need to fetch those many fields from the relevant table. (eg. if the number entered is 5 and table entered is mara i need to display the the first 5 fields of the mara table)

Read only

Former Member
0 Likes
889

Try this one

data : sel1 type table of edpline.

if proj-vbukr eq '4200' or

proj-vbukr eq '4100' or

proj-vbukr eq '4400' or

proj-vbukr eq '4300' or

proj-vbukr eq '4800'.

append

'BUKRS HKONT GJAHR BELNR BUDAT BLART SHKZG DMBTR PROJK' to sel1.

else.

append

'BUKRS HKONT GJAHR BELNR BUDAT BLART SHKZG WRBTR PROJK' to sel1.

select (sel1)

from bsis into corresponding fields of table i_final1

for all entries in i_prps

where projk eq i_prps-pspnr and

bukrs in s_bukrs and

gjahr in s_gjahr and

blart in s_blart and

budat in s_budat.