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

Question in ABAP syntax, read & insert data from internal table, while loop

Former Member
0 Likes
1,408

Hi, SDN Fellow.

I am from Java background and learnt ABAP, I don't usually write much ABAP code.

I am trying to implement the following logic in a RFC now.

I have one z-custom database table, the structure as the following:

It has two columns, with these sample data.

Says datable table is ZEMPMGRTAB.

EmployeeID,ManagerID
--------------------
user10,user1
user9,user1
user8,user1
user7,user2
user6,user2
user5,user2
user4,user2
user2,user1

The logic is this:

I have a input parameter, userid. I am using this parameter to have a select statement to query the record into export table,EXPTAB 'LIKE' table ZEMPMGRTAB.

SELECT * FROM  ZEMPMGRTAB
        
  into table EXPTAB
   WHERE  EMPLOYEEID  = USERID.

Say, my parameter value, USERID ='USER4'.

Referring to the sample data above, I can get the record of this in my EXPTAB,

EmployeeID,ManagerID
--------------------
user4,user2

Now, I want to iterately use the EXPTABLE-ManagerID

as the USERID input in SELECT statement, until it has no return result. Then, insert the new records in

EXPTAB.

In above new loop case, we will get this table content in EXPTAB,

EmployeeID,ManagerID
--------------------
user4,user2
user2,user1

I kind of think of the pseudocode logic as below:

(These may not be a valid ABAP code, so I need help to convert/correct them)

DATA: 
IWA TYEP ZZEMPMGRTAB, 
ITAB 
HASHED TABLE OF ZZEMPMGRTAB 
WITH UNIQUE KEY EMPLOYEEID. 


SELECT * FROM  ZEMPMGRTAB
        
  into table ITAB
   WHERE  EMPLOYEEID  = USERID.

*Question 1: I cannot insert a internal table to export table, it is *incompatible type, what is the alternative way fo this?
*Question 2: How can I access thedata of the internal table like this,ITAB-MANAGERID? As if I can, I would do this:
 

* IWA-EMPLOYEEE = ITAB-EMPLOYEEID. IWA-MANAGERID = IWA-MANAGERID. INSERT IWA INTO TABLE EXPTAB.

* Question 3: Is the 'NE NULL' - 'not equal to NULL' is right syntax?
IF ITAB NE NULL.
INSERT ITAB INTO EXPTAB.
ENDIF

* Question 4: Is my WHILE loop setup right here? And are the syntax right?

WHILE ITAB NE NULL.

SELECT * FROM  ZEMPMGRTAB
        
  into table ITAB 
   WHERE  EMPLOYEEID  = ITAB-MANAGERID.

IF ITAB NE NULL.
INSERT ITAB INTO EXPTAB.
ENDIF

REFRESH ITAB.

ENDWHILE.

Assume all the syntax and logic are right, I should get this result:

EmployeeID,ManagerID
--------------------
user4,user2
user2,user1

If I have a new entry in datable table,ZEMPMGRTAB like this:

user1,user0

My pseudocode logic will get this result:

EmployeeID,ManagerID
--------------------
user4,user2
user2,user1
user1,user0

I truly appreciate if you can help me to validate the above syntax and pseudocode logic.

Thanks in advance.

KC

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,002

Hi

What are the primary key fields in table ZEMPMGRTAB.

Based on that, it will easy to write the logic.

Regards

Madhan

8 REPLIES 8
Read only

mvoros
Active Contributor
0 Likes
1,002

Hi,

why do not you use a simple recursion for your task.



form add_line using i_user type ZEMPMGRTAB-EmployeeID
                    changing t_result type XXX "table with line EXPTAB.

data: ls_row type ZEMPMGRTAB.

* Get record for i_user
select single * into ls_row from  ZEMPMGRTAB
 where EmployeeID = i_user.

if sy-subrc NE 0.
* Do nothing, there is not manager for this employee
else.
* Store result 
   append ls_row into t_result.
* Call recursion
 perform add_line using ls_row-ManagerID
                          changing t_result.
endif.

endform.

Q2 - check documentation for ABAP commands READ TABLE and LOOP.

Q3 - NOT xxx IS INITIAL.

Q4 - check documentation for LOOP command

Cheers

Read only

Former Member
0 Likes
1,002

Martin,

Let me try to understand your suggestion, and try out your suggestion.

Will get back to you.

Thanks for your constructive suggestion.

KC

Read only

Former Member
0 Likes
1,002

