2008 Jul 17 9:26 AM
Hello Experts.
So this may be a question like "They don't have anything to do with each other" but during this short time I've being working with ABAP; I've saw people that used each one of them to do pretty much the same thing; Select data, loop through it and write it; so I think that there must be some kind of "rule" that tells us which is the best way to use each one of them.
I think that a local table mimics a database table, a field-symbol is like a pointer and I used to work with cursors in Trans-SQL which were very powerful, but are they the same in ABAP ? Which of them is faster?
Can you please tell me when to use the different techniques and/or arrange me some kind of documentation on this matter?
Thank you for your help.
Jorge
2008 Jul 17 9:32 AM
A local table is used internally in a program as a "copy" of database contents. This allows for faster processing in some cases; reducing the actual load on the database itself.
Field symbols are, like you say, pointers that are set dynamically at run time; allowing for more flexible data handling within a program.
I suggest you get a decent ABAP book, for instance from sap-press com; or check the ABAP help documentation.
Good luck with ABAP!
2008 Jul 17 10:21 AM
Hi,
As you said, there always exist a thumb rule to follow while looping through a local/internal table.
Field symbols are not actually pointers like in other programming languages like C, they are just for assignment. Actual look alike for pointers in ABAP is data references which we create the following way.
take the below code snippet
LOOP AT itab INTO wa
wa-val1 = 2.
APPEND wa to itab.
ENDLOOP.
Vs.
LOOP AT itab ASSIGNING <fs>.
<fs>-val1 = 2.
ENDLOOP.
The first one copies the content from itab and put into workarea wa.Additional APPEND is needed to update the table.
But the second one assigns the field symbol to the corresponding record
in itab and the assignment automatically updates the record in itab which is very much efficient.
Please note that there will be heavy time consumtion if the above table has nested/deep structures within it where assignment of field symbols to internal tables itself will take so much time.
So you need to choose on your own the either of the method whichever is helpful.
One more info, the field symbols and the data references are used in dynamic programming.i.e you are not sure about the incoming structure or its type.
please see the below example for generic programming.
DATA: dref TYPE REF TO DATA.
FIELD-SYMBOLS <fs> TYPE any. "this field symbol can point to any variable.
CREATE DATA dref TYPE ref to (TYPE_1). "TYPE_1 is a variable where u dont know its type
ASSIGN dref->* TO <fs>
Award points if the answer is helpful.
Best Regards,
Ragha.
2008 Jul 17 10:52 AM
Thank you for your replies.
So you mean that, if I use the field-symbol always assigned to a specific structure instead of assign it to "ANY" I can have a better control of it and, as I need less code (don't need to do any appends), it is faster. Of course I need to be extra careful, because it will update immediately every record just by assigning the field-symbol to a value.
Then, if I'm running something in online mode, especially if I have the user to do something, the better way is to use the local tables, this way, the user can do whatever changes within the local table and after that, we can update the DB table from the local one.
If we're running an batch program or a report that only presents results, we're better with the field-symbols, as long as we don't have tables with deep structures in which case we should use the local tables once again.
Is that it?
Jorge