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 help regarding Dynamic Read Table statement

santoshkulkarni
Explorer
0 Likes
2,588

Hello

I know that the syntax for dymanic read table statement is

READ TABLE <ITAB> WITH KEY (KEY1) = VALUE1 (KEY2) = VALUE2 .....(KEYN) = VALUEN

Here is my problem..

I am dynamically creating an internal table based on parameter table entered by user.

The key for this table can be determined only at runtime.

The table entered might have by one field as key field or 10 key fields..

How can I use the dynamic read in this situation ?

Thanks for your help in advance,

Santosh

Edited by: Santosh Kulkarni on Jan 3, 2010 6:58 AM

Edited by: Santosh Kulkarni on Jan 3, 2010 7:01 AM

1 ACCEPTED SOLUTION
Read only

Clemenss
Active Contributor
0 Likes
1,614

Hi ajit,

without logging onto system and creating a sample, I can tell you what online documentation says about [Using a Different Search Key|http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb35f8358411d1829f0000e829fbfe/content.htm]

READ TABLE itab WITH KEY k1 = f1 ... kn = fn result.

The search key consists of arbitrary table fields k1 ... kn. If you do not know the name of a component until runtime, you can specify it dynamically as the content of a field n1 ... nn using (n1) = f1 (n2) = f2 . If n1 ... nn is empty when the statement is executed, the search field is ignored.

That means that for the READ TABLE statement you should use a number of key specifications not less than the possible maximum and then dynamically fill the key variables and key values.

Do not forget to clear before you do the next read for another table.

Hope it works.

Also, this is SAP NetWeaver 7.0, I think dynamic key specification was not possible earlier.

Regards,

Clemens

11 REPLIES 11
Read only

vinod_vemuru2
Active Contributor
0 Likes
1,614

Hi Santhosh,

Can't we get the key field details from fieldcatelog internal table(columnKEY = X)? I am not sure if we can build the WITH KEY clause into a variable and use it as we can do it for the WHERE clause of the SELECT. If this is possible, i hope your issue will be resolved.

eg:

CONCATENATE 'EBELN' 'IN' 'SO_EBELN' INTO l_where SEPARATED BY space.

Select...

...

WHERE (l_where).

Similarly will the read also works like this!!!?

READ TABLE <tab> WITH KEY (l_withkey).

Just try this option and let us know. If it works, its a new learning for me too

Thanks,

Vinod.

Read only

0 Likes
1,614

Not sure if the above would work. However i would like to get more information on the requirement and precisely how are you creating your internal tables.

Read only

0 Likes
1,614

Amit

I am creating 2 internal tables dynamically based on the user entered value using the dynamic internal table creation code

create data xxx type (p_table)

After this based on the field value of one of these internal tables, I am trying to read the other dynamic internal table using dynamic read . during this read , I am not able to code a generic read because the number of key fields are not known till the runtime.

Hope this clarifies my doubt

Thanks for your help,

Santosh

Read only

0 Likes
1,614

Vinod

I tried this but it did not work . The dynamic read does not work same as the dynamic where clause.

Thanks again for your help

Santosh

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,614

Also you can try loop with where clause, with dynamically built where clause.

Read only

Clemenss
Active Contributor
0 Likes
1,615

Hi ajit,

without logging onto system and creating a sample, I can tell you what online documentation says about [Using a Different Search Key|http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb35f8358411d1829f0000e829fbfe/content.htm]

READ TABLE itab WITH KEY k1 = f1 ... kn = fn result.

The search key consists of arbitrary table fields k1 ... kn. If you do not know the name of a component until runtime, you can specify it dynamically as the content of a field n1 ... nn using (n1) = f1 (n2) = f2 . If n1 ... nn is empty when the statement is executed, the search field is ignored.

That means that for the READ TABLE statement you should use a number of key specifications not less than the possible maximum and then dynamically fill the key variables and key values.

Do not forget to clear before you do the next read for another table.

