‎2009 Jun 08 8:47 AM
Hi,
I've got a query related to Dynamic table read with Dynamic key fields.
e.g.
Int. Table Name#### CASE 1#### CASE 2
T_CURRENT#### Structure of MARA#### Structure of BSEG
T_COMPARE#### Structure of MARA#### Structure of BSEG
T_FIELD#### MATNR#### BUKRS/BELNR/GJAHR/BUZEI
T_CURRENT and T_COMPARE tables have dynamic structure, and they will be populated at run time based on the table name specified. So, table keys depend on the table name selected by the user, which will be available in T_FIELDS table.
I've to read contents of the table T_CURRENT, for each record of T_COMPARE table using key fields. As mentioned in given example, we can have multiple scenarios, where Key fields vary from 1 to n.
So, how do I tackle this logic?
Thanks in advance.
Regards,
Amit
Edited by: Amit Satose on Jun 8, 2009 9:49 AM
‎2009 Jun 08 9:26 AM
If your dynamic table has, for example, a field with the name "FIELD", you can use
READ TABLE <dynamic_table> INTO <dynamic_work_area> WITH KEY ('FIELD') = some_value.or better
CONTANTS: l_field TYPE fieldname VALUE 'FIELD'.
READ TABLE <dynamic_table> INTO <dynamic_work_area> WITH KEY (l_field) = some_value.matt
‎2009 Jun 08 9:25 AM
Hi,
Please make use of field-symbols for creating the dynamic internal table and
use ASSIGN COMPONENT OF STRUCTURE statement to access the fields.
You can check this wiki for more details.
[https://wiki.sdn.sap.com/wiki/x/UYA_Bg]
Regards,
Ankur Parab
‎2009 Jun 08 9:26 AM
If your dynamic table has, for example, a field with the name "FIELD", you can use
READ TABLE <dynamic_table> INTO <dynamic_work_area> WITH KEY ('FIELD') = some_value.or better
CONTANTS: l_field TYPE fieldname VALUE 'FIELD'.
READ TABLE <dynamic_table> INTO <dynamic_work_area> WITH KEY (l_field) = some_value.matt
‎2009 Jun 08 11:20 AM
Hi Matt n Ankur,
Thanks for your reply, but in my scenario, I'm not sure about Number of key fields.
So, in where clause how do I include multiple keys in the below mentioned syntax?
WITH KEY ('FIELD') = some_value.
Regards,
Amit
‎2009 Jun 08 11:28 AM
Hi,
In that case instead of READ you can go for a LOOP.
Regards,
Ankur Parab
‎2009 Jun 08 11:52 AM
Hi Ankur,
As per my knowledge, system doesn't accept below mentioned syntax.
Loop @ T_table
where (W_WHERE_CLAUSE).
ENDLOOP.
Do you have any idea about the syntax to be used?
Regards,
Amit
‎2009 Jun 08 12:23 PM
Hi,
What i meant by loop was as follows:-
PARAMETERS: p_matnr TYPE matnr,
p_matnr1 TYPE matnr.
DATA: t_current TYPE STANDARD TABLE OF mara.
DATA: t_compare TYPE STANDARD TABLE OF mara,
t_fields TYPE STANDARD TABLE OF string.
DATA: wa TYPE mara,
wa1 TYPE string,
wa2 TYPE mara.
DATA : cntr TYPE i,
flag TYPE x.
FIELD-SYMBOLS: <f1> TYPE ANY,
<f2> TYPE ANY.
wa1 = 'MATNR'.
APPEND wa1 TO t_fields.
wa1 = 'MEINS'.
APPEND wa1 TO t_fields.
SELECT * FROM mara
INTO TABLE t_current
WHERE matnr EQ p_matnr.
SELECT * FROM mara
INTO TABLE t_compare
WHERE matnr EQ p_matnr1.
CLEAR : flag .
LOOP AT t_compare INTO wa.
LOOP AT t_current INTO wa2.
CLEAR : cntr.
LOOP AT t_fields INTO wa1.
ASSIGN COMPONENT wa1 OF STRUCTURE wa TO <f1>.
ASSIGN COMPONENT wa1 OF STRUCTURE wa2 TO <f2>.
IF <f1> EQ <f2>.
* read successful
ELSE.
ADD 1 TO cntr.
* read unsuccessful
ENDIF.
ENDLOOP.
IF sy-subrc EQ 0.
IF cntr EQ 0.
* record found
WRITE:/ 'Record found'.
flag = 'X'.
EXIT.
ELSE.
* record not found
WRITE:/ 'Record not found'.
ENDIF.
ENDIF.
ENDLOOP.
IF flag EQ 'X'.
EXIT.
ENDIF.I hope this helps you.
The read would be a bit tedious but thats the way it is if you have dynamic fields.
Regards,
Ankur Parab
Edited by: Ankur Parab on Jun 8, 2009 4:53 PM
‎2009 Jun 08 12:54 PM
Hi Ankur,
Thanks again for quick turnaround.
But, as per your logic, all the fields of the table are getting compared. But I need to compare only key fields, and if match found/not found, need to generate report for the non-key fields contents.
Do you see any modification for this logic to suit the requirements?
Regards,
Amit
‎2009 Jun 08 1:33 PM
Hi,
I was under the assuption that the table T_FIELDS will contain only the keyfields.
Anyways if you are referring to data dictionary tables then you can make use of FM 'DDIF_FIELDINFO_GET' to get the fields of the particular table. The field KEYFLAG of the tables parameter DFIES_TAB will indicate whether the field is a key field or not.
So in this way you can get your key fields.
Incase your dynamic tables are not directly coming from the data dictionary then in that case you will have to think of some solution as to how to find the key fields.
Regards,
Ankur Parab
‎2009 Jun 09 6:59 AM
Modified the logic provided by Ankur to suit the end user requirement.