2016 Jul 04 12:20 PM
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.
2016 Jul 04 12:31 PM
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.
2016 Jul 04 1:48 PM
2016 Jul 04 1:53 PM
So, please check the ABAP language help for the correct syntax of new OpenSQL.
Some considerations:
2016 Jul 04 2:04 PM
Is there any other alternative to this to write select query.
2016 Jul 04 3:23 PM
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.
2016 Jul 04 4:22 PM
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.
2016 Jul 04 12:58 PM
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
2016 Jul 04 1:30 PM
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..
2016 Jul 04 12:59 PM
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.
2016 Jul 04 1:23 PM
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