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 Where Clause in Loop/Endloop

Former Member
0 Likes
1,816

Hi all,

We need to pick up a row from an internal Table but the Where Condition is dynamic.

I tried to use the

LOOP AT LT_T_CACHE INTO LS_CACHE WHERE ( LS_CLAUSE ) .

But is not generating the code. How can we use a dynamic where clause in a Loop/Endloop.

How can we check the

Thanks and Regards,

Arunava

6 REPLIES 6
Read only

Former Member
0 Likes
851

Hallo Arunava,

this type of selection is not possible. You can use "WHERE (LS_CLAUSE)" with a SELECT but not with LOOP. One way around this would be to use a transient routine i.e. you generate code for a routine with all your selection criterien and then call the routine.

There's a Web-Log that describes this. If you look in the Web-Logs and look for "transient" you'll find it. Also, there was a message on this theme in this forum. I took part in it so you could either search for my name or for the word "transient".

I hope this helps.

All the best,

Gerard.

Read only

VXLozano
Active Contributor
0 Likes
851

Use an IF sentence...

LOOP AT....

IF <--LS_CLAUSE.

...

ENDIF.

ENDLOOP.

Read only

ssimsekler
Product and Topic Expert
Product and Topic Expert
0 Likes
851

Hi

If it is really required, you can generate subroutine pools dynamically at ABAP. Look for the statement <b>"GENERATE SUBROUTINE-POOL FROM..."</b> at SAPHelp.

*--Serdar

Read only

Former Member
0 Likes
851

Hi,

For the Usage of Dynamic Where Clause in a program,Its better to use the Transient programs,A Transient program is a program written inside a program and ur requirement is met in that program,the syntax used for that program is:

Generate subroutine pool <Internal table name>

Program_Name.

“ To Get the dynamically generated

Program Name

With the following additions :

Message “ Contains Error Message

Include “ Name of the include program

Line “ Line Number in Transient program

Word “ Incorrect word that caused the error

Offset “ Position of the incorrect word

Trace-file. “ Trace output

Read only

Former Member
0 Likes
851

Hi All,

Thanks for the suggestions. I have been able to solve the problem.

Arunava

Read only

Former Member
0 Likes
851

A small example program in case it is needed:

TABLES : sflight.

DATA: lt_sflight TYPE TABLE OF sflight,

ls_sflight TYPE sflight,

lv_line TYPE string,

lt_code TYPE STANDARD TABLE OF string,

lv_prg TYPE char8,

lv_msg TYPE string.

PARAMETERS: pa_carid TYPE sflight-carrid.

SELECT * FROM sflight

INTO TABLE lt_sflight.

lv_line = 'REPORT DYN_LOOP.'.

APPEND lv_line TO lt_code.

lv_line = 'FORM DYN_LH.'.

APPEND lv_line TO lt_code.

lv_line = 'data : ls_sflight type sflight.'.

APPEND lv_line TO lt_code.

lv_line = 'data : lt_sflight type table of sflight.'.

APPEND lv_line TO lt_code.

lv_line = 'select * from sflight into table lt_sflight.'.

APPEND lv_line TO lt_code.

lv_line = 'loop at lt_sflight'.

APPEND lv_line TO lt_code.

lv_line = 'into ls_sflight'.

APPEND lv_line TO lt_code.

IF pa_carid EQ 'LH'.

lv_line = `where carrid eq 'LH'.`.

ELSEIF pa_carid EQ 'AA'.

lv_line = `where carrid eq 'AA'.`.

ENDIF.

APPEND lv_line TO lt_code.

lv_line = 'write:/ ls_sflight-carrid,ls_sflight-connid, ls_sflight-fldate.'.

APPEND lv_line TO lt_code.

APPEND lv_line TO lt_code.

lv_line = 'endloop.'.

APPEND lv_line TO lt_code.

lv_line = 'ENDFORM.'.

APPEND lv_line TO lt_code.

GENERATE SUBROUTINE POOL lt_code

NAME lv_prg

MESSAGE lv_msg.

IF sy-subrc EQ 0.

WRITE 😕 'Program', lv_prg, 'is generated'.

PERFORM dyn_lh IN PROGRAM (lv_prg).

ELSE.

WRITE / 'error occured'.

WRITE lv_msg.

ENDIF.

Regards,

Sükrü