Difference between Function and 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:
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:
DATA: lv_number TYPE I VALUE 5,
lv_squared TYPE I.
CALL FUNCTION 'ZSQUARE_NUMBER'
NUMBER = lv_number
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 type: Database View
SFLIGHT-CARRID = SCARR-CARRID
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.
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.