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 at run time?

Former Member
0 Likes
1,504

Hi Experts,

How are you i am facing one problem and i know you will help me actually i want to read internal table records with composite key but the key field will be assign at run time i tried with one basic logic; i'm pasting here .

lv_string = 'GENDERCODE = ''F'' '.

READ TABLE lt_itab WITH KEY lv_string TRANSPORTING NO FIELDS.

IF sy-subrc = 0.

     WRITE: 'Record Found'.

ENDIF.

here my it_tab contains gender field with value 'F' and my lv_string contains all the conditions as string but this is not working and same thing is working with while clause.

I hope you understood my problem please help me.

Thanks and regards.

Rohit kumar.

Hi Experts,

How are you i am facing one problem and i know you will help me actually i want to read internal table records with composite key but the key field will be assign at run time i tried with one basic logic; i'm pasting here .

lv_string = 'GENDERCODE = ''F'' '.

READ TABLE lt_itab WITH KEY lv_string TRANSPORTING NO FIELDS.

IF sy-subrc = 0.

     WRITE: 'Record Found'.

ENDIF.

here my it_tab contains gender field with value 'F' and my lv_string contains all the conditions as string but this is not working and same thing is working with while clause.

I hope you understood my problem please help me.

Thanks and regards.

Rohit kumar.

9 REPLIES 9
Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,105

Did you try a code like

fieldname = 'GENDERCODE'.

searcharea-gendercode = 'F'.

ASSIGN COMPONENT fieldname OF STRUCTURE searcharea TO <key>.

READ TABLE lt_itab WITH KEY (fieldname) = <key> TRANSPORTING NO FIELDS.

IF sy-subrc = 0.

  WRITE: 'Record Found'.

ENDIF.

Regards,

Raymond

Read only

0 Likes
1,105

This code is ok but my problem is some time i need to check two condition and some time one it depends upon the requirement so if i'm using condition as string then i can give all the condition as per the requirement.

Ex if i need only 1 condition then i can do like-

lv_string = 'GENDERCODE = ''F'' '.

READ TABLE lt_itab WITH KEY lv_string TRANSPORTING NO FIELDS.

and if i need two condition then i can do like-


lv_string = ' GENDERCODE = ''F''  GENDERTYPE = ''FEMALE'' '.

READ TABLE lt_itab WITH KEY lv_string TRANSPORTING NO FIELDS.

I hope you understood my problem.

Thanks

Rohit kumar

Read only

0 Likes
1,105

You can add a fixed number of dynamic values in the READ TABLE

READ TABLE lt_itab

  WITH KEY (fieldname1) = <key1>

                    (fieldname2) = <key2>

  TRANSPORTING NO FIELDS

But you should then code separately for each possible number of key fields. So you may rely on a LOOP AT, checking values in the LOOP.

Regards,

Raymond

Read only

0 Likes
1,105

Thanks for your reply but i think you didn't understood my problem properly.

sir here it is fixed like each time we are checking two condition but as i told using the same read statement some time i will read using one key field or some time two key field or more than two also based on the requirement so i want a generic condition that match my requirement. if still you have dot please ask.

Thanks

Rohit kumar

Read only

0 Likes
1,105

This is one way you can do this:

   REPORT  zterscvgha.
DATA: tab(255) OCCURS 0 WITH HEADER LINE,
      lt_itab TYPE  table of mara,
name LIKE sy-repid.

DATA: statement TYPE string,
      condition TYPE string,
      readstat TYPE string,
      statend TYPE string.


statement = 'READ TABLE lt_itab WITH KEY '.
condition = ' MATNR = ''10''  MTART = ''FLE'' '.  " Add your multiple conditions here
statend = ' TRANSPORTING NO FIELDS.'.

.
tab = 'PROGRAM ZTEST35348976.'.
APPEND tab.
tab = 'TYPES: ty_t TYPE STANDARD TABLE OF mara.'.
APPEND tab.

tab = 'FORM check_subrc USING LT_ITAB TYPE ty_t CHANGING  lv_flag TYPE SYSUBRC.'.
APPEND tab.
CONCATENATE statement condition statend INTO readstat.
tab = readstat.
APPEND tab.
tab = 'ENDFORM.'.
APPEND tab.

GENERATE SUBROUTINE POOL tab NAME name.
IF sy-subrc = 0.
  PERFORM check_subrc IN PROGRAM (name) USING lt_itab CHANGING sy-subrc.
  IF sy-subrc = 0.
    WRITE: 'Record Found'.
  ENDIF.
  name = 'ZTEST35348976'.
  DELETE REPORT name.
ENDIF.

Read only

Former Member
0 Likes
1,105

Hi,

if you know the maximum fields you will use, another option is  LOOP AT itab WHERE field IN lt_field_range AND field2 IN lt_field2_range etc. pp. An empty range behaves just the same as not specifying the field in the WHERE clause.

Cheers

   Adi

Read only

0 Likes
1,105

Thanks for your reply but

i want to set field name dynamic in loop at.

Read only

0 Likes
1,105

From Release 7.0, EhP2, you can use dynamic where-conditions for LOOP AT itab. If you are not yet there, it seems that the GENERATE SUBTROUTINE POOL logic mentioned above might be the ultima ratio (although black listed by many IT auditors).

Thomas

Read only

che_eky
Active Contributor
0 Likes
1,105

Rohit,

The dynamic key string you are building is wrong:

lv_string = 'GENDERCODE = ''F'' '.

Though the above does not short-dump, it will not find the table entry you are looking for either. Hence you are not able to find the entry 'F' in your table.

The following working example will give you what you need, I have tried it and it works:

TYPES: BEGIN OF t_tab,
          gendercode TYPE c LENGTH 1,
          matnr TYPE matnr,
        END OF t_tab.

DATA: lv_string1 TYPE char255,
       lv_string2 TYPE char255,
       lv_string3 TYPE char255,
       lv_string4 TYPE char255,
       lv_string5 TYPE char255,
       lv_string6 TYPE char255,
       lv_value1 TYPE char255,
       lv_value2 TYPE char255,
       lv_value3 TYPE char255,
       lv_value4 TYPE char255,
       lv_value5 TYPE char255,
       lv_value6 TYPE char255,
       lt_itab TYPE STANDARD TABLE OF t_tab,
       ls_itab LIKE LINE OF lt_itab.

* Create some dummy data in our internal table
ls_itab-gendercode = 'A'.
APPEND ls_itab TO lt_itab.
ls_itab-gendercode = 'B'.
APPEND ls_itab TO lt_itab.

* This is the table entry we shall try to read...
ls_itab-gendercode = 'F'.
ls_itab-matnr = '1001'.
APPEND ls_itab TO lt_itab.
ls_itab-gendercode = 'C'.
APPEND ls_itab TO lt_itab.

* Build the table key we would like to read
lv_string1 = 'GENDERCODE'.
lv_value1 = 'F'.
lv_string2 = 'MATNR'.
lv_value2 = '1001'.

* Read the table (note key components with no value will be ignored, which is great as it

* allows a subset of key fields to be specified and the rest left blank)
READ TABLE lt_itab INTO ls_itab WITH KEY (lv_string1) = lv_value1
                                          (lv_string2) = lv_value2
                                          (lv_string3) = lv_value3
                                          (lv_string4) = lv_value4
                                          (lv_string5) = lv_value5
                                          (lv_string6) = lv_value6.

IF sy-subrc = 0.
   WRITE: 'Record Found', ls_itab.
ENDIF.

Let me know if it works!

Che