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

How to properly Start/Stop SAP system through command line ?

Starting/stopping an SAP system is not a critical task, but the method that most of us follow to achieve this is sometimes wrong. A common mistake that most of the SAP admins do is, making use of the 'startsap' and 'stopsap' commands for starting/stopping the system.  These commands got deprecated in 2015 because the scripts were not being maintained anymore and SAP recommends not to use them as many people have faced errors while executing those scripts. For more info and the bugs in scripts, you can check the sap note 809477.  These scripts are not available in kernel version 7.73 and later. So if these are not the correct commands, then how to start/stop the sap system?  In this post, we will see how to do it in the correct way. SAP SYSTEM VS INSTANCE In SAP, an instance is a group of resources such as memory, work processes and so on, usually in support of a single application server or database server with

sapstartsrv is not started or sapcontrol is not working

 What is sapstartsrv ? The SAP start service runs on every computer where an instance of an SAP system is started. It is implemented as a service on Windows, and as a daemon on UNIX. The process is called  sapstartsrv.exe   on Windows, and   sapstartsrv   on UNIX platforms. The SAP start service provides the following functions for monitoring SAP systems, instances, and processes. Starting and stopping Monitoring the runtime state Reading logs, traces, and configuration files Technical information, such as network ports, active sessions, thread lists, etc. These services are provided on SAPControl SOAP Web Service, and used by SAP monitoring tools (SAP Management Console,  SAP NetWeaver  Administrator, etc.). For more understanding use this link : https://help.sap.com/doc/saphelp_nw73ehp1/7.31.19/enUS/b3/903925c34a45e28a2861b59c3c5623/content.htm?no_cache=true How to check if it is working or not ? In case of linux , you can simply ps -ef | grep sapstartsrv In case of windows, you need

HANA System Replication - Prerequisites & Setup

Hey Folks! Welcome back to Hana high availability blog series. In our last blog we checked out operation & replication modes in hana system replication. If you haven't gone though that blog, you can checkout  this link In this blog we will be talking about the prerequisites of hana replication and it's setup. So let's get started. When we plan to setup hana system replication, we need to make sure that all prerequisite steps have been followed. Let's have a look at these prerequisites. HANA System Replication Prerequisites: Primary & secondary systems should be up & running HDB version of secondary should be greater than or equal to Primary database sever But, for Active/Active(read enabled config), HDB version should be same on both sites. System configuration/ini files should be identical on both sides Replication happe

ST03N : The chapter for all BASIS Admins

This blog is targeted to BASIS ADMINS Transaction for workload analysis statistical data changed over time are monitored using transaction code ST03 , now ST03N (from SAP R/3 4.6C) . With SAP Web AS 6.4 the transaction ST03 is available again. From time to time ST03 and ST03N has seen many changes but later in SAP NW7.0 ST03N has reworked in detail specially processing time is now shown in separate column. Main Use of ST03N  is to get detailed information on performance of any ABAP based SAP system. Workload monitor analyzes the statistical data originally collected by kernel. You can compare or analyze the performance of a single application server or multiple application server. Using this you start checking from the entire system and finding your way to that one application server and narrowing down to exact issue. By Default :- You see data of current day as default view , you can change the default view. Source of the image : sap-perf.ca Let's discuss the WORKLOAD MONITOR By D

HANA hdbuserstore

The hdbuserstore (hana secure user store) is a tool which comes as an executable with the SAP Hana Client package. This secure user store allows you to store SAP HANA connection information, including user passwords, securely on clients. With the help of secure store, the client applications can connect to SAP HANA without the user having to enter host name or logon credentials. You can also use the secure store to configure failover support for application servers in a 3-tier scenario (for example, SAP Business Warehouse) by storing a list of all the hosts that the application server can connect to. To access the system using secure store, there are two connect options: (1)key and (2)virtualHostName. key is the hdbuserstore key that you use to connect to SAP HANA, while virtualHostName specifies the virtual host name. This option allows you to change where the hdbuserstore searches for the data and key files. Note

