Application Development 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: 

Update a database table dynamically

former_member602116
Participant
0 Kudos

Hi ABAP Experts,

Good day!

I have a test code to update a database table dynamically. (Currently, values are in strings since this is only a test code and my actual code is in a class. Unfortunately, I am unable to test it as I am still missing some logic.). My problem here is I actually see the correct values in <ls_hyper_update_log> that will be used to update the database table. However, the update statement returns a sy-subrc of 4, which means table was not updated. May you please check on what's wrong with the below code? Thank you very much.

Regards,

Katherine Darunday

FIELD-SYMBOLS:
    <ls_hyper_update_log> TYPE any,
    <ls_update>           TYPE any.
  DATA:
    lv_field1 TYPE /txn/de_hyper_gen_fld_name,
    lv_field2 TYPE /txn/de_hyper_gen_fld_name,
    lv_field3 TYPE /txn/de_hyper_gen_fld_name,
    lv_field4 TYPE /txn/de_hyper_gen_fld_name,
    lv_field5 TYPE /txn/de_hyper_gen_fld_name,
    im_field_1 type CHAR20,
    im_field_2 type CHAR20,
    im_field_3 type CHAR20,
    im_field_4 type CHAR20,
    im_field_5 type CHAR20,
  DATA: DYN_WA TYPE REF TO DATA.
  CREATE DATA DYN_WA TYPE /txn/t_hyper_log.
  ASSIGN DYN_WA->* TO <ls_hyper_update_log>.
  lv_field1 = 'HYPER_TEAM'. "im_s_hyper_tsk-field_name1.
  lv_field2 = 'HYPER_TASK'. "im_s_hyper_tsk-field_name2.
  lv_field3 = 'TASK_CLASS'. "im_s_hyper_tsk-field_name3.
  lv_field4 = 'MONI_DATE'.  ".m_s_hyper_tsk-field_name4.
  lv_field5 = 'MONI_TIME'.  "im_s_hyper_tsk-field_name5.

  im_field_1 = 'LOGISTICS'.
  im_field_2 = 'DYNAMIC'.
  im_field_3 = 'TEST'.
  im_field_4 = SY-DATUM.
  im_field_5 = SY-UZEIT.
  ASSIGN COMPONENT lv_field1 OF STRUCTURE <ls_hyper_update_log> TO <ls_update>.
  IF sy-subrc EQ 0.
    <ls_update> = im_field_1.
  ENDIF.
  ASSIGN COMPONENT lv_field2 OF STRUCTURE <ls_hyper_update_log> TO <ls_update>.
  IF sy-subrc EQ 0.
    <ls_update> = im_field_2.
  ENDIF.
  ASSIGN COMPONENT lv_field3 OF STRUCTURE <ls_hyper_update_log> TO <ls_update>.
  IF sy-subrc EQ 0.
    <ls_update> = im_field_3.
  ENDIF.
  ASSIGN COMPONENT lv_field4 OF STRUCTURE <ls_hyper_update_log> TO <ls_update>.
  IF sy-subrc EQ 0.
    <ls_update> = im_field_4.
  ENDIF.
  ASSIGN COMPONENT lv_field5 OF STRUCTURE <ls_hyper_update_log> TO <ls_update>.
  IF sy-subrc EQ 0.
    <ls_update> = im_field_5.
  ENDIF.
  UPDATE /txn/t_hyper_log FROM <ls_hyper_update_log>.
1 ACCEPTED SOLUTION

maheshpalavalli
Active Contributor

Can u check if any entry exists with the specified key field in the database? sy-subrc 4 will usually come if no entry is available in the data base with the specified primary key

4 REPLIES 4

Sandra_Rossi
Active Contributor
0 Kudos

It means that there's no line in the database table for the key fields in <ls_hyper_update_log>, or the update is impossible because the table has one or more unique indexes and the values would make a duplicate key for these indexes.

See ABAP doc: UPDATE

maheshpalavalli
Active Contributor

Can u check if any entry exists with the specified key field in the database? sy-subrc 4 will usually come if no entry is available in the data base with the specified primary key

former_member602116
Participant
0 Kudos

Hi Sandra,

Yes, I know meaning of sy-subrc 4 in Update statement. The table I am trying to update is actually new and only has one entry. The values I am trying to insert have different primary keys with that of the existing entry. Which is why I was wondering why sy-subrc is being encountered.

Thank you.

Regards,

Kath

former_member602116
Participant
0 Kudos

Hi Sandra,

"It means that there's no line in the database table for the key fields in <ls_hyper_update_log>"

Thank you very much for this. I just realized I should have used insert since it's a new entry and will not update any lines in the database table.I am now able to insert the new entry.

Again, thanks for the assistance.

Regards,

Kath