0% found this document useful (0 votes)
89 views5 pages

Dynamic Select in Abap1697435803612

The document describes how to write a dynamic select query in ABAP with a dynamic table, fields, and where clause. It shows code to: 1. Accept the table, fields, and where clause as parameters 2. Build a field catalog from the table metadata for dynamic table creation 3. Create the dynamic internal table 4. Execute the dynamic select statement on the table with the dynamic where clause
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views5 pages

Dynamic Select in Abap1697435803612

The document describes how to write a dynamic select query in ABAP with a dynamic table, fields, and where clause. It shows code to: 1. Accept the table, fields, and where clause as parameters 2. Build a field catalog from the table metadata for dynamic table creation 3. Create the dynamic internal table 4. Execute the dynamic select statement on the table with the dynamic where clause
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Dynamic Select Query With Dynamic Where Clause

Write a select query which is dynamic.


● Get the table name in a Parameter.
● Get the fields of the particular table in a parameter with f4 help.
● Get the where clause in parameter.

CODE:

*&---------------------------------------------------------------------*
*& Report ZPJ_WHERE_CLAUSE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zpj_where_clause.

*include zpj_where_clause_junk_codes.
DATA: lv_tab_nm TYPE tabname,
lt_mul TYPE ddshmarks,
lt_dfies TYPE TABLE OF dfies,
lt_return TYPE TABLE OF ddshretval,
gv_field TYPE dd03l-fieldname.

DATA: db_table TYPE ddobjname,


fieldnm TYPE dfies-fieldname.

DATA: BEGIN OF lt_f_info OCCURS 100.


INCLUDE STRUCTURE dfies.
DATA: END OF lt_f_info.

TYPES: BEGIN OF ty_fields,


fieldname TYPE dd03l-fieldname,
END OF ty_fields.

PARAMETERS:
P_db_tab TYPE dd02l-tabname,
p_Fields TYPE String,
P_clause TYPE String DEFAULT 'MATNR = ''000000000000001514'' '.

FIELD-SYMBOLS: <fs_lt_tab> TYPE ANY TABLE,


<fs_str> TYPE any.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fields.


SELECT fieldname FROM dd03l INTO TABLE @DATA(lt_f4) WHERE tabname =
@p_db_tab.
DATA: ls_f4 LIKE LINE OF lt_f4.

CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' " --------------> Dynamic F4 ans


Muliple fields selection in F4
EXPORTING
* DDIC_STRUCTURE = lv_tab_nm
retfield = 'FIELDNAME'
* PVALKEY = ' '
dynpprog = sy-repid
dynpnr = sy-dynnr
dynprofield = 'FIELDNAME'
value_org = 'S'
multiple_choice = 'X'
mark_tab = lt_mul
TABLES
value_tab = lt_f4
field_tab = lt_dfies
return_tab = lt_return
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE 'F4 Help Failed' TYPE 'E'.
ENDIF.

LOOP AT lt_return INTO DATA(ls_return).


IF sy-tabix NE 1 AND ls_return-fieldval IS NOT INITIAL.
CONCATENATE p_fields ls_return-fieldval INTO p_fields SEPARATED BY space.
ELSE.
p_fields = ls_return-fieldval.
ENDIF.
CLEAR ls_return.
ENDLOOP.

START-OF-SELECTION.

SPLIT p_fields AT space INTO TABLE DATA(lt_fields) . "-----------> Getting


the fields in an internal table

DATA: xfc TYPE lvc_s_fcat,


ifc TYPE lvc_t_fcat.
DATA ls_fields LIKE LINE OF lt_fields.
db_table = p_db_tab.
LOOP AT lt_fields INTO ls_fields.

fieldnm = ls_fields.

CALL FUNCTION 'DDIF_FIELDINFO_GET' " --------------> Getting the field's


Info in lt_f_info table.
EXPORTING
tabname = db_table
fieldname = fieldnm
langu = sy-langu
TABLES
dfies_tab = lt_f_info[].
IF sy-subrc <> 0.
MESSAGE 'Field Name Not Found' TYPE 'E'.
ENDIF.

READ TABLE lt_f_info INTO DATA(ls_f_info) WITH KEY fieldname = ls_fields.

xfc-fieldname = ls_fields.
xfc-rollname = ls_f_info-rollname.
xfc-datatype = ls_f_info-datatype.
xfc-inttype = ls_f_info-inttype.
xfc-intlen = ls_f_info-intlen.
xfc-decimals = ls_f_info-decimals.
xfc-scrtext_m = ls_f_info-scrtext_m.
APPEND xfc TO ifc. " -------------------> Appending the values to IFC to
pass in dynamic internal table creation
ENDLOOP.

DATA: lt_table TYPE REF TO data.

CALL METHOD cl_alv_table_create=>create_dynamic_table "------------------>


Creating a Dynamic Internal table with specified fields
EXPORTING
it_fieldcatalog = ifc
IMPORTING
ep_table = lt_table.
ASSIGN lt_table->* TO <fs_lt_tab>.

SELECT (p_fields) " ------------------------------> Dynamic Select Query


INTO CORRESPONDING FIELDS OF TABLE <fs_lt_tab>
FROM (p_db_tab)
WHERE (p_clause).
IF sy-subrc <> 0.
MESSAGE 'select failed' TYPE 'E'.
ENDIF.

cl_demo_output=>display( <fs_lt_tab> ).

OUTPUT:

Click f4 in table fields.

Click execute:

You might also like