Hi, Martin.

I converted your suggested code snippet to my RFC code. Here, I have couple more questions on the errors I got.

FUNCTION ZGETSOMEINFO3.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(USERID) TYPE  AWTXT
*"  EXPORTING
*"     VALUE(RETURN) TYPE  BAPIRETURN
*"  TABLES
*"      APPROVERT STRUCTURE  ZTAB_FMAPPROVER
*"----------------------------------------------------------------------

** Question 1: How do I call the form function?*
add_line(USERID).

ENDFUNCTION.

**Question 2: Is the form-endform block should be inside function-endfunction? As I got an error if I put it inside.*
 
form add_line using i_user type ZTAB_FMAPPROVER-EmployeeID
                    changing t_result type ZTAB_FMAPPROVER.

data: ls_row type ZTAB_FMAPPROVER.

* Get record for i_user
select single * into ls_row from ZTAB_FMAPPROVER
 where EmployeeID = i_user.

if sy-subrc NE 0.
* Do nothing, there is not manager for this employee
else.


* Store result
**Question 3: I got this error message,"T_RESULT" is not an internal table "OCCURS n" specification is missing. How do I fix this?*
   append ls_row to t_result.
* Call recursion
 perform add_line using ls_row-ManagerID
                          changing t_result.
endif.

endform.

Thanks,

KC

Read only

Former Member
0 Likes
1,002

Hi KC,

For your Question 1. add_line(USERID).

Reply: You need to call this function module like this.


  perform add_line(USERID)

*Question 2: Is the form-endform block should be inside function-endfunction? As I got an error if I put it inside.

Reply: No the Form Endform shouldnot be in Function EndFunction. It should be in some other Include.

So you need to write your Form EndForm in another Include.

*Question 3: I got this error message,"T_RESULT" is not an internal table "OCCURS n" specification is missing. How do I fix this?

Reply: For RFC You sholuld not use the Header. Instead of this what you need to create a workarea like thins.


  DATA: T_RESULT TYPE STANDARD TABLE OF <ZZCUSTOM>  "--> zzcustom table is your z table.
  DATA: WA_RESULT TYPE <ZZCUSTOM>.             "WA_RESULT is your workarea

Thanks,

Chidanand

Read only

Former Member
0 Likes
1,002

Hi,

FUNCTION ZGETSOMEINFO3.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(USERID) TYPE  AWTXT
*"     VALUE(FMTYPEID) TYPE  AWTXT
*"  EXPORTING
*"     VALUE(RETURN) TYPE  BAPIRETURN
*"  TABLES
*"      APPROVERT STRUCTURE  ZTAB_FMAPPROVER
*"      ACTOWNERT STRUCTURE  ZTAB_FMACTOWNER
*"----------------------------------------------------------------------
DATA: T_RESULT TYPE STANDARD TABLE OF ZTAB_FMAPPROVER.

**Question 1: For this line, I got an error says "Program ''USERID" *not found. Is the syntax right, as the USERID is a parameter for the function.

perform add_line(USERID).

  ENDFUNCTION.

form add_line using i_user type ZTAB_FMAPPROVER.EMPLOYEEID
                    changing T_RESULT TYPE ZTAB_FMAPPROVER.

data: ls_row type ZTAB_FMAPPROVER.

* Get record for i_user
select single * into ls_row from ZTAB_FMAPPROVER
 where EmployeeID = i_user.

if sy-subrc NE 0.
* Do nothing, there is not manager for this employee
else.


* Store result
    • QUESTION 2: I am still got stuck on this line of code. It still *says that "T_RESULT" is not an internal table "OCCURS n" *specification is missing. I thought the line: "T_RESULT TYPE *ZTAB_FMAPPROVER" means declare internal table, T_RESULT as type of ZTAB_FMAPPROVER". Am I understand it wrongly?

append ls_row to t_result.


* Call recursion
 perform add_line using ls_row-ManagerID
                          changing t_result.
endif.

endform.

Thanks,

KC

Read only

Former Member
0 Likes
1,003

Hi

What are the primary key fields in table ZEMPMGRTAB.

Based on that, it will easy to write the logic.

Regards

Madhan

Read only

0 Likes
1,002

Madhan,

The primary key is the EMPLOYEEID.

The database table only consists of two columns, EMPLOYEEID & MANAGERID as shown from my first post.

Thanks for help.

KC

Read only

Former Member
0 Likes
1,002

This message was moderated.