Fully flexible query:
SELECT SINGLE (<fs_user_table>-fieldname) FROM (<fs_user_table>-tabname)
INTO gv_text
WHERE (gt_sql_where).
Note: Even target INTO variable can be created dynamically - see another post.
Source table for Query
Structure of custom table Z_USER_TABS:Possible table content (user related tables):
ABAP coding:
REPORT z_osql_dynamics.
PARAMETER p_user TYPE xubname.
" Get/List user param table list
TYPES: BEGIN OF tys_user_tab,
tabname TYPE tabname,
fieldname TYPE fieldname,
END OF tys_user_tab.
TYPES tyv_user_tab TYPE c LENGTH 72.
DATA: gt_user_tables TYPE STANDARD TABLE OF tys_user_tab,
gt_sql_where TYPE STANDARD TABLE OF tyv_user_tab,
gv_sql_where LIKE LINE OF gt_sql_where,
gv_text TYPE string.
START-OF-SELECTION.
" Get list of all user param tables to be checked
SELECT tabname fieldname
FROM z_user_tabs
INTO TABLE gt_user_tables.
FIELD-SYMBOLS <fs_user_table> LIKE LINE OF gt_user_tables.
LOOP AT gt_user_tables ASSIGNING <fs_user_table>.
CLEAR gv_sql_where.
CLEAR gt_sql_where.
CONCATENATE <fs_user_table>-fieldname ' = ' '`' p_user '`' INTO gv_sql_where RESPECTING BLANKS.
APPEND gv_sql_where TO gt_sql_where.
SELECT SINGLE (<fs_user_table>-fieldname) FROM (<fs_user_table>-tabname)
INTO gv_text
WHERE (gt_sql_where).
IF sy-subrc = 0.
WRITE: / <fs_user_table>-tabname, <fs_user_table>-fieldname, ' = ', gv_text.
ENDIF.
ENDLOOP.
END-OF-SELECTION.