Handling Changes to Database Tables

Objectives

After completing this lesson, you will be able to:

  • Describe the adjustment of database tables.
  • Adjust direct changes to database tables.
  • Describe the conversion process for database tables.
  • Adjust indirect changes to database tables.

Adjustment of Database Tables

If a change in the dictionary affects a database table, directly or indirectly, this change needs to be applied to the database table. This is called the database table adjustment.

There are three different approaches for adjusting the database table:

ALTER TABLE

Using this SQL statement, the dictionary tool can change (alter) the table layout on the database. Data in the table gets adjusted if necessary. However, not every change can be implemented using ALTER TABLE. In particular, changes that imply the risk of data losses or conversion errors are not implemented with ALTER TABLE.

Delete and Create

This is the simplest approach. The database object gets deleted and created again based on the changed definition. This is of course only a good idea if the table is empty, or if the data stored in the table is redundant.

Table Conversion

In this procedure, the table is first created on the database under a new name. Then the data is copied from the old table to the new table, converting it if necessary. Finally, the old table is deleted and the new table is renamed to replace the old table.

During a table conversion, a partial loss of data is possible, for example, when you remove a table field or reduce its length. Runtime errors can occur if you change the primary key or the type of an existing table field.

Activating Changes - Decision Tree

Play the video to learn more about activating changes.

How to Perform and Analyze Direct Changes

Play the video to see how to perform and analyze direct changes to a database table.

Converting a Database Table

When it is not possible to implement your changes on the database during the activation, the editor displays error message "TABLE <...> was not activated".

To see more details, right-click anywhere on the source code and choose Open WithDictionary Log .

The Dictionary Log view then tells you that ALTER TABLE is not possible, and displays error message Structure change at field level (convert table <...> ).

You perform the manual conversion using a quick fix. In the editor, right-click on the table name and choose Quick fix. Alternatively, place the cursor on the table name and press Ctrl + 1. There are two quick fixes to adjust and activate a database table:

Adjust and activate database table, convert data

Choose this quick fix if you want to keep and convert your data. In most situations, this will be the right option.

Adjust and activate database table, delete data

Choose this quick fix if the data in the table are not needed anymore.

Avoid using quick fix Delete table on database. This removes the table from the database. After this, any access to the table will fail until you activate the database table in the dictionary again.

Note

Having an activation error is not a prerequisite for using these quick fixes. In principle, you can adjust or delete a table on the database at any time as long as you have sufficient authorizations.

When you choose Adjust an activate database table, convert data, the Quick Assist view will automatically open below the editor window. In the section on the left, you can trigger the same, or another quick fix again. The section on the right allows you to analyze the last adjustment in more detail. Here you find the start time of the last adjustment, and a link to the detailed DB Adjusting Log.

The DB Adjusting Log contains detailed information on how the table was adjusted. It is particularly useful when something goes wrong during the conversion process.

Before we discuss in a bit more detail what exactly happens during a table conversion, watch the following video to see the adjustment of a database table.

How to Convert a Database Table

Play the video to see how to convert a database table.

The Conversion Process (Step by Step)

Play the video to learn about the conversion process.

Adjusting Indirect Changes to Database Tables

Earlier, we discussed how the database table needs to be adjusted after a direct change of the table definition in the ABAP dictionary. Now we want to have a look at indirect changes.

One example for such an indirect change is a change of the basic type of a domain, which describes a field of the database table. Another example is the replacement of the used domain in a data element. If the table includes a structure, a change of the include structure also affects the table on the database.

When you activate a dictionary object, the tool adjusts all dependent objects as well. However, if a database table is affected, the tool stops the activation with error message Domain <...> cannot be activated due to dependent tables. This error is independent from the question, whether it is possible to adjust the affected database tables directly or whether a manual conversion is required.

In the example, domain ZS4D430_DEPARMENT_ID is used indirectly in table definition ZS4D430_EMPLOYEE. After a length change from 20 to 40, the activation fails with the displayed error message, even though the length extension of a field can be adjusted using ALTER TABLE.

The error message comes with a quick fix. To invoke the quick fix, right-click the error message on the Problems view. Alternatively, left-click the error message an press Ctrl + 1. On the quick fix window that opens, choose Finish.

The outcome of the quick fix is displayed on the Quick Assist view. The activation is successful if it was possible to adjust all dependent tables using either ALTER TABLE or Delete and Create.

The activation fails if a least one of the dependent tables needs manual adjustment. Check the Activation log to identify the affected database tables and adjust them manually as we did before.

Hint

After adjusting all dependent tables, return to the dictionary object that you changed directly, and make sure that it is in status Active. In our example, this is domain ZS4D430_DEPARTMENT_ID.

How to Activate an Indirect Change

Play the video to see how to activate an indirect change in a database table.

Handle Changes to a Database Table

You need to make changes to the field list of a database table that already contains data. Depending on the nature of the change, the system request you to adjust the table.

Template:

  • /LRN/EMPLOY (Database Table Definition)

Solution:

  • /LRN/EMPLOY_DEP (Database Table Definition)
  • /LRN/DEPARTMENT_ID (Data Element)
  • /LRN/DEPARTMENT_ID (Domain)

Task 1: Copy Template (Optional)

Copy the template database table definition. If you finished the previous exercise, you can skip this task and continue editing your database table Z##EMPLOY.

