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

internal table in shared memory

rainer_hbenthal
Active Contributor
0 Likes
1,228

I'm using an internal table in a shared memory area. Depending on the existance of rows i would like to modify these rows or appending new rows.

So i need read and write access.

I tried to use attach_for_write( ) but after that i cannot read the table, sy-subrc is always 4.

So i tried to read the table with attach_for_read( ) first. I can read it, and the result is now sy-subrc = 0.

But after attaching the table via attach_for_write i'm unable to modify the table, how can i do this?

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
1,006

Use method ATTACH_FOR_UPDATE to modify the data in the shared memory.

First Read it using the ATTACH_FOR_READ.

Now get the object by ATTACH_FOR_UPDATE

Call your Update method

Commit work by


    my_handle->detach_commit( ).

Regards,

Naimesh Patel

7 REPLIES 7
Read only

Former Member
0 Likes
1,006

assign a field symbol to ur internal table and make changes to ur field symbol. the changes shud get reflected in ur internal table also.

Read only

naimesh_patel
Active Contributor
0 Likes
1,007

Use method ATTACH_FOR_UPDATE to modify the data in the shared memory.

First Read it using the ATTACH_FOR_READ.

Now get the object by ATTACH_FOR_UPDATE

Call your Update method

Commit work by


    my_handle->detach_commit( ).

Regards,

Naimesh Patel

Read only

0 Likes
1,006

The problem is the modify statement itself, using modify the report abends because the modify statement does not have a binding to that table anymore.

fieldsymbols do not work, they are modifying the table directly which will not work if i'm in the read phase.

Read only

0 Likes
1,006

Do you try to modify after the DETACH?

I am able to modify my shared memory data using this code:


TRY.
    my_handle = ztest_np=>attach_for_update( ).

    CREATE OBJECT my_data AREA HANDLE my_handle.

    my_handle->set_root( my_data ).

*.. data modification logic begin
    CLEAR: my_data->t_data.

    SELECT vbeln
           FROM  vbap
           INTO  CORRESPONDING FIELDS OF TABLE my_data->t_data
           UP TO 25 ROWS.

*.. data modification logic end
    my_handle->detach_commit( ).

  CATCH cx_shm_attach_error.
    ...
ENDTRY.

Regards,

Naimesh Patel

Read only

0 Likes
1,006

To simplify, just use ROOT attribute :

TRY.
    my_handle = ztest_np=>attach_for_update( ).
 
*.. data modification logic begin
    CLEAR: my_handle->root->t_data.
 
    SELECT vbeln
           FROM  vbap
           INTO  CORRESPONDING FIELDS OF TABLE my_handle->root->t_data.
           UP TO 25 ROWS.
 
*.. data modification logic end
    my_handle->detach_commit( ).
 
  CATCH cx_shm_attach_error.
    ...
ENDTRY.

Read only

0 Likes
1,006

myshmhandle = zca_shm_repstat_area=>attach_for_update( ).

CREATE OBJECT myroot AREA HANDLE myshmhandle.

read table myshmhandle->root->it_stat with key mandt = sy-mandt report = sy-repid
  ASSIGNING <p>.

 if sy-subrc = 0.
    <p>-freq    = wa_stat-freq + 1.
    <p>-ldate   = sy-datum.
    <p>-ltime   = sy-uzeit.
    <p>-userid  = sy-uname.

    ins = abap_false.
  else.
    wa_stat-mandt   = sy-mandt.
    wa_stat-report  = sy-repid.
    wa_stat-freq    = 1.
    wa_stat-ldate   = sy-datum.
    wa_stat-ltime   = sy-uzeit.
    wa_stat-userid  = sy-uname.

    ins = abap_true.
    append wa_stat to myroot->it_stat.
  endif.

myshmhandle->set_root( myroot ).
myshmhandle->detach_commit( ).

Running the first time, everyting is ok. Running the second time the read will give back sy-subrc = 0 and <p> contains the correct values. after running to the end the table has no rows at all and is inconsitent I cant see what i'm doing wrong.

Read only

0 Likes
1,006

I think this should work with these corrections :

myshmhandle = zca_shm_repstat_area=>attach_for_update( ).
 
* CREATE OBJECT myroot AREA HANDLE myshmhandle. "DELETE
 
read table myshmhandle->root->it_stat with key mandt = sy-mandt report = sy-repid
  ASSIGNING <p>.
 
 if sy-subrc = 0.
    <p>-freq    = wa_stat-freq + 1.
    <p>-ldate   = sy-datum.
    <p>-ltime   = sy-uzeit.
    <p>-userid  = sy-uname.
 
    ins = abap_false.
  else.
    wa_stat-mandt   = sy-mandt.
    wa_stat-report  = sy-repid.
    wa_stat-freq    = 1.
    wa_stat-ldate   = sy-datum.
    wa_stat-ltime   = sy-uzeit.
    wa_stat-userid  = sy-uname.
 
    ins = abap_true.
*    append wa_stat to myroot->it_stat. "DELETE
    append wa_stat to myshmhandle->root->it_stat. "ADD
  endif.

* myshmhandle->set_root( myroot ). "DELETE
myshmhandle->detach_commit( ).