cancel
Showing results for 
Search instead for 
Did you mean: 

Selecting fields dynamically from SAP table

Former Member
0 Kudos

Hi

I have a table where there are 16 total fields.

TSL01

TSL02.....TSL16

I have a function module which takes a fiscal period that can be ranged from 00 to 16.I need to do select from the table to fetch the total field based on the fiscal period value.Like if fiscal period is 01 i need to select TSL01 if it is 02 then i need to select TSL02 from the table.

As we dont know what is the value of the fiscal period untill runtime i think i need to do a dynamic select to fetch the field dynamically.Do anybody have the Sample code.

Please send it to me as i need it urgently.

Thanks

Debraj

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

use the dynamic use of select statement...

this is from F1...

Alternative 3

... (column_syntax)

Effect

Instead of static data, a data object column_syntax in brackets can be specified, which, when the command is executed, either contains the syntax shown with the static data, or is initial. The data object column_syntax can be a character-type data object or an internal table with a character-type data type. The syntax in column_syntax, like in the ABAP editor, is not case-sensitive. When specifying an internal table, you can distribute the syntax over multiple rows.

If column_syntax is initial when the command is executed, columns is implicitly set to * and all columns are read.

If columns are specificied dynamically without the SINGLE addition, the resulting set is always regarded as having multiple rows.

Notes

Before Release 6.10, you could only specify an internal table with a flat character-type row type for column_syntax with a maximum of 72 characters. Also, before Release 6.10, if you used the DISTINCT addition for dynamic access to pool tables or cluster tables, this was ignored, but since release 6.10, this causes a known exception.

If column_syntax is an internal table with header line, the table body and not the header line is evaluated.

Example

Read out how many flights go to and from a city. The SELECT command is implemented only once in a sub-program. The column data, including aggregate function and the data after GROUP BY, is dynamic. Instead of adding the column data to an internal l_columns table, you could just as easily concatenate it in a character-type l_columns field.

PERFORM my_select USING `CITYFROM`.

ULINE.

PERFORM my_select USING `CITYTO`.

FORM my_select USING l_group TYPE string.

DATA: l_columns TYPE TABLE OF string,

l_container TYPE string,

l_count TYPE i.

APPEND l_group TO l_columns.

APPEND `count( * )` TO l_columns.

SELECT (l_columns)

FROM spfli

INTO (l_container, l_count)

GROUP BY (l_group).

WRITE: / l_count, l_container.

ENDSELECT.

ENDFORM.