Unlock the Power of SAP HANA Performance Optimization: A Comprehensive Guide for SAP Basis Administrators [Part 2 : Deep Dive ]

In SAP systems, the performance of the underlying database plays a crucial role in delivering a seamless user experience. SAP HANA, being an in-memory database, offers exceptional speed and agility. However, it's essential to monitor and troubleshoot potential performance bottlenecks to ensure optimal system performance. In this blog post, we will explore key areas to focus on when monitoring the SAP HANA database and provide examples of how to identify and address common performance issues. Threads : Threads in the SAP HANA database represent individual tasks that execute concurrently. Monitoring thread utilization helps identify any thread-related issues, such as high thread utilization or thread exhaustion. You can use the M_SERVICE_THREADS table to gather information about active threads and their status. For example, a high number of waiting threads may indicate resource contention or blocking situations that require investigation and optimization To retrieve  thread based on

Work Process and Memory Management in SAP

Let’s talk about the entire concepts that are related to memory when we talk about SAP Application. Starting with few basic terminologies, Local Memory :  Local process memory, the operating system keeps the two allocation steps transparent. The operating system does the other tasks, such as reserving physical memory, loading and unloading virtual memory into and out of the main memory. Shared Memory :  If several processes are to access the same memory area, the two allocation steps are not transparent. One object is created that represents the physical memory and can be used by various processes. The processes can map the object fully or partially into the address space. The way this is done varies from platform to platform. Memory mapped files, unnamed mapped files, and shared memory are used.  Extended Memory : SAP extended memory is the core of the SAP memory management system. Each SAP work process has a part reserved in its virtual address space for extended memory. You can set

How to resolve Common Error : Standard Template "sap_sm.xls" missing

Hey everyone, putting forward a common error we usually face when we have “ Excel inplace” functionality enabled in our SAP system. This error occurs when validity of the signature of SAP standard templates expired or were incorrectly delivered via support packages. We can reproduce the error by doing as below.. Click on “spreadsheet” icon after any SAP ALV grid view of data is on screen to make this data to export into excel directly from SAP.

ABAP Dumps Analysis

Ever now and then have you heard about ABAP Dumps, We also have a joke everything in temporary in life except ABAP dumps for SAP Consultants. Lets try to understand ABAP dumps from perspective of a SAP BASIS Consultant. Dumps happen when an ABAP program runs and something goes wrong that cannot be handled by the program We have two broad categories of Dumps , In custom program Dumps and SAP provided program Dumps. Dumps that happen in the customer namespace ranges (i.e. own-developed code) or known as Custom Program , can usually be fixed by the ABAP programmer of your team. Dumps that happen in SAP standard code probably need a fix from SAP. You do not have to be an "ABAPer" in order to resolve ABAP dump issues. The common way to deal with them is to look up in ST22 How to correct the error ? Hints are given for the keywords that may be used to search on the note system. Gather Information about the issue  Go to System > Status and Check the Basis SP level as well as info

SAP application log tables: BALHDR (Application Log: Header Data) and BALDAT (Application Log: Detail Data)

  BALHDR (Application Log: Header Data): Usage : The BALHDR table stores the header information for application logs. It serves as a central repository for managing and organizing log entries. Example Data Stored: The table may contain entries for various system activities, such as error messages, warnings, or information logs generated during SAP transactions or custom programs. Columns Involved: LOGNUMBER: Unique log number assigned to each log entry. OBJECT: Identifies the object associated with the log entry (e.g., a program, transaction, or process). SUBOBJECT: Further categorizes the object. USERNAME: User ID of the person who created the log entry. TIME: Date and time when the log entry was created. ADD_OBJECT: Additional information or details related to the log entry. BALDAT (Application Log: Detail Data): Usage : The BALDAT table contains the detailed data for each log entry, linked to the corresponding entry in the BALHDR table. It stores the specific log details and me