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

Problem in Where Condition while Looping

Former Member
0 Likes
777

Hey! experts,

I have a question to all of you out there? Is it possible to give dynamic where condition while looping an internal table. As for example i have got some code here i have check in debugger all the internal table contain values. Just go through the code and please help me out.

TYPES : BEGIN OF struct,

pernr TYPE persno,

begda TYPE begda,

END OF struct.

DATA it_struct TYPE TABLE OF struct.

DATA wa_struct TYPE struct.

TYPES : BEGIN OF temp,

pernr TYPE persno,

begda TYPE begda,

END OF temp.

DATA wa_temp TYPE temp.

DATA it_temp TYPE TABLE OF temp.

DATA it_hrp1001 TYPE TABLE OF hrp1001.

DATA wa_hrp1001 TYPE hrp1001.

DATA it_hrp1001_chk TYPE TABLE OF hrp1001.

DATA wa_hrp1001_chk TYPE hrp1001.

DATA lv_hrobjid TYPE hrobjid.

SELECT a~pernr o~begda INTO CORRESPONDING FIELDS OF TABLE it_struct FROM pa0000 AS a INNER JOIN pa0001 AS o

ON a~pernr = o~pernr

WHERE o~endda = '99991231' AND a~stat2 = '3' AND o~pernr = '128'. " and O~BUKRS = 'ETPL'.

SORT it_struct ASCENDING BY pernr.

DELETE ADJACENT DUPLICATES FROM it_struct.

SELECT * FROM hrp1001 INTO TABLE it_hrp1001 WHERE plvar = '01' AND

otype = 'P' AND

rsign = 'A' AND

relat = '046' AND

sclas = 'BA' AND

histo = 'X'.

SORT it_hrp1001 ASCENDING BY objid.

SELECT * FROM hrp1001 INTO TABLE it_hrp1001_chk WHERE plvar = '01' AND

otype = 'BA' AND

rsign = 'A' AND

relat = '003' AND

sclas = 'BS' AND

sobid = '50001710' AND

histo = 'X'.

LOOP AT it_struct INTO wa_struct.

  • WRITE : / wa_struct-pernr, wa_struct-begda.

lv_hrobjid = wa_struct-pernr.

****************************************The problem is here...... it_hrp1001 is not fetching value in wa_hrp1001 so that sy-subrc is 4. Thats correct but it should print something but nothing is getting printed.

LOOP AT it_hrp1001 into wa_hrp1001 WHERE objid EQ lv_hrobjid.

****************************************************************

IF sy-subrc EQ 0.

READ TABLE it_hrp1001_chk INTO wa_hrp1001_chk WITH KEY

objid = wa_hrp1001-sobid.

IF sy-subrc EQ 0.

WRITE : / 'Some Logic'.

ELSE.

WRITE : / 'Some Logic'.

ENDIF.

ELSE.

WRITE 😕 'Some Logic'.

ENDIF.

ENDLOOP.

ENDLOOP.

Regards,

Mistry Wasim

5 REPLIES 5
Read only

Former Member
0 Likes
726

hi

goo

go through this example and try accordingly.

constants: c_var(15) type c value ' IN S_RANGE'.

parameters:

p_table type dd02l-tabname, " Parameter to capture table name.

p_field(10) type c. " Parameter to capture field name.

data: v_where type string.

concatenate p_field c_var into v_where.

try.

select * into corresponding fields of table <dyn_table>

from (p_table) where (v_where) .

if sy-dbcnt = 0.

write : text-t02.

endif.

  • Exception Catching.

catch cx_root into o_field.

text = o_field->get_text( ).

reward point if helpful.

thanks

mrutyun^

Read only

Former Member
0 Likes
726

Hi,

Yes u can build the where clause condition dynamically as like below

*declare the variable as like below

constants: otype(5) type c value 'OTYPE',

rsign(5) type c value 'RSIGN',

relat(5) type c value ''RELAT',

SCLAS(5) type c value 'SCLAS',

histo(5) type c value 'HISTO'.

DATA: cond type string,

sel type i.

*Build the where clause condition as like below

concatente otype

space

'='

space

l_data1

'AND'

rsign

space

'='

space

l_data2

'AND'

relat

space

'='

space

l_data2

INTO COND.

sel = 1.

  • Call the perform

perform selectquery using cond sel.

form selectquery using cond type string

sel type i.

if sel eq 1.

SELECT * FROM hrp1001 INTO TABLE it_hrp1001 WHERE cond.

elseif sel eq 2.

SELECT * FROM hrp1001_chk INTO TABLE it_hrp1001 WHERE cond.

endif

endform.

Read only

0 Likes
726

Hey! guys,

Listen i dont have any problem with SELECT queries in my code. I just have problem with the Loop Statement which i have pointed out and that too when i am passing a where condition to it thats all.

Actually, the thing is that for the inner loop if the condition is not satisfied that means SY-SUBRC is 4 it is completely coming out of both the loops.

Please help me out.

Mistry Wasim

Read only

0 Likes
726

Hi,

I am not sure if we should use the check on sy-subrc = 4 check inside the loop...

imagine we need to check for a field called 'Field'.............---->>>

the purpose of ....loop at internal table where Field = 'X'

is that only records with 'X' should come in the loop..what i believe is that since there are no records it just exits to the endloop..it will not bother to go inside

if you really need to check the case where field <> 'X'

it is better to write

loop at internal table

if field = 'X'.

write : / 'Logic'.

else.

write : / 'Something'.

endif.

endloop.

the best option is to put a break point and see if the code in the loop is being accessed even when the condition is not satisfied..in this way we can have a clear picture

Pls check,revert and reward if helpful

Regards

Byju

Read only

0 Likes
726

Hey! Edamana,

Thanx for your reply.....i got the hint from your reply.....

Now i am check the sy-subrc value outside the loop condition and its working i suppose.

Mistry Wasim