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: 

modify the internal table data using field symbols

Former Member
0 Kudos

Hi,

I am working on field symbols in the abap program. I am trying to modify the records of the internal table. I am new to field pointers and going to through several threads, add the code in the program but not getting the desired results. I would appreciate your help in this regard.

Here is the code..

data: begin of dept_itab occurs 0,

deptid like p0001-kostl,

pstl2 like csks-pstl2,

end of dept_itab.

field-symbols: <fs_dept> like line of dept_itab.

      • the dept_itab table is populated then, few dept records have the record PSTL2 value to be updated. I am trying to do using field pointers.

loop at dept_itab assigning <fs_dept>.

select single * from csks where kostl = dept_itab-deptid.

<fs_dept>-deptid = dept_itab-deptid.

<fs_dept>-pstl2 = csks-pstl2.

append <fs_dept>. ??? how to append the record in the field symbol - Error here

endloop.

Then I need to store the updated records back to dept_itab.

Hence I added..

refresh dept_itab.

assing dept_itab to <fs-dept>

move <fs_dept> to dept_itab.

How to correct this? It has large number of records and to improve the perormance I am applying this technque.

Thanks in advance,

VG

1 ACCEPTED SOLUTION

former_member194669
Active Contributor
0 Kudos

First rule will not to use select inside a loop If you need better performance change the code this way.


if not dept_itab[] is initial.
select * from csks into table i_csks   " Here still we have performance issue, CSKS have key of
         for all entries in dept_itab  " kokrs, kostl datbi but you only key kostl
         where kostl = dept_itab-deptid.
endif.

sort i_csks by kostl.

loop at dept_itab assigning <fs_dept>.
read table i_csks with key kostl = <fs_dept>-deptid binary search
if sy-subrc eq 0.
<fs_dept>-deptid = dept_itab-deptid.
<fs_dept>-pstl2 = csks-pstl2.
 modify dept_itab from <fs_dept>.
endif.
endloop.

7 REPLIES 7

Former Member
0 Kudos

This will solve your problem

loop at dept_itab assigning <fs_dept>.

select single * from csks where kostl = dept_itab-deptid.

<fs_dept>-deptid = dept_itab-deptid.
<fs_dept>-pstl2 = csks-pstl2.
modify dept_itab from <fs_dept>.  "since you are assigning existing record to field symbol it "should be modified as you are updating records
endloop.

Former Member
0 Kudos

Dont use, refresh on your internal table. You will lose internal table data.

former_member194669
Active Contributor
0 Kudos

First rule will not to use select inside a loop If you need better performance change the code this way.


if not dept_itab[] is initial.
select * from csks into table i_csks   " Here still we have performance issue, CSKS have key of
         for all entries in dept_itab  " kokrs, kostl datbi but you only key kostl
         where kostl = dept_itab-deptid.
endif.

sort i_csks by kostl.

loop at dept_itab assigning <fs_dept>.
read table i_csks with key kostl = <fs_dept>-deptid binary search
if sy-subrc eq 0.
<fs_dept>-deptid = dept_itab-deptid.
<fs_dept>-pstl2 = csks-pstl2.
 modify dept_itab from <fs_dept>.
endif.
endloop.

Former Member
0 Kudos

Hi,

Thanks for the response. I had a clarification. while reading the data into the field symbol..

loop at dept_itab assigning <fs_dept>.

read table i_csks with key kostl = <fs_dept>-deptid binary search.

IF SY-SUBRC = 0.

<fs_dept>-deptid = i_csks-kostl. <------- the dept_itab-deptid is blank here

<fs_dept>-pstl2 = i_csks-pstl2.

modify dept_itab from <fs_dept>.

ENDIF.

endloop.

For the marked field symbol when the control comes to <fs_dept>-deptid the value from dept_itab-deptid is blank. I did not understand why it is so. hence I changed to i_csks-kostl. Can you tell me why?

Thanks again,

VG

0 Kudos

>

> loop at dept_itab assigning <fs_dept>.

> read table i_csks with key kostl = <fs_dept>-deptid binary search.

> IF SY-SUBRC = 0.

> <fs_dept>-deptid = i_csks-kostl. <------- the dept_itab-deptid is blank here

> <fs_dept>-pstl2 = i_csks-pstl2.

> modify dept_itab from <fs_dept>.

> ENDIF.

> endloop.

sort is important if you are using binary search....

sort i_csks by kostl.

...control comes to <fs_dept>-deptid the value from dept_itab-deptid is blank.

the data for the field deptid in the dept_itab might be blank...

Former Member
0 Kudos

Hi Vinu,

Try this...

loop at dept_itab assigning <fs_dept>.
read table i_csks with key kostl = <fs_dept>-deptid.
IF SY-SUBRC = 0.
<fs_dept>-deptid = i_csks-kostl.
<fs_dept>-pstl2 = i_csks-pstl2.
 ENDIF.
endloop.

<fs_dept>-deptid = i_csks-kostl. <------- the dept_itab-deptid is blank here -


> I guess dept_itab is the header of the internal table dept_itab. You are assigning a field symbol in the loop statement and I guess you are not populating anything to the header dept_itab. Hence obviously dept_itab-deptid will be blank

Few tips I want to share with you:

1. When we make modifications in the field symbol (that is assigned to an internal table), the corresponding record in the internal table also gets modified. So there is no need for modify or append statements. You can check this in debugging.

2. Whenever we use field symbols, In that case there is no need for the workarea as we can very well use the field symbol itself like a workarea.

In other words, Instead of

Data: it_dept_itab type standard table of dept_itab with header line.

we can use

Data: it_dept_itab type standard table of dept_itab.

3. We must be very careful whenever we clear an internal table for which we have assigned a field symbol. Even small mistake can go for a short dump.

Former Member
0 Kudos

Thanks for the response. Yes. I had declared the dept_itab as a internal table with header line. Hence it was returning the value as blank. I changed the code to read from i_csks table and it worked.

Thanks for everyone.

VG