Hope it works.

Also, this is SAP NetWeaver 7.0, I think dynamic key specification was not possible earlier.

Regards,

Clemens

Read only

0 Likes
1,614

Thanks Clemens

I did not know that the empty field will be ignored..

Thanks for your help

Santosh

Read only

Former Member
0 Likes
1,614

Hello Santosh,

please check out the following solution. The program demonstrates how to use the dynamic read statement with three key field conditions. Additional key fields can be added in the same way.


REPORT z_dynamic_read.

DATA: gt_itab TYPE REF TO data,
      ge_key_field1 TYPE char30,
      ge_key_field2 TYPE char30,
      ge_key_field3 TYPE char30,
      go_descr_ref TYPE REF TO cl_abap_tabledescr.

FIELD-SYMBOLS: <gt_itab> TYPE STANDARD TABLE,
               <gs_key_comp> TYPE abap_keydescr,
               <gs_result> TYPE ANY.

PARAMETERS: pa_table TYPE tabname16 DEFAULT 'SPFLI',
            pa_cond1 TYPE string DEFAULT sy-mandt,
            pa_cond2 TYPE string DEFAULT 'LH',
            pa_cond3 TYPE string DEFAULT '0123'.

START-OF-SELECTION.

* Create and populate internal table
  CREATE DATA gt_itab TYPE STANDARD TABLE OF (pa_table).
  ASSIGN gt_itab->* TO <gt_itab>.
  SELECT * FROM (pa_table) INTO TABLE <gt_itab>.

* Get the key components into the fields ge_key_field1, ...
  go_descr_ref ?= cl_abap_typedescr=>describe_by_data_ref( gt_itab ).
  LOOP AT go_descr_ref->key ASSIGNING <gs_key_comp>.
    CASE sy-tabix.
      WHEN 1.
        ge_key_field1 = <gs_key_comp>-name.
      WHEN 2.
        ge_key_field2 = <gs_key_comp>-name.
      WHEN 3.
        ge_key_field3 = <gs_key_comp>-name.
    ENDCASE.
  ENDLOOP.

* Finally, perform the search
  READ TABLE <gt_itab> ASSIGNING <gs_result>
          WITH KEY (ge_key_field1) = pa_cond1
                   (ge_key_field2) = pa_cond2
                   (ge_key_field3) = pa_cond3.
  IF sy-subrc = 0.
    WRITE / 'Record found.'.
  ELSE.
    WRITE / 'No record found.'.
  ENDIF.

One note: When an internal table is created dynamically like in my program above, the table key is not the key defined in the DDIC structure -- instead, the default key for the standard table is used (i.e. all non-numeric fields).

Hope this helps,

David

Read only

0 Likes
1,614

Hello David

Thanks..this solution should solve my problem..One thing I did not understand though..

You are saying that the default key for the dynamically created table is all non-numeric fields in the dynamic table and not the DDIC key.. If this is true what will happen if the dynamic table created has more than 16 non-numeric fields..

Thanks again for your help

Santosh

Read only

Former Member
0 Likes
1,614

Hello Santosh,

I guess there is no limitation on key fields for internal tables (at least I couldn't find one in the online help). You can use class CL_ABAP_TABLEDESCR to find out:


DATA: descr_ref TYPE REF TO cl_abap_tabledescr.
FIELD-SYMBOLS: <key_comp_wa> TYPE abap_keydescr.

descr_ref ?= cl_abap_typedescr=>describe_by_data_ref( your_itab_ref_here ).

WRITE: / 'Key Components:'.
LOOP AT descr_ref->key ASSIGNING <key_comp_wa>.
  WRITE <key_comp_wa>-name.
ENDLOOP.

Best regards,

David

Read only

krzysztof_murkowski2
Participant
0 Likes
1,614

yet another solution for read of internal table with dynamic specified keys:

http://www.kerum.pl/infodepot/00033

regards,

Kris