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 table name, how to query?

Former Member
0 Likes
1,179

Hi!

There is a table name that is decided dynamically. the name is:

someNameYYY where YYY denotes the client number.

I get the client number by sy-mandt and concatenate it with someNameYYY to lc_table_name but then comes the problem:

I cannot do

SELECT *

FROM lc_table_name

because I get compiler error "lc_table_name" is not defined in ABAP dictionary.

How do I query a table with the name decided dynamically?

regards

Baran

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,029

Hi,

lv_table = 'MARA'.

select * from ( lv_table ).

Regards

amole

6 REPLIES 6
Read only

Former Member
0 Likes
1,029

use..

read table.

Read only

Former Member
0 Likes
1,030

Hi,

lv_table = 'MARA'.

select * from ( lv_table ).

Regards

amole

Read only

0 Likes
1,029

thanks... that solved the problem...

regards

Baran

Read only

Former Member
0 Likes
1,029

Hi,

u need to conactenate the sy-mandt and other string..but provided that should be a ur table name..

and the write select as follows.

tablename = > conactenate ur strings

select field1 field2

from ( tablename)===>which can be vary

into i_tab

where condition..

Read only

Former Member
0 Likes
1,029

Hi,

REPORT demo_select_dynamic_database .

DATA wa TYPE scarr.

DATA name(10) TYPE c.

Move your table name to <Name> i mean

MOVE lc_table_name to Name

<b>SELECT *

INTO wa

FROM (name) CLIENT SPECIFIED

WHERE mandt = '000'.

WRITE: / wa-carrid, wa-carrname.

ENDSELECT.</b>

Regards

Sudheer

Read only

Former Member
0 Likes
1,029

You can have do something like this :

REPORT ZTABLE_DOWNLOAD .

tables :

dd02l, "SAP Tables

dd03l, "Table fields

dd04t. "R/3 DD: Data element texts

constants : c_activation_status(1) value 'A',

c_tabclass(6) type c value 'INTTAB',

c_language(2) type c value 'EN'.

type-pools : slis.

selection-screen begin of block b1 with frame title text-003.

*parameters :p_mandt like t001-mandt obligatory default '560'.

parameters :p_table like dd03l-tabname obligatory.

selection-screen end of block b1.

data:

table_desc(70) type c,

table_field like dd03l-fieldname,

total_rows type i,

t_rows(20) type c.

field-symbols:

<fs_line> type any,

<fs_field> type any.

at selection-screen.

*Check for the existence of the table

select single * from dd02l where tabname = p_table

and as4local = c_activation_status.

if sy-subrc ne 0.

*Table is not active in dictionary

message e999(zs) with 'Table is not active in dictionary'.

elseif dd02l-tabclass = c_tabclass.

*It is a structure not a table

message e999(zs) with 'This is a structure not a table'.

endif.

start-of-selection.

perform table_data_display.

end-of-selection.

form table_data_display.

data:

l_long_type type i,

lx_struct type ref to data,

lt_table type ref to data,

lcl_sdescr type ref to cl_abap_structdescr,

lx_lvc_cat type lvc_s_fcat,

lt_lvc_cat type lvc_t_fcat, "Field catalog

lx_fieldcat type slis_fieldcat_alv,

lt_fieldcat type slis_t_fieldcat_alv,

lx_layout type slis_layout_alv,

lt_sort type slis_t_sortinfo_alv, "Sort table

ls_sort type slis_sortinfo_alv.

field-symbols :

<fieldcat> type slis_fieldcat_alv,

<lt_table> type table,

<fs> type any,

<components> type abap_compdescr.

*Dynamic creation of a structure

create data lx_struct type (p_table).

assign lx_struct->* to <fs>.

*Get the field structure

lcl_sdescr ?= cl_abap_typedescr=>describe_by_data( <fs> ).

loop at lcl_sdescr->components assigning <components>.

*Do not display field "MANDT"

IF sy-tabix = 1 AND <components>-name = 'MANDT'.

CONTINUE. "next loop

ENDIF.

*Build fieldcatalog

lx_lvc_cat-fieldname = <components>-name.

lx_lvc_cat-ref_table = p_table.

append lx_lvc_cat to lt_lvc_cat.

lx_fieldcat-fieldname = <components>-name.

lx_fieldcat-ref_tabname = p_table.

append lx_fieldcat to lt_fieldcat.

endloop.

*Create an internal table

call method cl_alv_table_create=>create_dynamic_table

exporting it_fieldcatalog = lt_lvc_cat

importing ep_table = lt_table.

assign lt_table->* to <lt_table>.

*Read the data

select * from (p_table)

into corresponding fields of table <lt_table>

order by primary key.

  • loop at <lt_table> assigning <fs_line>.

  • assign component 'MANDT' of

  • structure <fs_line> to <fs_field>.

  • <fs_field> = p_mandt.

  • endloop.

if <lt_table>[] is initial.

*No table enties are existing

message e003(zdynamictable) with p_table.

exit.

else.

describe table <lt_table>[] lines total_rows.

t_rows = total_rows.

shift t_rows left deleting leading space.

endif.

*Specify the layout

lx_layout-zebra = 'X'.

lx_layout-colwidth_optimize = 'X'.

*Display the ALV List

select single ddtext into table_desc from dd02t

where tabname eq p_table and ddlanguage eq c_language.

check not table_desc is initial.

concatenate 'Entries from table:'

p_table '(' table_desc ')' '-:' t_rows 'Entries Found' into

table_desc

separated by space.

*Start - Download the data to excel sheet by validating the file path.

data:

*lv_filename type string,

lv_fname_validate like rlgrap-filename.

*CONCATENATE 'C:\ZTABLE_DWN\' P_TABLE '.Xls' INTO LV_FILENAME.

*CONCATENATE '
Pc-p31061\Harman\Tasks\ZTABLE_DWN\' P_TABLE '.xls' INTO

CONCATENATE 'C:\ZTABLE_DWN\' P_TABLE '.xls' INTO

LV_FNAME_VALIDATE.

call function 'WS_FILE_DELETE'

exporting

file = lv_fname_validate

  • IMPORTING

  • RETURN =

.

CALL FUNCTION 'WS_DOWNLOAD'

EXPORTING

  • BIN_FILESIZE = ' '

  • CODEPAGE = ' '

FILENAME = LV_FNAME_VALIDATE

FILETYPE = 'DAT'

  • IMPORTING

  • FILELENGTH =

TABLES

DATA_TAB = <lt_table>.

*call function 'GUI_DOWNLOAD'

  • exporting

    • BIN_FILESIZE =

  • filename = lv_fname_validate

  • filetype = 'ASC'

  • write_field_separator = 'X'

    • IMPORTING

    • FILELENGTH =

  • tables

  • data_tab = <lt_table>.

  • End of Download.

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

i_background_id = 'ALV_BACKGROUND'

i_grid_title = table_desc

is_layout = lx_layout

it_fieldcat = lt_fieldcat

tables

t_outtab = <lt_table>

exceptions

program_error = 1

others = 2

.

if sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

endif.

clear table_desc.

endform. " table_data_display