Steps

  1. Copy database table definition /LRN/EMPLOY to your own package (suggested name: Z##EMPLOY, where ## stands for your group number).

    1. In the Project Explorer view, right-click database table definition /LRN/EMPLOY to open the context menu.

    2. From the context menu, choose Duplicate ....

    3. Enter the name of your package in the Package field. In the Name field, enter the name Z##EMPLOY, where ## stands for your group number.

    4. Confirm the transport request and choose Finish.

  2. Activate the copy.

    1. Press Ctrl + F3 to activate the database table definition.

Task 2: Handle Direct Changes

In the field list of your database table, add a new field (suggested name: DEPARTMENT_ID) and type it with built in type CHAR and length 40. After activating the table with the new field, reduce the length of the field to 20 and adjust the table on the database.

Steps

  1. Before theANNUAL_SALARY field, add a new field (suggested name: DEPARTMENT_ID) and type it with built in type CHAR and length 40.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567
      birth_date : z##birth_date; entry_date : z##entry_date; @Semantics.amount.currencyCode : 'z##employ.currency_code' annual_salary : z##annual_salary; currency_code : waers;
  2. Activate the database table definition.

    1. Press Ctrl + F3 to activate the development object.

  3. Display the Dictionary Log for the table.

    1. Right-click anywhere in the source code and choose Open WithDictionary Log.

    2. Navigate to the Dictionary Log tab that appears below the editor.

    3. Hold down the Ctrl key and click on the row starting with See log.

    4. From the toolbar on the Dictionary Log tab, choose Show Search Toolbar.

    5. Enter the name of your database table in the Find: field and press Enter.

    6. The update strategy is mentioned after the row Adjust table in DB >>>.

  4. Reduce the length of the new field from 40 to 20.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567
      birth_date : z##birth_date; entry_date : z##entry_date; @Semantics.amount.currencyCode : 'z##employ.currency_code' annual_salary : z##annual_salary; currency_code : waers;
  5. Activate the database table definition.

    1. Press Ctrl + F3 to activate the development object.

  6. Analyze the ABAP log.

    1. On the dialog window with the error message, choose Open ABAP Log to display the Dictionary Log.

    2. On the Dictionary Log tab, hold down the Ctrl key and click on the row starting with See log.

    3. From the toolbar on the Dictionary Log tab, choose Show Search Toolbar.

    4. Enter the name of your database table in the Find: field and press Enter.

    5. The update strategy is mentioned after the row Adjust table in DB >>>.

  7. Use the quick fix to convert the content of your database table.

    1. Navigate to the source code of your database table definition.

    2. In the code row that starts with define table, place the cursor on the table name and press Ctrl + 1 to invoke the quick fix.

    3. From the list of available quick fixes, choose Adjust and activate database table, convert data.

Task 3: Handle Indirect Changes

Replace the hard-coded type with a new data element (suggested name: Z##_DEPARTMENT_ID, where ## is your group number) which uses a new domain ((suggested name: Z##_DEPARTMENT_ID). In the domain, use technical type CHAR with a length of 20. After activating the database table, change the length in the domain to 10. Then activate and adjust the database table.

Steps

  1. In your database table definition, replace abap.char(20) with Z##_DEPARTMENT_ID and use a quick fix to create the new data element.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678
      birth_date : z##_birth_date; entry_date : z##_entry_date; department_id : z##_department_id; @Semantics.amount.currencyCode : 'z##employ_dep.currency_code' annual_salary : z##_annual_salary; currency_code : waers;
    2. Click on z##_department_id, and press Ctrl + 1 to open the quick fix menu.

    3. From the quick fix menu, double-click Create data element ....

    4. Confirm the package and the name, enter a description, for example, Department Identification, and choose Next.

    5. Assign the new object to a transport request and choose Finish.

    6. Under Type Name, enter Z##_DEPARTMENT_ID and enter the following field labels:

      FieldValue
      ShortDep.
      MediumDepartment
      LongDepartment ID
      HeadingDepartment ID
  2. Save the data element. Then use forward navigation to create the domain with technical type CHAR and a length of 20.

    1. Press Ctrl + S to save the data element.

    2. Choose the Type Name: label to start creating the domain.

    3. Confirm the package and the name, enter a description, for example, Department Identification, and choose Next.

    4. Assign the new object to a transport request and choose Finish.

    5. Enter CHAR as Data Type and 20 as Length.

  3. Save the domain. Then activate the domain, the data element, and the database table definition.

    1. Press Ctrl + S to save the domain.

    2. Choose Activate inactive ABAP development objects. Alternatively, press Ctrl + Shift + F3.

    3. Choose Select All and then Activate.

  4. In your domain for the department ID, reduce the technical length and the display length from 20 to 10.

    1. Navigate to your domain Z##_DEPARTMENT_ID.

    2. Change the value in the Length field from 20 to 10.

    3. Remove the value in the Output Length field (this will be filled automatically during activation).

  5. Try to activate the domain and analyze the messages on the Problems view.

    1. Press Ctrl + F3 to activate the domain.

    2. Open the Problems view below the editor to display the error messages.

  6. Use the quick fix for the error message Domain ... cannot be activated due to dependent tables.

    1. In the Problems view, place the cursor on the error message that starts with Domain ... and press Ctrl + 1 to invoke the quick fix.

    2. Choose Finish.

    3. From the Previous Result section on the lower right, choose Activation Log to analyze the activation log.

  7. Use a quick fix to adjust and convert the dependent database table, that is, your database table Z##EMPLOY.

    1. Navigate to the source code of your database table definition.

    2. In the code row that starts with define table, place the cursor on the table name and press Ctrl + 1 to invoke the quick fix.

    3. From the list of available quick fixes, choose Adjust and activate database table, convert data.

  8. Activate the domain for the department ID.

    1. Navigate to the definition of your domain for the department ID.

    2. Press Ctrl + F3 to activate the development object.

Log in to track your progress & complete quizzes