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

Select query to pass Multiple values

sandeep_gvv2
Participant
0 Likes
12,826

Hi,

I have a requirement like i have an internal table like  having only two fields- element,value.

itab1 :

-------

Element                                       value

------------                                      ----------

step                                                2

step1                                              3

step2                                             

4

Next i have a custom table ytab having fields step,step1,step2....

so i need to pass itab1 values to ytab through select query like above

select step step1 step2 from ytab into table itab2 where step = itab1-element

                                                                            and step1 = itab1-element.

is it correct ??? if not how to write select query for the above????

Please help me out.

10 REPLIES 10
Read only

Former Member
0 Likes
5,009

I guess you want to access to the value of your internal table depending on the element name. So your select should work this way (basis NW 7.4 >= SP08):

SELECT step, step1, step2 FROM ytab INTO TABLE @itab2

WHERE step = @itab1[ element = 'step' ]-value AND

             step1 = @itab1[ element = 'step1' ]-value.

But for me your database model seems to be improvable. It's not the best choice to use fields like Step, Step1, Step2 and so on. Please check the principles of relational databases.

Read only

0 Likes
5,009

when i used it i am getting error like below...

   

Read only

0 Likes
5,009

So, please check the ABAP language help for the correct syntax of new OpenSQL.

Some considerations:

  • use commas between fields in the field list
  • use @ vor each specified variable
Read only

0 Likes
5,009

Is there any other alternative to this to write select query.

Read only

0 Likes
5,009

Well, you can do it also old style, but I recommend to learn the new syntax:

READ TABLE itab1 ASSIGNING <ls_step> WITH KEY element = 'step'.

READ TABLE itab1 ASSIGNING <ls_step1> WITH KEY element = 'step1'.

SELECT step step1 step2 FROM ytab INTO TABLE itab2

WHERE step = <ls_step>-value AND

             step1 = <ls_step1>-value.

Read only

0 Likes
5,009

its Fine but in read table i need to read two keys- element,value.

in select query i need to pass the value in where condition.

Read only

Former Member
0 Likes
5,009

Hi Srikanth,

Select query like this

SELECT STEP1 STEP2 FROM YTAB INTO TABLE ITAB2 FOR ALL ENTRIES IN ITAB1                                                                                                     WHERE STEP1 = ITAB1-STEP1

                                                                                        AND  STEP2 = ITAB1-STEP2.

Regards,

E.Ananthachari

Read only

0 Likes
5,009

i think it won't work in where condition you are passing directly step1 from itab,but in itab step1 is not a field element is the field.then how can we use it..

Read only

Former Member
0 Likes
5,009

Yes you can do that by using dynamic where clause in select query.

Find below the sample code for the same:

DATA: lv_fields TYPE string,

     lv_where_clause TYPE string,

     lv_lines TYPE i,

     lv_index  TYPE i.

*

DESCRIBE TABLE itab LINES lv_lines.

LOOP AT itab INTO ls_tab.

   lv_index = sy-tabix.

   CONCATENATE lv_fields ls_tab-element INTO lv_fields SEPARATED BY space.

   IF lv_lines = 1.

     CONCATENATE lv_where ls_tab-element '=' ls_tab-value '.' INTO lv_where SEPARATED BY space.

   ELSE.

     IF lv_lines = lv_index.

       CONCATENATE lv_where ls_tab-element '=' ls_tab-value '.' INTO lv_where SEPARATED BY space.

     ELSE.

       CONCATENATE lv_where ls_tab-element '=' ls_tab-value 'AND' INTO lv_where SEPARATED BY space.

     ENDIF.

   ENDIF.

ENDLOOP.


select (lv_fields) FROM cnvmbtcobj INTO TABLE ytab WHERE (lv_where_clause).


Hope this helps.


Thanks & Regards,

Ajeet.

Read only

Former Member
0 Likes
5,009

hi,

Use of For all enteries in would suffice your requirement but  fetch the key fields of your custom table while using it.

hope it helps