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

read statement with key syntax

Former Member
0 Likes
8,685

Hi all,

In read statement, is there a possibility to use 'contains string (CS)' instead of '=' in with key?

I have a requirement to fetch from an internal table, the record whose field1 value contains a particular string.

Thanks,

David.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
5,372

hi,

You can have the String operation in READ statement. Instead you need to use the Loop ..Endloop statement.

LOOP AT itab WHERE field CS l_field.
* Do your Process.
ENDLOOP.

13 REPLIES 13
Read only

Former Member
0 Likes
5,372

Hi David,

The contain string operations cannot be used in the read statement.

instead you use it in the select clause.

for eg.

data: l_srch_str type string.

l_srch_str = 'ABC%'.

select *

from <table>

into table itab

where <fieldname> like l_srch_str.

regards,

Santosh Thorat

Read only

Former Member
0 Likes
5,372

Hi David,

LOOP AT <ITAB> where <Field> CS <VALUE>.

**********Do your work here.

EXIT.

ENDLOOP.

Hope it wors for you.

Read only

0 Likes
5,372

Hi Sanket and Vinod,

Thanks for the reply.

Can you tell me the impact on performance when using loop with where condition instead of a read?

The scenario is:

I have a loop which will have thousands of record.

In this loop, I have to use my requirement of fetching from an internal table.

Thanks,

David.

Read only

0 Likes
5,372

Hi David,

LOOP will definitely have impact on performance. But if there is no other way then we have to compromise.

SORT the table based on the field u want to check will help a lot when using LOOP with Where clause.

Thanks,

Vinod.

Read only

0 Likes
5,372

Hi David..

Try using Wild card character % in READ to check for Contains string..

Ex



read table t_vbak into wa_vbak with key vbeln = '%53%'.

Check whether something like the above is givin any error............ i am trying to read the internal table where VBELN contains the string 53

Cheers..

Read only

vinod_vemuru2
Active Contributor
0 Likes
5,372

HI,

There is no way u can use any operator other than = with read statement.

Instead u can use loop statement.

CLEAR wa.

LOOP AT itab INTO wa WHERE field CS ur value.

EXIT.

ENDLOOP.

now ur wa contains required data.

Thanks,

Vinod.

Read only

Former Member
0 Likes
5,373

hi,

You can have the String operation in READ statement. Instead you need to use the Loop ..Endloop statement.

LOOP AT itab WHERE field CS l_field.
* Do your Process.
ENDLOOP.

Read only

Former Member
0 Likes
5,372

Hi,

In Read Statement you cannot use like statement, you can use only =.

Thanks,

Sriram POnna.

Read only

Former Member
0 Likes
5,372

HI,

First try to read table with some other key field...

And then use if to check ur condition.

loop at itab.

read table itab1 with key f1 = itab-f1.

if sy-subrc = 0.

if itab1-f2 CS 'abc'.

move data.

endif.

endif.

endloop.

Try this.

Regards,

Mr.A

Read only

Former Member
0 Likes
5,372

HI

it won't accept CS in read table clause. syntax error will be displayed.

for more clarity just execute the falloing code.

DATA: BEGIN OF ITAB OCCURS 0,

MATNR LIKE MARA-MATNR,

ERSDA LIKE MARA-ERSDA,

ERNAM LIKE MARA-ERNAM,

END OF ITAB.

SELECT MATNR

ERSDA

ERNAM

FROM MARA

INTO TABLE ITAB

WHERE ERNAM = 'BOHNSTEDT'.

IF SY-SUBRC EQ 0.

SORT ITAB BY MATNR ASCENDING.

READ TABLE ITAB WITH KEY MATNR CS '3'.

IF SY-SUBRC EQ 0.

WRITE: ITAB.

ENDIF.

ENDIF.

Read only

0 Likes
5,372

Hi David,

As per the performence if u having a lot of record u will follow the Phani Krishna suggestion. Loop logic will fine with small no. of recocords.

Read only

Former Member
0 Likes
5,372

you cant use other than = in Read statement.

As you requirement is to find a singe record from a internal table, try these ways

1. Read the table with some other index and try using LOOP at where FROM INDEX

2. Make the table as SORTED with the column as key and then LOOP AT WHERE F1 CS 'XXX'.

for better performance only...

Edited by: Sumanth Nag Kristam on Jan 2, 2009 7:43 AM

Read only

Former Member
0 Likes
5,372

Hi,

fatching recordds with read statement.

READ TABLE <ITAB> INTO <WA> INDEX 1.

READ TABLE <ITAB> INTO <WA> WITH KEY <FIELD NAME> = 'VALUE'.

READ TABLE <ITAB> INTO <WA> INDEX 1 TRANSPORTING NO FIELDS.

Regards

Md.MahaboobKhan