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

SELECT Query

Former Member
0 Likes
541

Hi,

I need to use a variable for the table name in the SELECT query statement.

Say for ex:

data c_tablename type char5 value 'MARA'.

Select * from c_tablename.

Is it possible to do this ? or is there any other way to achieve this?

Because c_tablename can be any table in SAP .It will be difficult to put in IF/Case statment

Thanks

Pls help

4 REPLIES 4
Read only

former_member404244
Active Contributor
0 Likes
518

hi,,

chek the sample code..

Below code might give you some idea:

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.

selection-screen begin of block b1 with frame.

parameters: p_table(30) type c default 'T001'.

selection-screen end of block b1.

start-of-selection.

perform get_structure.

perform create_dynamic_itab.

  • Creates a dyanamic internal table **

perform get_data.

perform write_out.

form get_structure.

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." / 2.

xfc-decimals = xdetails-decimals. " / 2.

append xfc to ifc.

endloop.

endform.

form create_dynamic_itab.

  • 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>.

endform.

form get_data.

  • Select Data from table.

select * into table <dyn_table>

from (p_table).

endform.

form write_out.

  • 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.

endform.

Reward points if u find useful..

Regards,

nagaraj

Read only

Former Member
0 Likes
518

Its acceptable. Jst chk the below cod for ex :

SELECT * FROM (gt_con_dbmap_name)

INTO TABLE lt_sel_ddic_map

WHERE programm = l_program_map.

Read only

varma_narayana
Active Contributor
0 Likes
518

hI Prabha..

Dynamic select query

Eg:

data c_tablename type char5 value 'MARA'.

Select * from (c_tablename). "Table name must be enclosed in brackets

<b>reward if helpful.</b>

Read only

Former Member
0 Likes
518

Hi Prabha.

it is possible to write dynamic select query...

put the table name in brackets.

data c_tablename type char5 value 'MARA'.

Select * from ( c_tablename ).

Reward points for helpful answers.

Regards,

hari krishna