Monday, 6 January 2014

Key Fields in SAP Tables

Key Fields in SAP Tables

If you want to add or remove fields which are designated key fields, then it is important to take into consideration what will be going on in the database itself. All of the new ele-ments which have been created for this table have their features applied by the system to the ABAP dictionary, not the underlying database. When any key field is adjusted, the sys-tem has to apply changes to the underlying database itself. If there is data in the table, and key fields are changed, this can have unintended consequences.
If you introduce a new key field, this will probably not have a large effect. However, if one makes a key field no longer a key field, this will require consideration, because if there is a lot of data in the underlying database, by taking away a key field, duplicate records could be introduced. Corrupt data or records being deleted from the table can also happen here.
Let’s see how we can add, remove and alter fields without these hazards.
Open the full ZEMPLOYEES2 table in the ABAP Dictionary ‘Maintain Table’ screen. Let’s change the ‘Surname’ field by turning it into a key field.
Check the two boxes (key and Index) by ‘SURNAME’ and Activate the table. When you now view the table contents, the surname column will be a darker colour, indicating that it is now a key field. Beyond this though, it appears very little has changed
                                
Key Fields in SAP Tables

Key Fields in SAP Tables

Now, uncheck the boxes on the ‘Maintain Table’ screen, to make it no longer a key field. When you try to activate the table an error message appears, refusing to activate the ta-ble as data may be lost with the removal of a key field
                     
Key Fields in SAP Tables

To activate the table against what seem to be the wishes of the system (after all, one knows the data will be fine as the surname field has not been operating as a key field at any point previously), a different transaction must be used.
From the ‘Utilities’ menu, select ‘Database utility’, or use transaction code SE14. A new screen will appear.
       
Key Fields in SAP Tables

This transaction lets us automatically adjust the data held in our table when making ad-justments to the database table structure. Environments where tables are being worked on may contain a huge number of records. With this in mind, this transaction can be exe-cuted as a background process. However, for our example the ‘Direct’ option is the option
to choose because we know we have very few records in our database table. Select this, and then click ‘Activate and adjust database’ with ‘Save data’ radio button selected. Say ‘Yes’ when the box asks “Request: ‘Adjust’” and notice the status bar should indicate the success of this execution. Then, step back to the ‘Maintain Table’ screen and you will see the table should be Active with the surname field no longer key.

To insert a new field as part of the table key, you must be able to adjust the location of fields on the screen. For example, if you wanted to create a new field above the surname field, you would highlight the row and then click the ‘Insert row’ icon in the toolbar. This toolbar also includes ‘Cut’, ‘Copy’ and ‘Paste’ options, allowing for rows to be moved up and down if there is a need to do this
Key Fields in SAP Tables

No comments:

Post a Comment