How to update Data records in a Database table in SAP ABAP

Using the ‘UPDATE’ keyword, we can modify one or more Data records in an internal table.

The data records can be modified in the following different ways.

  1. Through Structure.
  2. Through Internal table.
  3. Through ‚WHERE‘ Clause.
  • Through Structure:

Single line of record can be modified using structure. Therefore define a structure with reference to the Database table name. The line of record must correspond to the primary key of the structure. Structure name is given after the ‘FROM’ clause. If the data records are updated successfully, then the system field sy-subrc is set to 0. If the data records are not updated successfully, then the system field sy-subrc is set to 4.

Now let’s look at the syntax.

DATA ls_contact TYPE ztt_db_table2.

ls_contact-contact_id = 102.

ls_contact-contact_name = ‘Sai’.

UPDATE ztt_db_table2 FROM ls_contact.

If all the components of the structure are not assigned with values, the initial values will be set in Database table for those components.

  • Through Internal table.

If more than one line of record must be updated, then we can use an Internal table to achieve that. The structure of the Internal table must be similar to the Database table. All lines of records will be updated which have the same primary key values. The lines of record which are not found will not be updated. At the same time, it will not influence the rest of the data records. If all the lines of record are updated successfully, then the system field sy-subrc is set to 0. If atleast one line of record is not updated successfully, then the system field sy-subrc is set to 4. The system field sy-dbcnt will print out the number of records updated successfully.

UPDATE ztt_db_table2 FROM TABLE lt_int_table.

  • Through ‘WHERE’ clause

Through this option we can change more number of records targeted at a particular column. The ‘WHERE’ clause can further be added, to constrain the updated records. Through the addition of the keyword ‘SET’, we can specify which columns of the Database table should be changed.

If atleast one of the data record is updated successfully, then the system field sy-subrc is set to 0. If none of the data records is updated successfully, then the system field is set to sy-subrc = 4. The system field sy-dbcnt prints out the number of Data records which are updated successfully.

The following statement is the syntax for ‘WHERE’ clause.

UPDATE ztt_db_table2 SET contact_address = ‘Street 10’ WHERE contact_name = ‘Mike’.

The execution of above query will set the contact_address to ‘Street 10’ for all the contacts with the contact_name = ‘Mike’.