Skip to Content

Create Table Persistence and Generate Data

test
0 %
Create Table Persistence and Generate Data
Details

Create Table Persistence and Generate Data

August 24, 2020
Created by
September 6, 2019
Create table persistence and generate data for it.

You will learn

  • How to create an ABAP package
  • How to create a database table
  • How to create an ABAP class to generate data

Prerequisites

  • You have created an SAP Cloud Platform ABAP environment trial user or
  • You have created a developer user in an SAP Cloud Platform ABAP Environment system.
  • You have downloaded Eclipse Photon or Oxygen and installed ABAP Development Tools (ADT). See https://tools.hana.ondemand.com/#abap.

In this tutorial, wherever XXX appears, use a number (e.g. 000).


Step 1: Create ABAP package
  1. Open ABAP Development Tools (ADT) and select your ABAP Cloud Project you created in Create an SAP Cloud Platform ABAP Environment Trial User.

    Right-click on ZLOCAL and select New > ABAP Package.

    Create ABAP package

  2. Create a new ABAP package:

    • Name: ZTRAVEL_APP_XXX
    • Description: Package for travel XXX

    Use your number instead of XXX.

    Click Next >.

    Create ABAP package
  3. Select Create new request and enter a request description.

    Create ABAP package

  4. Right-click on Favorite Packages and select Add Package.

    Create ABAP package

  5. Search for ZTRAVEL_APP_XXX, select it and click OK to add your package in the favorite packages section.

    Create ABAP package

Log on to answer question
Step 2: Create database table
  1. Right-click on your package ZTRAVEL_APP_XXX, select New > Other ABAP Repository Object.

    Create database table

  2. Search for database table, select it and click Next >.

    Create database table

  3. Create a new database table:

    • Name: ZTRAVEL_XXX
    • Description: Database table for travel data XXX

    Click Next >.

    Create database table

  4. Click Finish to create your transport request.

    Create database table

  5. Replace your code with following:

    @EndUserText.label : 'Database table for travel data XXX'
    @AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE
    @AbapCatalog.tableCategory : #TRANSPARENT
    @AbapCatalog.deliveryClass : #A
    @AbapCatalog.dataMaintenance : #RESTRICTED
    define table ztravel_xxx {
      key client      : abap.clnt not null;
      key mykey       : sysuuid_x16 not null;
      travel_id       : /dmo/travel_id;
      agency_id       : /dmo/agency_id;
      customer_id     : /dmo/customer_id;
      begin_date      : /dmo/begin_date;
      end_date        : /dmo/end_date;
      @Semantics.amount.currencyCode : 'ztravel_xxx.currency_code'
      booking_fee     : /dmo/booking_fee;
      @Semantics.amount.currencyCode : 'ztravel_xxx.currency_code'
      total_price     : /dmo/total_price;
      currency_code   : /dmo/currency_code;
      description     : /dmo/description;
      overall_status  : /dmo/overall_status;
      created_by      : syuname;
      created_at      : timestampl;
      last_changed_by : syuname;
      last_changed_at : timestampl;
    
    }   
    
  6. Save and activate.

    save and activate

    Now the dictionary tables are defined. The dictionary tables are the basis of our travel booking data model.

Log on to answer question
Step 3: Create ABAP class
  1. Right-click on your package ZTRAVEL_APP_XXX, select New > ABAP Class.

    Create ABAP class

  2. Create a new ABAP class:

    • Name: ZCL_GENERATE_TRAVEL_DATA_XXX
    • Description: Class for generating travel data

    Click Next >.

    Create ABAP class

  3. Click Finish to create your transport request.

    Create ABAP class

  4. Replace your code with following:

    CLASS zcl_generate_travel_data_xxx DEFINITION
      PUBLIC
      FINAL
      CREATE PUBLIC .
    
      PUBLIC SECTION.
        INTERFACES if_oo_adt_classrun.
      PROTECTED SECTION.
      PRIVATE SECTION.
    ENDCLASS.
    
    
    
    CLASS zcl_generate_travel_data_xxx IMPLEMENTATION.
      METHOD if_oo_adt_classrun~main.
        DATA:itab TYPE TABLE OF ztravel_xxx.
    
    *   read current timestamp
        GET TIME STAMP FIELD DATA(zv_tsl).
    
    *   fill internal travel table (itab)
        itab = VALUE #(
      ( mykey = '02D5290E594C1EDA93815057FD946624' travel_id = '00000022' agency_id = '070001' customer_id = '000077' begin_date = '20190624' end_date = '20190628' booking_fee = '60.00' total_price =  '750.00' currency_code = 'USD'
        description = 'mv' overall_status = 'A' created_by = 'MUSTERMANN' created_at = '20190612133945.5960060' last_changed_by = 'MUSTERFRAU' last_changed_at = '20190702105400.3647680'  )
      ( mykey = '02D5290E594C1EDA93815C50CD7AE62A' travel_id = '00000106' agency_id = '070005' customer_id = '000005' begin_date = '20190613' end_date = '20190716' booking_fee = '17.00' total_price = '650.00' currency_code = 'AFN'
        description = 'Enter your comments here' overall_status = 'A' created_by = 'MUSTERMANN' created_at = '20190613111129.2391370' last_changed_by = 'MUSTERMANN' last_changed_at = '20190711140753.1472620' )
      ( mykey = '02D5290E594C1EDA93858EED2DA2EB0B' travel_id = '00000103' agency_id = '070010' customer_id = '000011' begin_date = '20190610' end_date = '20190714' booking_fee = '17.00' total_price = '800.00' currency_code = 'AFN'
        description = 'Enter your comments here' overall_status = 'X' created_by = 'MUSTERFRAU' created_at = '20190613105654.4296640' last_changed_by = 'MUSTERFRAU' last_changed_at = '20190613111041.2251330' )
      ).
    
    *   delete existing entries in the database table
        DELETE FROM ztravel_xxx.
    
    *   insert the new table entries
        INSERT ztravel_xxx FROM TABLE @itab.
    
    *   check the result
        SELECT * FROM ztravel_xxx INTO TABLE @itab.
        out->write( sy-dbcnt ).
        out->write( 'Travel data inserted successfully!').
    
      ENDMETHOD.
    ENDCLASS.
    
  5. Save, activate and click F9 to run your ABAP class.

    save and activate

  6. Check your result. Therefore open your database table ZTRAVEL_XXX and press F8 to see your data.
    Now the dictionary tables are filled with data.

    Create ABAP class

Log on to answer question
Step 4: Test yourself
Where can you find your database table?
×

Give us 55 seconds of your time to help us improve

Next Steps

Back to top