Skip to Content
Author's profile photo Vineet Lakhera

Reading and Writing Data in Cluster table

Cluster database tables are special relational databases, defined in ABAP that have a special use in ABAP.

In this tutorial, we will see as how to create a custom Cluster Table and then will try to insert some data into it and then retrieving the same using standard program statements already provided by SAP.

We use different Open SQL statements (not SELECT or INSERT) to read and write the contents in the cluster table using EXPORT TO DATABASE and IMPORT FROM DATABASE.

PCL2 Cluster database tables have the following structure:

Field Data Element Data Type Length Description
CLIENT MANDT CLNT 0003 Client
RELID RELID_PCL2 CHAR 0002 PCL2 RELID (Area Id)
SRTFD PCLKEY CHAR 0040 Data cluster Key used for record identification
SRTF2 PCLSRTFD INT4 0010 Sort Field for the duplicate key field
HISTO HISTO CHAR 0001 Historical Record
AEDTM AEDAT DATS 0008 Changed On
UNAME UNAME CHAR 0012 User Name
PGMID OLD_PROG CHAR 0012 Program Name
VERSN PVRSN CHAR 0002 Version
CLUSTR PCLCLUSTR INT2 0005 Data Length
CLUSTD PCLDATA LRAW 3900 Data Cluster

The first four fields are the key fields and uniquely identifies a record.

Description of some Important fields:

1. RELID: This is important field and determines the area of the cluster where data needs to inserted/changed. The system fills the field RELID with the area ID specified in the EXPORT statement (which is used to save record in a data cluster).

2. SRTFD: This field is normally a combination of fields that in combination will be used to determine the content of the cluster. Along with the next field, it forms the complete key of the table.

3. SRTF2: Sometime due to huge data value for a particular record(data cluster), SRTFD field alone is not able to uniquely identify a record, and in that scenario SRTF2 field along with SRTFD field combined makes up the primary key of the table. This field contains the current line number within the data cluster and is filled automatically by the system when you save a data cluster.

4. CLUSTR: This field contains the length of the data in the next field CLUSTD. This field is filled automatically by system when you save data cluster.

5. CLUSTD: This is the most important field and is used to contain a the actual data of the data cluster. The data is stored in the compressed format and therefore to read the data, specific IMPORT statements are used. If the CLUSTD length is not enough to accommodate the complete data cluster record, than the field SRTF2 comes into picture and splits the record increasing the counter of itself.

After SRTF2, you can include any number of user data fields of any name and type. The system does not automatically fill these fields when you save a cluster.  Instead, you must assign values to them explicitly in the program before the EXPORT statement. These fields normally contain administrative information such as program name and user ID.

Saving Data Objects in Cluster Databases:

To save data objects from an ABAP program in a cluster database, use the following statement:

EXPORT <f1> [FROM <g 1>] <f 2> [FROM <g 2>] … 
     TO DATABASE <dbtab>(<ar>) [CLIENT <cli>] ID <key>.

This statement stores the data objects specified in the list as a cluster in the cluster database <dbtab>. You must declare <dbtab> using a TABLES statement. If you do not use the option FROM <fi>, the data object <fi> is saved under its own name. If you use the FROM <gi> option, the data objet <gi> is saved under the name <fi>.

<ar> is the two-character area ID for the cluster in the database

The name <key> identifies the data in the database. Its maximum length depends on the length of the SRTFD field in <dbtab>.

The CLIENT <cli> option allows you to disable the automatic client handling of a client-specific cluster database, and specify the client yourself.  The addition must always come directly after the name of the database.

The EXPORT statement also transports the contents of the user fields of the table work area <dbtab> into the database table.  You can fill these fields yourself beforehand [fields AEDTM, UNAME, PGMID].

The EXPORT statement always completely overwrites the contents of any existing data cluster in the same area <ar> with the same name <key> in the same client <cli>, so to update any particular record for which key is known, we will use EXPORT statement itself.

Reading Data Objects From Cluster Databases:

To read data objects from an ABAP cluster database into an ABAP program, use the following statement:

IMPORT <f1> [TO <g 1>] <f 2> [TO <g 2>] … 
     FROM DATABASE <dbtab>(<ar>) [CLIENT <cli>] ID <key>.

This statement reads the data objects specified in the list from a cluster in the database <dbtab>.  You must declare <dbtab> using a TABLES statement. If you do not use the TO <gi> option, the data object <fi> in the database is assigned to the data object in the program with the same name.  If you do use the option, the data object <fi> is read from the database into the field <gi>.

The IMPORT statement also reads the contents of the user fields from the database table fields [AEDTM, UNAME, PGMID].

Deleting Data Clusters from Cluster Databases:

To delete data objects from a cluster database, use the following statement:

DELETE FROM DATABASE <dbtab>(<ar>) [CLIENT <cli>] ID <key>.

You can refer ABAP Programming (BC-ABA) – SAP Library for more details on the above.

Enough theory..

Now we will create a custom cluster table and will try to INSERT/UPDATE a data cluster into same as well as try to perform DELETE operation and see how this all works.

1. First, we will create a cluster table that will hold our data clusters, I have created our custom table with same field and technical properties as PCL4                 cluster table.

1.JPG

2. Also, created a dictionary structure which will form the data cluster fields.

struct.JPG

3.  Now our aim will be to store some data in this cluster table (insert), change the same and then delete to see how all operations work for the cluster table.

    To fill the data for our above structure, we used logical database programming and used macro to fill the internal table with all system data of infotype 0002.

it_data.JPG

    Now, our internal table T_CLUSTER_DATA contains the required field of infotype 0002 which forms our cluster data to be stored. Below code                         does the same for us.

populate_cluster.JPG

    On executing the below code, the entries are created successfully in our cluster tale.

101.JPG

    As can be seen that the data in the last column is stored in a compressed hexadecimal format and cannot be read manually, also since a single key                 is holding large amount of data which extends to multiple rows, therefore field ‘SRTF2’ is populated to form a unique key for the table.

4. Now, we will get the data in readable format using EXPORT statement and then use OOPS ALV to display the same.

read_cluster.JPG

    OOPS ALV content:

102.JPG

5. Now, we will delete the entries that is created earlier.

delete.JPG

I have attached the complete code snippet which will first add the entries in cluster table, display it and then finally delete it. For testing purpose keep a debugger point in each of the routine and see the table entries created and deleted.

PS: I have hard-coded the key value (in screen-shots) taken from the cluster table for simplification. The same can be obtained using a Select statement.

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Namrata Somani
      Namrata Somani

      there is error in this report error is :  no  NODES: peras

      Author's profile photo Vineet Lakhera
      Vineet Lakhera
      Blog Post Author

      Please use logical database 'PNPCE' in report attribute section.

      Author's profile photo Arushi Singhal
      Arushi Singhal

      Vineet Lakhera I have a requirement to build a CDS Report on HR Infotype 2001 (Absences). I am fetching most of my data from table PA2001 except the Infotype Text stored in PCL1-CLUSTD as it in LRAW format.

      Is there a way to get the value stored in field CLUSTD of table PCL1 in an ABAP CDS view in readable format?

      I have tried the format conversion using CAST but it doesn't work for LRAW to CHAR. Is there any other way to do achieve this in ABAP CDS views?