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: 

how to update the data base table with data

Former Member
0 Kudos
86

i have two ztables, one is zfm_kfz and other one is zfm_kmvrg

zfm_kfz is maintained by using table maintenance generator as well as alv grid control for list display.

zfm_kfz the field r like this KFZR, GERAET, KOSTENTRAEGER, BEZEICHNUNG, TUVDATUMMMYYYY, ASUDATUMMMYYYY, KMSTAND, HISTO AND REIFEN.

PROBLEM: all the data in grid control r updated except KMSTAND

fields in zfm_kmvrg are kostentraeger, kfznr and kmstand i m creating table control for this screen here what ever enter the last km stand is updated in the list.for one kfznr many kostentraegers and kmstand, the last km stand is updated here , go through this code plz hepl me

CONTROLS tabctrl TYPE TABLEVIEW USING SCREEN 100.

DATA: cols LIKE LINE OF tabctrl-cols,

lines TYPE i.

DATA: ok_code TYPE sy-ucomm,

save_ok TYPE sy-ucomm.

DATA: itab TYPE TABLE OF zfm_kmvrg,

fs_itab LIKE LINE OF itab,

fl_change TYPE c,

fl_error TYPE c.

*TABLES fs_itab.

LOOP AT tabctrl-cols INTO cols.

cols-screen-input = '0'.

MODIFY tabctrl-cols FROM cols INDEX sy-tabix.

ENDLOOP.

*SELECT * FROM spfli INTO TABLE itab.

CALL SCREEN 100.

----


  • MODULE status_0100 OUTPUT

----


*

----


MODULE status_0100 OUTPUT.

SET PF-STATUS 'SCREEN_101'.

DESCRIBE TABLE itab LINES lines.

tabctrl-lines = lines.

ENDMODULE. "status_0100 OUTPUT

----


  • MODULE cancel INPUT

----


*

----


MODULE cancel INPUT.

LEAVE PROGRAM.

ENDMODULE. "cancel INPUT

----


  • MODULE read_table_control INPUT

----


*

----


MODULE read_table_control INPUT.

MODIFY itab FROM fs_itab INDEX tabctrl-current_line.

ENDMODULE. "read_table_control INPUT

----


  • MODULE user_command_0100 INPUT

----


*

----


MODULE user_command_0100 INPUT.

DATA:

lw_index TYPE i.

save_ok = ok_code.

CLEAR ok_code.

CASE save_ok.

WHEN 'ADD'.

LOOP AT tabctrl-cols INTO cols.

cols-screen-input = '1'.

MODIFY tabctrl-cols FROM cols INDEX sy-tabix.

ENDLOOP.

CLEAR fs_itab.

APPEND fs_itab TO itab.

WHEN 'SAVE'.

IF NOT itab[] IS INITIAL.

LOOP AT itab[] into FS_ITAB.

lw_index = sy-tabix.

IF NOT fs_itab IS INITIAL.

MODIFY ZFM_KMVRG FROM fs_itab.

IF sy-subrc EQ 0.

UPDATE ZFM_KFZ set kmstand = fs_itab-kmstand

WHERE kfznr = fs_itab-kfznr.

ELSE.

fl_error = 'X'.

WRITE:/ 'The record number', lw_index,

'has not been updated'.

ENDIF.

ENDIF.

ENDLOOP.

ELSE.

MESSAGE s000(0) WITH 'No data is present to update'.

ENDIF.

ENDCASE.

IF fl_error = 'X'.

LEAVE TO LIST-PROCESSING.

ELSE.

MESSAGE s000(0) WITH

'All the records have been updated successfully'.

ENDIF.

ENDMODULE. "user_command_0100 INPUT

IN SE51

PROCESS BEFORE OUTPUT.

MODULE STATUS_0100.

LOOP AT ITAB INTO fs_itab WITH CONTROL tabctrl.

ENDLOOP.

PROCESS AFTER INPUT.

MODULE CANCEL AT EXIT-COMMAND.

LOOP AT ITAB.

module read_table_control.

ENDLOOP.

module user_command_0100.

i m trying many times i m not getting proper output, plz help me on this

3 REPLIES 3

jayanthi_jayaraman
Active Contributor
0 Kudos
57

Hi,

In debugging,just find out,whether your internal table fs_itab is having the values before this modify statement.

IF NOT fs_itab IS INITIAL.

MODIFY ZFM_KMVRG FROM fs_itab.

If not,you need to look at this place,whether fs_itab is getting modified after each PAI.

MODULE read_table_control INPUT.

MODIFY itab FROM fs_itab INDEX tabctrl-current_line.

ENDMODULE. "read_table_control INPUT

PS:It's not better to handle database operations inside a loop.Move it as a bulk.

0 Kudos
57

hi,

i m checking in debbugging mode, the option SAVE is not worked, the cursor directly goes to else part and getting message data is updated succeefully.

but ADD button is working fine even i m not getting valus into the fs_itab.

plz send the solution for this problem

jayanthi_jayaraman
Active Contributor
0 Kudos
57

Hi,

I am hereby givng the similar sample code.Check this with your requirement.

In the flow logic of the screen 9000, write the following code.

PROCESS BEFORE OUTPUT.

  MODULE set_status.

  MODULE get_t_ctrl_lines.
  LOOP AT i_makt WITH CONTROL t_ctrl CURSOR t_ctrl-current_line.
* Dynamic screen modifications
    MODULE set_screen_fields.
  ENDLOOP.

