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

Dynamic Table read with Dynamic Key fields

Former Member
6,120

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

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
4,013

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

9 REPLIES 9
Read only

Former Member
0 Likes
4,013

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

Read only

matt
Active Contributor
4,014

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

Read only

Former Member
4,013

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

Read only

Former Member
4,013

Hi,

In that case instead of READ you can go for a LOOP.

Regards,

Ankur Parab

Read only

Former Member
4,013

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

Read only

Former Member
4,013

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

Read only

Former Member
4,013

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

Read only

Former Member
4,013

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

Read only

Former Member
4,013

Modified the logic provided by Ankur to suit the end user requirement.