I want MOVE fields from itab1 to itab2 based on their field names. I have tried following:
CLEAR itab2.
MOVE-CORRESPONDING itab1 TO itab2.
This is working out, but only as long as the FIELDS are named equal.
Now I want to to something like that:
CLEAR itab2.
MOVE-CORRESPONDING itab1-field1 TO itab2-field2.
MOVE-CORRESPONDING itab1-field3 TO itab2-field4.
and so on.. But each time I try to do that I get following error "itab1-field1" is not a structure or an internal table.
I have also tried to write it like this MOVE-CORRESPONDING <itab1>-field1 but this doesn't work either.
How can I achieve what I want? Thanks for trying helping me out..
From ABAP 7.4 it can be done with the CORRESPONDING ... MAPPING statement:
itab_target = CORRESPONDING #( itab_source
MAPPING field2 = field1
field4 = field3 ).
Target will be based on source, where the fields have the same name, otherwise the MAPPING will be used (target-field2 will be source-field1, etc.) This works with structures and internal tables as well.
ABAP Help CORRESPONDING
Related
Recently I've come across a Read statement which gives Sy-subrc eq 8. this happens because the internal table has no records and at the same time variables in the WHERE clause are empty too. What I have been wondering is why wouldn't we check if the table is not initial before the read statement?
pleas let me know if we can check the itab is not initial before read statement.
Thanks!
Yes, you can check itab is not initial before read statement. what's bothering you? it's not an issue. And WITH KEY or WITH TABLE KEY clause used with read statement not WHERE.
And if there is no value exist in internal table for given key value then SY-SUBRC will be 4
It's a wrong assumption to make a direct link between READ TABLE on an empty table and SY-SUBRC = 8. If you read the official ABAP documentation, you will see that SY-SUBRC = 8 is related to the variant READ TABLE ... BINARY SEARCH.
So, with READ TABLE ... BINARY SEARCH, SY-SUBRC will be 8 if the searched line doesn't exist but if it was inserted it would be placed after the last line of the table. Of course, that's always the case when the internal table is empty.
Addendum May, 10th: SY-SUBRC = 8 may also occur with READ TABLE on internal tables of type SORTED (because it's using a binary search implicitly).
All tables may be listed with t-code SE16 and table DD02L. But how can that list be accessed programmatically?
It is rarely a good idea to access the database tables directly since you will have to deal with all kinds of technicalities you probably don't even know about - active / inactive versions, for example. You will also bypass all security and authorization checks, which might be irrelevant to you personally, but is undesirable in general. To get a list of tables, you can use the function module RPY_TABLE_SELECT. This function module will take care of the version handling and provide the description in the language of your choice as well.
Improved Alex code in some way and put it as an option:
SELECT tabname
FROM DD02L
INTO TABLE #DATA(itab)
WHERE TABCLASS = 'TRANSP'.
LOOP AT itab ASSIGNING FIELD-SYMBOL(<FS>).
WRITE:/ <FS>.
ENDLOOP.
Several things were refined: incline declarations were utilized, field-symbols added, SELECT * and WHERE IN were omitted and so on. Also tables in SAP have only TRANSP class, INTTAB class belongs to structures.
Note: the sample is functional since ABAP 7.40, SP08.
An ongoing search resulted in the following snippet:
DATA ITAB TYPE TABLE OF DD02L.
SELECT * FROM DD02L INTO TABLE ITAB WHERE TABCLASS IN ('TRANSP', 'INTTAB').
WRITE :SY-SUBRC .
DATA FS TYPE DD02L.
LOOP AT ITAB INTO FS.
WRITE:/ FS-TABNAME.
ENDLOOP.
Table description is given in table DD02T.
I am new in ABAP and I have to modify these lines of code:
LOOP AT t_abc ASSIGNING <fs_abc> WHERE lgart = xyz.
g_abc-lkj = g_abc-lkj + <fs_abc>-abc.
ENDLOOP.
A coworker told me that I have to use a structure and not a field symbol.
How will be the syntax and why to use a structure in this case?
I have no idea why the co-worker wants that you use a structure in this case, because using a field symbol while looping is usually more performant. The reason could be that you are doing some kind of a novice training and he wants you to learn different syntax variants.
Using a structure while looping would like this
LOOP AT t_abc INTO DATA(ls_abc)
WHERE lgart = xyz.
g_abc-lkj = g_abc-lkj + ls_abc-abc.
ENDLOOP.
Your code is correct, because Field symbol functions almost the same as a structure.
For Field symbol
Field symbol is a pointer,
so there is no data copy action for field symbol, and the performance is better
Well if we changed the value via field symbol, the internal table get changed also
For Structure
Structure is a copy of the data, so there is a data copy action, and the performance is bad if the data row is bigger than 200 bytes (based on the SAP ABAP programming guide for performance)
If changed the data in the structure, the original internal table remains the same because there are 2 copies of the data in memory
I want to dynamically get the structure of a dynamic table. Getting the table is no problem, but I stuck with getting the structure of the table.
DATA: lo_dynamic_table TYPE REF TO data.
FIELD-SYMBOLS: <lt_table_structure> TYPE table,
<ls_table_structure> TYPE any.
CREATE DATA lo_dynamic_table TYPE TABLE OF (lv_my_string_of_table).
ASSIGN lo_dynamic_table->* TO <lt_table_structure>.
// some code assigning the structure
Now I want to execute this command:
SELECT SINGLE * FROM (lv_my_string_of_table) INTO <ls_table_structure> WHERE (lv_dynamid_where_field).
If there is any other solution, I will be okay with that.
Take use of RTTS.
Runtime type services
With this framework You are able to get the desired type during runtime.
http://wiki.scn.sap.com/wiki/pages/viewpage.action?pageId=42965
The desired class should be CL_ABAP_TABLEDESCR or even CL_ABAP_DATADESCR.
They will do the work for You.
As it seems like, You are getting a ddic table name and want to select dynamically data from the tablename into a generic internal table.
So, if You are getting already an nice ddic name, then the usage of rtts is even more simple.
Because You have the ddic name.
Usually there are also many function modules ( mostly in the namespace-prefix "RPY_*" ).
There You surely can find one, which determines the structure of a table, whether it contains includes, and so on.
But, try typedescriptors first, I would start with cl_abap_tabledescr=>get_table_line_type.
This code worked for my case:
DATA: table_name type string,
lo_dynamic_table TYPE REF TO data,
lo_dynamic_line TYPE REF TO data.
FIELD-SYMBOLS: <lt_table_structure> TYPE table,
<ls_table_structure> TYPE any.
table_name = 'yourtablename'.
CREATE DATA lo_dynamic_table TYPE TABLE OF (table_name).
ASSIGN lo_dynamic_table->* TO <lt_table_structure>.
CREATE DATA lo_dynamic_line LIKE LINE OF <lt_table_structure>.
ASSIGN lo_dynamic_line->* TO <ls_table_structure>.
I added code for dynamic hashed table with dynamic keys.
DATA TAB_NAME LIKE SY-TNAME VALUE 'SCARR'.
DATA KEYTAB TYPE TABLE OF STRING.
DATA DREF TYPE REF TO DATA.
FIELD-SYMBOLS <F_TAB> TYPE ANY TABLE.
APPEND 'CARRID' TO KEYTAB.
CREATE DATA dref TYPE HASHED TABLE OF (TAB_NAME)
WITH UNIQUE KEY (KEYTAB).
ASSIGN dref->* TO <F_TAB>.
SELECT *
FROM (TAB_NAME)
INTO TABLE <F_TAB>.
cl_demo_output=>display( <F_TAB> ).
Consider also links.
https://help.sap.com/doc/saphelp_nw70/7.0.31/en-US/79/c55497b3dc11d5993800508b6b8b11/content.htm?no_cache=true
https://archive.sap.com/discussions/thread/92739
Is there a shortcut or should I just loop at the table and check?
I mean I am using an internal table and I want to check if a value is contained in one field of the internal table and I don't want to loop the table to find the value. Is it possible?
To check for a specific value without doing a loop or transferring values to a work area, you can use the READ statement with the addition TRANSPORTING NO FIELDS like so:
READ TABLE itab WITH KEY FIELD = 'X' TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
"Read was successful.
ENDIF.
UPDATE: From release 740 ABAP contains the predicate function LINE_EXISTS which is described in this blog post.
It's a built-in function to which you pass a table expression. Using the above example:
IF line_exists( itab[ field = 'X' ] ).
"Do stuff
ENDIF.
Full syntax of table expression in that predicate see here: https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abentable_expressions.htm
Selam,
If you are going to use loop in your algorithm, then you can use something like this:
LOOP ITAB WHERE FIELD = 'X'.
"code sample
ENDLOOP.
If you are not going to use a loop in your code, then i don't think there is a specific way to check for a specific value in itab.
Hope its helpful.
Talha