Application Development 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: 

Loop on database table to fill internal table - Build several rows from one row - ABAP

Former Member
0 Kudos

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.

6 REPLIES 6

craigcmehil
Community Administrator
Community Administrator
0 Kudos

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.



Jeansy
Active Contributor
0 Kudos
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.

Jeansy
Active Contributor
0 Kudos

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 🙂

Sandra_Rossi
Active Contributor

Many ways. Look at ABAP documentation, chapter "Internal tables", there are lots of examples.

selva123
Explorer
0 Kudos

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/

ThorstenHoefer
Active Contributor
0 Kudos

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)