Select Dynamic Column
We can select the columns dynamically in a select statement. The syntax is like this:
SELECT (local_internal_table)
FROM database_table INTO TABLE internal_table.
FROM database_table INTO TABLE internal_table.
Here the local internal table contains the field names dynamically. This table also has a line type which holds the data of field names like this.
DATA: line TYPE char100,
itab TYPE TABLE OF line.
line = 'ebeln ebelp matnr werks lgort'.
APPEND line TO itab.
itab TYPE TABLE OF line.
line = 'ebeln ebelp matnr werks lgort'.
APPEND line TO itab.
Now after appending the text to the itab it can be used dynamically in select statement. Here the WHERE clause is optional. If we don’t use it then the total rows/records of the fields will have been fetched by the system.
REPORT zabap_gui.
TABLES: ekpo.
* Creating a custom structure of Item Table
TYPES:
BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
matnr TYPE ekpo-matnr,
werks TYPE ekpo-werks,
lgort TYPE ekpo-lgort,
END OF ty_ekpo.
* Creating a line type of predefined structure
DATA:
wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo,
* Creating a line type and internal table
* to use as dynamic columns specification
line TYPE char100,
itab TYPE TABLE OF line.
line = 'ebeln ebelp matnr werks lgort'.
APPEND line TO itab.
SELECT (itab)
FROM ekpo INTO TABLE it_ekpo
WHERE ebeln = '3000000232'.
WRITE:/ 'PO No.',
15 'Item No',
28 'Material',
48 'Plant',
55 'Storage'.
ULINE.
SKIP.
LOOP AT it_ekpo INTO wa_ekpo.
WRITE:/ wa_ekpo-ebeln,
15 wa_ekpo-ebelp,
28 wa_ekpo-matnr,
48 wa_ekpo-werks,
55 wa_ekpo-lgort.
ENDLOOP.
TABLES: ekpo.
* Creating a custom structure of Item Table
TYPES:
BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
matnr TYPE ekpo-matnr,
werks TYPE ekpo-werks,
lgort TYPE ekpo-lgort,
END OF ty_ekpo.
* Creating a line type of predefined structure
DATA:
wa_ekpo TYPE ty_ekpo,
it_ekpo TYPE STANDARD TABLE OF ty_ekpo,
* Creating a line type and internal table
* to use as dynamic columns specification
line TYPE char100,
itab TYPE TABLE OF line.
line = 'ebeln ebelp matnr werks lgort'.
APPEND line TO itab.
SELECT (itab)
FROM ekpo INTO TABLE it_ekpo
WHERE ebeln = '3000000232'.
WRITE:/ 'PO No.',
15 'Item No',
28 'Material',
48 'Plant',
55 'Storage'.
ULINE.
SKIP.
LOOP AT it_ekpo INTO wa_ekpo.
WRITE:/ wa_ekpo-ebeln,
15 wa_ekpo-ebelp,
28 wa_ekpo-matnr,
48 wa_ekpo-werks,
55 wa_ekpo-lgort.
ENDLOOP.
Here is the output.