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

need hlep with some ABAP code

Former Member
0 Likes
1,518

I have a situation in which I need to read data from a table but then I need to basically move a particular field associated with the following record to the previous record. We are needing to placer the set-up time on the correct line. right now it calculates it correctly but places it with the 2nd like when really it belongs to the 1st record

19 REPLIES 19
Read only

Former Member
0 Likes
1,492

Hi there. You could try something like this. The only issue is that the first and second entries would end up with the same set-up time (unless you modify the code to set the value for the first record).

select * from ztable into wa_table_line.

if prev_table_line is INITIAL.

move wa_table_line to prev_table_line.

  • add code here if you want to change value of set_time for 1st record

else.

prev_table_line-set_time = wa_table_line-set_time.

append prev_table_line to itab.

move wa_table_line to prev_table_line.

endif.

endselect.

*add last record to table.

  • add code here if you want to change value of set_time for last record

append prev_table_line to itab.

update ztable from table itab.

I hope this helps.

- April King

Read only

0 Likes
1,492

the values will never be initial just one off so counter 1 value should be counter 2. I just don't know how to get it to grab the previous record

Read only

0 Likes
1,492

When you read from your table have a look at SY-TABIX. Subtract 1 from this value and it will tell you the index of the previous record.

Read only

0 Likes
1,492

That's basically what this code is doing. You are looping through the database records using the select statement. On the first loop, you just move the record to a work area (prev_table_line). On each subsequent loop, you move the value of set_time from the current record to the previous record work area and then append that previous record to an internal table. You then move the current record to the prevoius record work area. This continues for each record. After you've finished, you update the database table from your internal table. Here's an example of how the select works:

On pass 2 through the select,

prev_table_line contains record 1

wa_table_line contains record 2

So you are moving set_time from record 2 to record 1 when you say

prev_table_line-set_time = wa_table_line-set_time.

Is this clear?

- April

Read only

0 Likes
1,492

Thsi is what i tried but my selcect returens no data to the table.

select * from afru into WA_AFRU.

if prev_table_line is INITIAL.

move WA_AFRU to prev_table_line.

  • add code here if you want to change value of set_time for 1st record

else.

prev_table_line-ism02 = WA_AFRU-ism02.

append prev_table_line to itab.

move WA_AFRU to prev_table_line.

endif.

endselect.

*add last record to table.

  • add code here if you want to change value of set_time for last record append prev_table_line to itab.

update afru from table itab.

Read only

0 Likes
1,492

Your code works for me. Have you checked to make sure that there is data in table AFRU in your development client? This is the exact code that I ran:

TABLES: afru.

DATA: itab TYPE STANDARD TABLE OF afru.

DATA: wa_afru TYPE afru.

DATA: prev_table_line TYPE afru.

SELECT * FROM afru INTO wa_afru.

IF prev_table_line IS INITIAL.

MOVE wa_afru TO prev_table_line.

ELSE.

prev_table_line-ism02 = wa_afru-ism02.

APPEND prev_table_line TO itab.

MOVE wa_afru TO prev_table_line.

ENDIF.

ENDSELECT.

At the end of this I had 8 entries in my itab. Also check to make sure you have authorization to access that table.

- April

Read only

0 Likes
1,492

I didn't have an endselect. is this making a call to the database for every pass?

Read only

0 Likes
1,492

You need to have an endselect to close the loop. The only time you can skip the endselect is if you are using a "SELECT SINGLE" statement; you wouldn't want that here because then you would only get one record from the table. Yes, it will access the database each time through.

- April

Read only

0 Likes
1,492

Are your data staments correct? I get short dumps using your code.

Read only

0 Likes
1,492

I didn't get any errors when I checked my syntax, and I didn't get any runtime errors either.

- April

Read only

0 Likes
1,492

Would it be more effiecient then to just do I select to get all the data i need then loop at this internal table?

Read only

0 Likes
1,492

what version are you running? Would that make a difference

Read only

0 Likes
1,492

did you try to run it? I didn't get any errors either but when I run it gets through 1 pass and then bombs at the 2nd round

Read only

0 Likes
1,492

Yes, I ran it. I put a write statement after the endselect and set a break point there so I could check the itab for data. The version should not make a difference, as it is just a basic select statement.

- April

Read only

0 Likes
1,492

that is odd. I copied your code and get a short dump. Very odd

Read only

0 Likes
1,492

What is your dump? What is the error message?

- April

Read only

0 Likes
1,492

I moved it the code out of the user exit so now I have below which works fine but doens't wrtied the very last record.

SELECT * into table wa_afru from afru where

  • ersda > '01/01/2005'.

rueck = 0000000502.

loop at wa_afru.

IF prev_table_line IS INITIAL.

MOVE wa_afru TO prev_table_line.

ELSE.

if prev_table_line-rueck = wa_afru-rueck.

prev_table_line-ism01 = wa_afru-ism01.

APPEND prev_table_line TO itab.

MOVE wa_afru TO prev_table_line.

else.

MOVE wa_afru TO prev_table_line.

APPEND prev_table_line TO itab.

endif.

ENDIF.

endloop.

Read only

0 Likes
1,492

Yes. You need to add this line after the ENDLOOP:

APPEND prev_table_line TO itab.

This will include the last record in your table.

- April

Read only

0 Likes
1,492

thanks for all your help. I've awarded points.