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 table with key

Former Member
0 Likes
3,207

dear all,

i have a BW abap question for you.

this code is to select from a table and field /BIC/ZSFPPD96 is the key.


*   cache mapping table once
    IF g_t_sample IS INITIAL.
      SELECT /BIC/ZSFPPD96 /BIC/ZSFPPD95 /BIC/ZSFPPD94
        FROM /BIC/PZSFPPD96
        INTO CORRESPONDING FIELDS OF TABLE g_t_project
        WHERE objvers = 'A'.
    ENDIF.

somewhere down there i have this code and this is where my problem is... the scenario is i would like to use both field ZSFPPD95 and ZSFPPD94 as key fields and how can i get this done? thanks


*   look up Project code based on IO
    READ TABLE g_t_project INTO g_s_project WITH TABLE KEY
      /BIC/ZSFPPD95 = SOURCE_FIELDS-/BIC/ZSFPPD95 OR /BIC/ZSFPPD94 = SOURCE_FIELDS-/BIC/ZSFPPD94.

    IF sy-subrc = 0.
      RESULT = g_s_project-/bic/zattr1.
    ELSE.
      RESULT = ''.
    ENDIF.

9 REPLIES 9
Read only

Former Member
0 Likes
1,003

remove the OR condtion

READ TABLE g_t_project INTO g_s_project WITH TABLE KEY

/BIC/ZSFPPD95 = SOURCE_FIELDS-/BIC/ZSFPPD95 /BIC/ZSFPPD94 = SOURCE_FIELDS-/BIC/ZSFPPD94.

Press F1 or read and check the syntax

Thanks

Bala Duvvuri

Read only

0 Likes
1,003

hello bala,

by removing the OR, wont it give me an AND statement?

Read only

0 Likes
1,003

Yes you are right it gives AND statement,I am not sure how you can make it OR

Thanks

Bala Duvvuri

Read only

Former Member
0 Likes
1,003

Hi ,

In the Read table Statement , we cannot use AND , OR Conditions . Remove the OR and try.

In this case , value combination of Fields /BIC/ZSFPPD95 and /BIC/ZSFPPD94 , READ TABLE STATEMENT RETRIEVE the record.


*   look up Project code based on IO
    READ TABLE g_t_project INTO g_s_project WITH TABLE KEY
      /BIC/ZSFPPD95 = SOURCE_FIELDS-/BIC/ZSFPPD95  /BIC/ZSFPPD94 = SOURCE_FIELDS-/BIC/ZSFPPD94.
 
    IF sy-subrc = 0.
      RESULT = g_s_project-/bic/zattr1.
    ELSE.
      RESULT = ''.
    ENDIF.

Read only

0 Likes
1,003

hello,

how is that going to give me an OR statement?

Read only

0 Likes
1,003

Hi ,

you need to break the READ TABLE statement as below , it will work. as per this , read table statement will try for /BIC/ZSFPPD95 ( Key 1 ) , if It fails , REad table will try to find the record with /BIC/ZSFPPD94 ( key 2 ) , even then it fails My result ( Target value ) will be blank.


*   look up Project code based on IO
    READ TABLE g_t_project INTO g_s_project WITH TABLE KEY
      /BIC/ZSFPPD95 = SOURCE_FIELDS-/BIC/ZSFPPD95.
 
    IF sy-subrc = 0.
      RESULT = g_s_project-/bic/zattr1.
    ELSE.

       READ TABLE g_t_project INTO g_s_project WITH TABLE KEY   /BIC/ZSFPPD94 = SOURCE_FIELDS-/BIC/ZSFPPD94.
 
       IF sy-subrc = 0.
         RESULT = g_s_project-/bic/zattr1.
        ELSE.
         RESULT = ''.
       ENDIF.
ENDIF.

Edited by: Prasath Arivazhagan on Jun 30, 2010 8:00 AM

Read only

Former Member
0 Likes
1,003

HI

first you need to check that your key field should be the primary key. so that you can avoid multiple records because read table will fetch only one record.

and rather using read table statement you can use llop at your internal table with condition

like loop at internal table itab where <field1> = 'some value' and <field2> = 'some values'.

in this case it will check both the conditions and will fethc the values only if both the conditions will meat.

but in this scenario if you have multiple values for the same combination you will have multiple records.

therefor you need to check that both the fields are primary fields of the table so that you can avoid the duplicacy of the record.

I hope this will clear your doubt.

<removed by moderator>

thanks

Lalit Gupta

Edited by: Thomas Zloch on Jun 30, 2010 12:30 PM - do not ask for points

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,003

As you have mentioned in the table /BIC/PZSFPPD96, the key field is /BIC/ZSFPPD96. So how do you expect to get a unique record for the non-key fields ?

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,003

I think the g_t_project is of sorted or hased type, so try this


loop at g_t_project  INTO g_s_project where  /BIC/ZSFPPD95 = SOURCE_FIELDS-/BIC/ZSFPPD95 
                                                                        or /BIC/ZSFPPD94 = SOURCE_FIELDS-/BIC/ZSFPPD94.
RESULT = g_s_project-/bic/zattr1.
exit.
endloop.

if sy-subrc ne 0.
clear result.
endif.

@Suhas - + 1 .... didnt notice that

Edited by: Keshav.T on Jun 30, 2010 4:03 PM