2023 Sep 14 5:57 PM
Hey guys,
Help me with this problem:
I'm trying to make an UPDATE of just two pieces of data in my table, however, I don't understand how I can make this WHERE work, if I put it as IN the UPDATE will be done in all the fields in my table, this shouldn't happen, the UPDATE will be done only in the field selected in SALV.
*&---------------------------------------------------------------------*
*& Report zcadr_canteen_register
*&---------------------------------------------------------------------*
REPORT zcadr_canteen_register.
INCLUDE zcadr_canteen_register_s01.
INCLUDE zcadr_canteen_register_f01.
START-OF-SELECTION.
PERFORM read_data.
PERFORM display_alv.
*&---------------------------------------------------------------------*
*& Include zcadr_canteen_register_s01
*&---------------------------------------------------------------------*
TABLES: zcr_canteen, t001w.
DATA: gt_canteen TYPE STANDARD TABLE OF zcr_canteen,
gs_cant TYPE zcr_canteen,
go_alv TYPE REF TO cl_salv_table.
SELECTION-SCREEN BEGIN OF BLOCK b1.
SELECT-OPTIONS: so_werk FOR t001w-werks.
SELECTION-SCREEN END OF BLOCK b1.
*&---------------------------------------------------------------------*
*& Include zcadr_canteen_register_f01
*&---------------------------------------------------------------------*
FORM save_canteen.
DATA lv_werk_valid TYPE abap_bool.
SELECT code name werk FROM zcr_canteen INTO TABLE gt_canteen.
SELECT SINGLE * FROM t001w WHERE werks = zcr_canteen-werk.
gs_cant-code = zcr_canteen-code.
gs_cant-name = zcr_canteen-name.
gs_cant-werk = zcr_canteen-werk.
UPDATE zcr_canteen SET name = zcr_canteen-name, werk = zcr_canteen-werk WHERE code IN @so_werk.
ENDFORM.
FORM read_data.
SELECT * FROM zcr_canteen INTO TABLE gt_canteen
WHERE code IN so_werk.
ENDFORM.
2023 Sep 17 1:44 PM
Here is a valid syntax of UPDATE:
DATA sflight TYPE sflight.
UPDATE sflight
SET carrid = @sflight-carrid,
connid = @sflight-connid
WHERE carrid = @sflight-carrid.
IF sy-subrc = 0.
COMMIT WORK. " success
ELSE.
ROLLBACK WORK. " failure
ENDIF.
If it doesn't update, you can see SY-SUBRC = 4 because there's no line in the table corresponding to the WHERE clause. Only you can analyze and understand why there is a discrepancy between your table contents and your WHERE clause.
Good luck!
2023 Sep 14 6:15 PM
Please note: you may edit questions when needed, you don't have to delete and repost
2023 Sep 14 6:46 PM
Not sure to understand the question, why do you use SO_WERK and not ZCR_CANTEEN-CODE? Did you try this code below?
DATA sflight TYPE sflight.
UPDATE sflight
SET carrid = @sflight-carrid,
connid = @sflight-connid
WHERE carrid = @sflight-carrid.
IF sy-subrc = 0.
COMMIT WORK. " success
ELSE.
ROLLBACK WORK. " failure
ENDIF.
EDIT after Felipe comment "should work [...] without success": I guess you mean the code has SYNTAX ERROR, so you should post the message. I edited the code above to make sure it compiles. If it does a "failure" (go to rollback), it means that the WHERE condition doesn't find an existing line to update, or the update will lead to a duplicate in a unique index, only you can solve it.2023 Sep 14 10:12 PM
Hello Sandra,
Yes, I tried that way, in my opinion it should work too, but without success, so I'm a little lost in this sense, I couldn't find another logic that would make this work.
But to summarize the problem, what I want to happen is an update of just two data in the table, name and werks.
2023 Sep 15 5:43 AM
I have edited my previous comment with verified ABAP syntax and advice in order to help you find by yourself.
2023 Sep 15 7:32 AM
just to confirm your situation, so you want to update the field NAME, WERK of selected data based on the select-option so_werk, but instead, you updated the NAME, WERK of all rows based on your code?
2023 Sep 15 8:04 AM
FORMs and TABLE statements are obsolete and should not be used in new programs.
INCLUDEs are not necessary in modern IDEs.
2023 Sep 15 2:46 PM
Hi Sandra, thanks for your help and time, I'll see what I can do and hope I'm successful.
EDIT. Thanks for the help, I managed to find the problem, it is falling into rollback and the variable does not feed. I need to study and use more of these forms of verification.
2023 Sep 15 2:51 PM
Hello Siswanto, yes, I want to update the name and work field. I tried this way as shown in the code, but with WHERE IN SO_WERK it updated all the rows in the table, I also tried with WHERE = ZCR_CANTEEN-CODE, but it didn't update.
I think it might be a syntax issue, but I didn't get any error messages or warnings
2023 Sep 15 2:53 PM
Hello Matthew, thanks for your answer, could you tell me a little about it? I'm still very new to the ABAP world, some things still seem confusing to me
2023 Sep 17 10:05 AM
You should endeavour to learn OO principles and program with classes and methods. The report program itself should contain as little as possible.
2023 Sep 17 1:44 PM
Here is a valid syntax of UPDATE:
DATA sflight TYPE sflight.
UPDATE sflight
SET carrid = @sflight-carrid,
connid = @sflight-connid
WHERE carrid = @sflight-carrid.
IF sy-subrc = 0.
COMMIT WORK. " success
ELSE.
ROLLBACK WORK. " failure
ENDIF.
If it doesn't update, you can see SY-SUBRC = 4 because there's no line in the table corresponding to the WHERE clause. Only you can analyze and understand why there is a discrepancy between your table contents and your WHERE clause.
Good luck!
2023 Sep 18 7:47 AM
As I have tried it myself, I don't think it would update all table, only the filled in value inside the Select Option SO_WERK, unless your SO_WERK is empty, then of course it would update all row of data in the table.
-> the UPDATE will be done only in the field selected in SALV.
But, based on your original question, if this is what you are trying to achieve, then instead of using SO_WERK to update, you need find out which row is actually selected, move them into some data container ( itab, range, select-option, etc) and update based on that.