*
PROCESS AFTER INPUT.

  LOOP AT i_makt.
    FIELD i_makt-pick MODULE check.
    FIELD i_makt-zmatnr MODULE zmatnr .
  ENDLOOP.

  MODULE user_command_9000.


In the program, write the following code.

PROGRAM SAPMZTC MESSAGE-ID zz.

***********************************************************************
* Tables Declaration
***********************************************************************
TABLES: zzz_makt.

***********************************************************************
* Internal table Declaration
***********************************************************************
DATA : i_makt TYPE STANDARD TABLE OF zzz_makt WITH HEADER LINE.

***********************************************************************
* Table control Declaration
***********************************************************************
CONTROLS: t_ctrl TYPE TABLEVIEW USING SCREEN '9000'.

***********************************************************************
* Variable Declaration
***********************************************************************
DATA : flg,           "Flag to set the change mode
       ln TYPE i.     "No. of records

*&---------------------------------------------------------------------*
*&      Module  get_T_CTRL_lines  OUTPUT
*&---------------------------------------------------------------------*
*  Populating data
*----------------------------------------------------------------------*
MODULE get_t_ctrl_lines OUTPUT.
  SELECT zmatnr zmaktx
         INTO CORRESPONDING FIELDS OF TABLE i_makt
         FROM zzz_makt.
  DESCRIBE TABLE i_makt LINES ln.
* To make the vertical scroll bar to come on runtime
  t_ctrl-lines = ln + 100.
ENDMODULE.                 " get_T_CTRL_lines  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
* Triggering event according to the user command
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
  DATA :lv_fcode LIKE sy-ucomm,    "Function Code
        lv_answer(1) type c.       "Storing the answer
  lv_fcode = sy-ucomm.
  CASE lv_fcode.
    WHEN 'CHANGE'.
* Setting the flag to make the table control in editable mode[excluding
* primary key].
      flg = 'Y'.
    WHEN 'DELETE'.
* Setting the flag to make the table control in editable mode after
* deleting the selected line
      flg = 'Y'.
* Confirmation of delete
      CALL FUNCTION 'POPUP_TO_CONFIRM'
        EXPORTING
         TITLEBAR       = 'Confirm'
         text_question  = 'Are you sure to delete from database?'
         TEXT_BUTTON_1  = 'Yes'(001)
         TEXT_BUTTON_2  = 'No'(002)
        IMPORTING
         ANSWER         =  lv_answer.
      if lv_answer eq '1'.
* Updating the database table from the internal table
        UPDATE zzz_makt FROM TABLE i_makt.
* Deleting the selected row from the internal table
        DELETE i_makt WHERE pick = 'X'.
* Deleting the selected row from the database table
        DELETE FROM zzz_makt WHERE pick = 'X'.
        MESSAGE s005 WITH 'Deleted Successfully'.
      ENDIF.
    WHEN 'SAVE'.
* Inserting new record or updating existing record in database table
* from the internal table
      MODIFY zzz_makt FROM TABLE i_makt.
      MESSAGE s005 WITH 'Saved Successfully'.
    WHEN 'BACK'.
      SET SCREEN '0'.
    WHEN 'EXIT' OR 'CANCEL'.
* Leaving the program
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_9000  INPUT

*&---------------------------------------------------------------------*
*&      Module  set_screen_fields  OUTPUT
*&---------------------------------------------------------------------*
* Setting the screen fields
*----------------------------------------------------------------------*
MODULE set_screen_fields OUTPUT.
  LOOP AT SCREEN.
    IF flg IS INITIAL.
      screen-input = 0.
    ELSEIF ( flg EQ 'Y' ).
      IF ( ( screen-name = 'I_MAKT-ZMAKTX'
             OR screen-name = 'I_MAKT-CHECK1' )
            AND t_ctrl-current_line LE ln ) .
* Making the screen fields as editable
        screen-input = 1.
      ELSEIF ( ( screen-name = 'I_MAKT-ZMATNR' )
                 AND t_ctrl-current_line LE ln ).
* Making the screen field as uneditable
        screen-input = 0.
      ENDIF.
    ENDIF.
* Modifying the screen after making changes
    MODIFY SCREEN.
  ENDLOOP.
ENDMODULE.                 " set_screen_fields  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  zmatnr  INPUT
*&---------------------------------------------------------------------*
* Appending records to the internal table
*----------------------------------------------------------------------*
MODULE zmatnr INPUT.
  MODIFY i_makt INDEX t_ctrl-current_line.
  IF t_ctrl-current_line GT ln.
    READ TABLE i_makt WITH KEY zmatnr = i_makt-zmatnr.
    IF sy-subrc NE 0.
* Inserting record if it does not exist in database
      APPEND i_makt.
    ELSE.
     MESSAGE i005 WITH 'Material Number' i_makt-zmatnr 'already exists'.
    ENDIF.
  ENDIF.
ENDMODULE.                 " zmatnr  INPUT

*&---------------------------------------------------------------------*
*&      Module  set_status  OUTPUT
*&---------------------------------------------------------------------*
* Setting the GUI status
*----------------------------------------------------------------------*
MODULE set_status OUTPUT.
  SET PF-STATUS 'ZSTATUS'.
  SET TITLEBAR  'ZTITLE'.
ENDMODULE.                 " set_status  OUTPUT

*&---------------------------------------------------------------------
*&      Module  CHECK  INPUT
*&---------------------------------------------------------------------
* Modify the internal table using the current line in table control
*----------------------------------------------------------------------

MODULE check INPUT.
  MODIFY i_makt INDEX t_ctrl-current_line.
ENDMODULE.                 " CHECK  INPUT