04-08-2022 5:15 PM
Hello All
I am new here. I have a question. I am implementing a project using ABAP programming
I would to loop through db table and fill an internal table with several rows from one single row data of my db table (please see the example below).
Column in my db table : user id; text1; text2; text3; text4
Row (values): 123; 90; 40; 60; 50
My internal table should be filled from that db table row as follows:
userid; text
123 ; 90
123; 40
123; 60
123; 50
Could you please give me some suggestions on how to achieve the above?
Thank you.
04-08-2022 5:17 PM
Welcome to the community! While you wait for our amazing members to help answer your question we'd recommend you take a look at some of our fun little tutorials.
Some immediate suggestions to help with our members answering is to perhaps provide some references to material you have already reviewed on the subject. This would help avoid them simply pointing you to material you have already looked at.
Also you might find this brand new resource (hint it has table examples) quite interesting.
04-08-2022 5:40 PM
TYPES: BEGIN OF ts_out,
userid TYPE syuname,
value TYPE i,
END OF ts_out.
DATA: lt_out TYPE TABLE OF ts_out.
SELECT * FROM dbtab INTO TABLE @DATA(lt_dbtab).
LOOP AT lt_dbtab assigning FIELD-SYMBOLS(<ls_dbtab>).
DATA(lv_index) = 1.
DO.
ASSIGN COMPONENT lv_index OF STRUCTURE <ls_dbtab> TO FIELD-SYMBOLS(<lv_val>).
IF sy-subrc NE 0.
EXIT.
ENDIF.
if lv_index = 1.
APPEND INITIAL LINE TO lt_Out ASSIGNING FIELD-SYMBOLS(<ls_out>.
<ls_out>-userid = <lv_val>.
ELSE.
<ls_out>-value = <lv_val>.
ENDIF.
ADD 1 TO lv_index.
ENDDO.
ENDLOOP.
04-08-2022 5:41 PM
Sorry, I coded it now directy here. I hope I did not implement syntax errors.
But this could give you an idea how to implement this 🙂
04-08-2022 6:13 PM
Many ways. Look at ABAP documentation, chapter "Internal tables", there are lots of examples.
04-09-2022 8:18 PM
Please check the below link if your SAP system is running with SAP HANA DB. Solution can be achieved via code pushdown technology.
https://blogs.sap.com/2019/06/19/single-row-to-multiple-rowstranspose-in-cds-views/
04-09-2022 9:42 PM
Hi Former Member,
please check the help about select union
https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abapunion_clause.htm
select userid, text1 as text from mytab
union
select userid, text2 as text from mytab
union
select userid, text3 as text from mytab
union
select userid, text4 as text from mytab
into table @data(lt_values)