Skip to main content

ABAP Data Dictionary : Writing Functions and Views

Difference between Function and Program : 

  1. ABAP Program:

  2. An ABAP program is an executable unit. It is a collection of statements that fulfill a specific task or a set of tasks. These tasks can be data manipulations, data calculations, or any business logic. There are different types of ABAP programs such as reports, module pools, includes, function groups, and more. Reports are the most common type of ABAP programs.

    Each ABAP program has a type associated with it which determines how and where the program can be executed. For example, an executable program (type 1) can be directly executed in the ABAP runtime environment using a transaction code or via the ABAP Editor.


  3. ABAP Function (Module):

    A function, also known as a function module, is a subroutine that is part of a function group and can be called from various places in a program. Functions are not standalone executables but are pieces of code designed to perform a particular task, like a procedure or method in other programming languages.

    Function modules can have importing parameters (input), exporting parameters (output), changing parameters (input/output), and tables (internal table parameters). Functions can be called from ABAP programs or other function modules, and they can also be remotely invoked from external systems if they are defined as remote-enabled.

In essence, a program is an independent entity that can be executed directly, whereas a function is a piece of reusable code designed to perform a specific task that needs to be called from within a program or another function.



Writing a ABAP Function and calling it in a program :



1. Open the Function Builder:

    Transaction code: `SE37`
    
    Go to transaction `SE37` in the SAP GUI.

2. Create a new Function Module:

    In SE37, navigate to 'Function Module' -> 'Create'. You'll be prompted to enter a name for the function module. Function module names typically start with `Z` or `Y` to indicate that they're custom objects, so let's name it `ZSQUARE_NUMBER`.

    Next, you'll be asked to enter a function group. Function groups are containers for function modules that are logically related. If you don't have a suitable function group already, you can create a new one by going to 'Goto' -> 'Function Groups' -> 'Create'. In this step, you can use transaction code `SE80`.

3. Define the function module:

    In the next screen, you can enter a short description of the function module (e.g., 'Squares a number').

    You'll also need to define the function module's parameters. For this example, we'll add an input parameter `NUMBER` (type `I`, or integer) and an output parameter `SQUARED` (also type `I`).

    Under the 'Import' tab, add the `NUMBER` parameter with the type `I`. Under the 'Export' tab, add the `SQUARED` parameter with the type `I`.

4. Write the ABAP code for the function module:

    Click on the 'Source Code' tab. In the source code editor, enter the following code:

    ```ABAP
    SQUARED = NUMBER * NUMBER.
    ```

5. Save and activate the function module:

    Click the 'Save' and 'Activate' buttons. Your function module is now ready to use!

To use the function in your ABAP program:

Transaction code: `SE38` or `SE80`

    Open the ABAP editor with transaction `SE38` or `SE80`, create a new program or use an existing        one, and write the following code to call your function:

```ABAP
DATA: lv_number TYPE I VALUE 5,
      lv_squared TYPE I.

CALL FUNCTION 'ZSQUARE_NUMBER'
  EXPORTING
    NUMBER = lv_number
  IMPORTING
    SQUARED = lv_squared.

WRITE: / 'The square of', lv_number, 'is', lv_squared.
```
Then save and run your program to test the function.

Writing a ABAP View 

In ABAP, you can create a database view that combines several database tables into one view. Let's take an example of creating a view that combines fields from two tables.

For our example, let's consider we have two tables: SFLIGHT (Flight data) and SCARR (Airline companies). We want to create a view to display the AirlineID, Flight number, and Flight date from the SFLIGHT table along with the Airline's name from the SCARR table.

Follow these steps to create a database view:

1. Open the ABAP Dictionary:

    Use transaction code `SE11`.

2. Create a new database view:

    Choose the 'View' radio button and enter a name for the view, following your organization's naming conventions. For example, `ZVIEW_FLIGHTS`.

3. Define the view:

    On the View Maintenance screen, choose 'Database View' as the View type. In the Table/Join conditions section, add `SFLIGHT` and `SCARR` tables. Set the Join condition as `SFLIGHT-CARRID = SCARR-CARRID`.

    In the View fields section, choose the fields you want in the view: `SFLIGHT-CARRID`, `SFLIGHT-FLIGHTNUM`, `SFLIGHT-FLDATE`, and `SCARR-CARRNAME`.

    Your view should look like this:
    ```
    View: ZVIEW_FLIGHTS
    View type: Database View
    Table/Join conditions:
    SFLIGHT
    SCARR
    SFLIGHT-CARRID = SCARR-CARRID
    View fields:
    SFLIGHT-CARRID
    SFLIGHT-FLIGHTNUM
    SFLIGHT-FLDATE
    SCARR-CARRNAME
    ```

4. Save and activate the view:

    Click on the 'Save' and 'Activate' buttons.

Now you have a database view `ZVIEW_FLIGHTS` that combines information from the `SFLIGHT` and `SCARR` tables. You can use this view in your ABAP programs just like a database table. For example, you can use the SELECT statement to retrieve data from this view.

```ABAP
DATA: lt_flights TYPE TABLE OF ZVIEW_FLIGHTS.
SELECT * FROM ZVIEW_FLIGHTS INTO TABLE lt_flights.
LOOP AT lt_flights ASSIGNING FIELD-SYMBOL(<ls_flight>).
  WRITE: / <ls_flight>-carrid, <ls_flight>-flightnum, <ls_flight>-fldate, <ls_flight>-carrname.
ENDLOOP.
```

Comments

Post a Comment

You might find these interesting

8 Must-Know Questions About Object Store on SAP Business Technology Platform

What is the problem that Object Store solves ? Modern enterprise systems increasingly deal with massive volumes of unstructured data such as documents, logs, media files, and backups. Traditional relational databases are not optimized for such workloads. What is Object Store ? Object storage—commonly referred to as blob storage—addresses this gap by providing scalable, durable, and cost-efficient storage for unstructured data. Object storage is a storage architecture designed to manage unstructured data as discrete units called objects.  Each object consists of: Binary data (file content) : Image , File etc Metadata (descriptive attributes) : File size, Content type, Last modified timestamp, Storage class (hot, cool, archive) Unique identifier (key or URL) : unique path-like string used to locate a blob inside a bucket Unlike file systems or relational databases, object storage does not rely on hierarchical file structures or schemas. The SAP BTP Object Store service is a managed, ...