Wednesday, June 25, 2025

πŸ§ͺ Practicals Basics Programs - Data Types and Objects

 

Declare a TYPE as a character with 10 positions.
Solution:
REPORT z_abap101_001.

TYPES customer_name TYPE c LENGTH 10.

Declare an integer.
Solution:REPORT z_abap101_002.
DATA number_of_employees TYPE i.


Declare a type as a number with 7 positions.
Solution:
REPORT z_abap101_003.
TYPES number_of_unpaid_invoices TYPE n LENGTH 7.

Declare a date type.
Solution:
REPORT z_abap101_004.
TYPES creation_date TYPE d.

Declare a time type.
Solution:
REPORT z_abap101_005.
TYPES last_changed_at TYPE t.

Declare a structure type with 5 fields, each field with the same types from exercises 1 to 5.
Solution:

REPORT z_abap101_006.


TYPES customer_name TYPE c LENGTH 10.
DATA number_of_employees TYPE i.
TYPES number_of_unpaid_invoices TYPE n LENGTH 7.
TYPES creation_date TYPE d.

TYPES last_changed_at TYPE t.


 TYPES: BEGIN OF customer_structure,
name TYPE customer_name,
n_employees LIKE number_of_employees,
unpaid_invoices TYPE number_of_unpaid_invoices,
creation_date TYPE d,
last_changed_at TYPE t,
END OF customer_structure.


Note:

: (semicolon) is recommend in this case

name - This is a char with length 10

n_employees - This component (called ‘n_employees’ has the same type of the variable, but it is not

reusable)


Declare a type using the global structure SFLIGHT.

Solution:

REPORT z_abap101_007.

TYPES same_type_of_sflight TYPE SFLIGHT.

Note:

Double click on SFLIGHT

It's a global definition (it's inside the repository)

This is a TYPE, not a variable.


Declare a structure type with the following components of the global structure SFLIGHT:

CARRID, CONNID, FLDATE, PRICE, CURRENCY, PLANETYPE, SEATSMAX and

SEATSOCC.

Solution:

REPORT z_abap101_008.

* 1st Option - Declaring several TYPES
TYPES BEGIN OF some_components_sflight.
TYPES carrid TYPE sflight-carrid.
TYPES connid TYPE sflight-connid.
TYPES fldate TYPE sflight-fldate.
TYPES price TYPE sflight-price .
TYPES currency TYPE sflight-currency .
TYPES planetype TYPE sflight-planetype.
TYPES seatsmax TYPE sflight-seatsmax .
TYPES seatsoccupied TYPE sflight-seatsocc . " Different name for a component

TYPES END OF some_components_sflight.

 

* 2nd Option - Reusing TYPES keyword
TYPES: BEGIN OF some_components_sflight_2, " Semicolon ( : ) after TYPES
carrid TYPE sflight-carrid, " Comma after each component
connid TYPE sflight-connid,
fldate TYPE sflight-fldate,
price TYPE sflight-price,
currency TYPE sflight-currency,
planetype TYPE sflight-planetype,
seatsmax TYPE sflight-seatsmax,
seatsocc TYPE sflight-seatsocc,

END OF some_components_sflight_2.


Declare a structure type with the following components of the global structure SBOOK:

CARRID, CONNID, FLDATE, BOOKID, CUSTOMID.

Solution:

REPORT z_abap101_009.

TYPES: BEGIN OF flight_booking,

carrid TYPE sbook-carrid ,

connid TYPE sbook-connid ,

fldate TYPE sbook-fldate ,

bookid TYPE sbook-bookid ,

customid TYPE sbook-customid ,

END OF flight_booking. 


Declare a structure containing all the fields mentioned in exercises 8 and 9. Check it using

the ABAP Debugger.

Solution:

REPORT z_abap101_010.

* 2nd Option - Reusing TYPES keyword

TYPES: BEGIN OF some_components_sflight_2,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
fldate TYPE sflight-fldate,
price TYPE sflight-price,
currency TYPE sflight-currency,
planetype TYPE sflight-planetype,
seatsmax TYPE sflight-seatsmax,
seatsocc TYPE sflight-seatsocc,

END OF some_components_sflight_2.

 

TYPES: BEGIN OF flight_booking,
carrid TYPE sbook-carrid ,
connid TYPE sbook-connid ,
fldate TYPE sbook-fldate ,
bookid TYPE sbook-bookid ,
customid TYPE sbook-customid ,

END OF flight_booking.

 

* Note that some_components_sflight_2 and flight_booking have components with

the same name

* 'carrid', 'connid' and 'fldate'. 2 components cannot have the same name so

* in the structure below we add a suffix for each component originated in the

local

* structure flight_booking

TYPES: BEGIN OF sflight_sbook.
INCLUDE TYPE some_components_sflight_2.
INCLUDE TYPE flight_booking AS book RENAMING WITH SUFFIX _book.

TYPES END OF sflight_sbook.

 

START-OF-SELECTION. " F8 To Execute

DATA one_record TYPE sflight_sbook.

BREAK-POINT. " See one_record using the debugger



Declare a table type of integers.

Solution:

REPORT z_abap101_011.

TYPES table_type_with_number TYPE TABLE OF i.
DATA odd_numbers TYPE table_type_with_number.

DATA even_numbers TYPE table_type_with_number.


START-OF-SELECTION.


APPEND: 1 TO odd_numbers,
3 TO odd_numbers,
5 TO odd_numbers,
7 TO odd_numbers,

9 TO odd_numbers.

 

APPEND: 2 TO even_numbers,
4 TO even_numbers,
6 TO even_numbers,
8 TO even_numbers,

10 TO even_numbers.


Declare a table type with all components of the global structure SFLIGHT.

Solution:

REPORT Z_ABAP101_012.

TYPES table_type_sflight TYPE TABLE OF sflight.

DATA sflight_work_area TYPE LINE OF table_type_sflight.

DATA table_sflight TYPE table_type_sflight.

START-OF-SELECTION.

sflight_work_area-CARRID = 'AA'.

sflight_work_area-CONNID = '0017'.

sflight_work_area-FLDATE = 20131225. "Christmas

sflight_work_area-PRICE = '500.12'.

APPEND sflight_work_area TO table_sflight.

sflight_work_area-CARRID = 'AA'.

sflight_work_area-CONNID = '064'.

sflight_work_area-FLDATE = 20131225.

sflight_work_area-PRICE = '500.12'.

APPEND sflight_work_area TO table_sflight. 


Declare a table type using the structure type created in exercise 8.

Solution:

REPORT Z_ABAP101_013.

* 2nd Option - Reusing TYPES keyword

TYPES: BEGIN OF some_components_sflight_2,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
fldate TYPE sflight-fldate,
price TYPE sflight-price,
currency TYPE sflight-currency,
planetype TYPE sflight-planetype,
seatsmax TYPE sflight-seatsmax,
seatsocc TYPE sflight-seatsocc,

END OF some_components_sflight_2.

 

TYPES table_type_short_sflight TYPE TABLE OF some_components_sflight_2 WITH KEY

carrid connid fldate.


Declare a table type with the following components of the table SBOOK: CARRID,

CONNID, FLDATE, BOOKID, CUSTOMID but using CUSTOMID as part of the table key.

Solution:

REPORT Z_ABAP101_014.

TYPES: BEGIN OF flight_booking,
carrid TYPE sbook-carrid ,
connid TYPE sbook-connid ,
fldate TYPE sbook-fldate ,
bookid TYPE sbook-bookid ,
customid TYPE sbook-customid ,

END OF flight_booking .

 

TYPES table_type_booking TYPE TABLE OF flight_booking WITH KEY carrid connid

fldate customid. 


Declare a variable of type character with 10 positions and give it ‘Hello ABAP’ as an initial

value.

Solution:

REPORT Z_ABAP101_015.

DATA message TYPE c LENGTH 10 VALUE 'Hello ABAP'. 


Declare a variable of numeric type with 4 positions and initial value 1234.

Solution:

REPORT Z_ABAP101_016.

DATA amount TYPE f VALUE 1234.

Declare a variable of type integer with initial value 42.

Solution:

REPORT z_abap101_017.

DATA age TYPE i VALUE 42.

Declare a variable of type integer with initial value 12.72.

Solution:

REPORT z_abap101_018.

DATA round_number TYPE i VALUE '12.72'.

WRITE round_number.


Declare a variable of type date and give it halloween day.

Solution:

REPORT Z_ABAP101_019.

DATA any_day TYPE d.

any_day = '20131031'. " Halloween.


Declare a packed number variable with 7 decimal places.

Solution:

REPORT z_abap101_020.

DATA many_decimals TYPE p LENGTH 10 DECIMALS 7.

many_decimals = '123456789.987654321'.

WRITE many_decimals. 


Declare a variable of type S_CARR_ID.

Solution:

REPORT Z_ABAP101_021.

DATA air_line_code TYPE s_carr_id.


Declare a variable of the same type of field carrid from table SPFLI.

Solution:

REPORT Z_ABAP101_022.

DATA air_line_code TYPE spfli-carrid.


Declare a variable of the same type of field FLDATE table SFLIGHT.

Solution:

REPORT z_abap101_023.

DATA flight_date TYPE sflight-fldate.


Declare a structure of the same type of SBOOK.

Solution:

REPORT z_abap101_024.

DATA single_booking TYPE sbook.


Declare a structure with fields of the table SFLIGHT carrid, CONNID, FLDATE, PRICE,

CURRENCY, PLANETYPE, and SEATSMAX SEATSOCC.

Solution:

REPORT z_abap101_025.

TYPE: BEGIN OF some_components_of_one_flight,
carrid TYPE sflight-carrid ,
connid TYPE sflight-connid ,
fldate TYPE sflight-fldate ,
price TYPE sflight-price ,
currency TYPE sflight-currency ,
planetype TYPE sflight-planetype,
seatsmax TYPE sflight-seatsmax ,
seatsocc TYPE sflight-seatsocc ,

END OF some_components_of_one_flight.


Declare a structure with all fields of the table SBOOK and the field TELEPHONE from

SCUSTOM table.

Solution:

REPORT z_abap101_026.

DATA: BEGIN OF sbook_with_phone.

INCLUDE STRUCTURE sbook.

DATA phone TYPE scustom-telephone.

DATA END OF sbook_with_phone. 


Declare an internal table with fields of the table SBOOK CARRID, CONNID, FLDATE,

BOOKID, CUSTOMID.

Solution:

REPORT z_abap101_027.

TYPES: BEGIN OF ty_sbook ,
carrid TYPE sbook-carrid,
connid TYPE sbook-connid,
fldate TYPE sbook-fldate,
bookid TYPE sbook-bookid,
customer_id TYPE sbook-customid,

END OF ty_sbook.

 

TYPES ty_itab_sbook TYPE TABLE OF ty_sbook WITH KEY carrid connid fldate bookid.


DATA itab_sbook TYPE ty_itab_sbook.


Declare an internal table with all table fields from table SCARR.

Solution:

REPORT z_abap101_028.

DATA it_scarr TYPE STANDARD TABLE OF scarr.


Declare an internal table with all table fields SPFLI.

Solution:

REPORT z_abap101_029.

TYPES ty_spfli TYPE spfli.

DATA itab_spfli TYPE TABLE OF ty_spfli.


Declare an internal table with all table fields from SCARR and the field TELEPHONE

from table SCUSTOM.

Solution:

REPORT z_abap101_030.

TYPES: BEGIN OF ty_scarr_telephone.
INCLUDE TYPE scarr.
TYPES: phone TYPE scustom-telephone,

END OF ty_scarr_telephone,

ty_itab_scarr_telephone TYPE SORTED TABLE OF ty_scarr_telephone WITH UNIQUE KEY

carrid.

DATA itab TYPE ty_itab_scarr_telephone.

Declare a constant which contains your name.

Solution:

REPORT z_abap101_031.

CONSTANTS c_full_name TYPE string VALUE 'ABAP101.com'.


Declare two constants which contain the values 'X' (true) and ' ' (false).

Note: This is a common practice as ABAP does not contain a boolean primitive type.

Solution:

REPORT z_abap101_032.

TYPES ty_boolean TYPE c. " what is the length?

CONSTANTS c_true TYPE ty_boolean VALUE 'X'.

CONSTANTS c_false TYPE ty_boolean VALUE space.


Declare a constants which contains the 5 first decimals of Pi.

Solution:

REPORT z_abap101_033.

CONSTANTS c_pi TYPE p LENGTH 7 DECIMALS 5 VALUE '3.14159'.


Declare a work area of constants. All components must be integers.

Solution:

REPORT z_abap101_034.

CONSTANTS: BEGIN OF c_integers,

first TYPE i VALUE 1,

second TYPE i VALUE 2,

third TYPE i VALUE 3,

END OF c_integers.

Declare a work area of 5 constant components. All of them should have different primitive

types.

Solution:

REPORT z_abap101_035.

CONSTANTS: BEGIN OF c_primitives,

char TYPE c LENGTH 4 VALUE 'CHAR',

int TYPE i VALUE 101,

date TYPE d VALUE '20141225',

time TYPE t VALUE '112359',

string TYPE string VALUE `STRING`,

END OF c_primitives.


Is it possible to declare an internal table of constants?

Answer:

No. No initial value can be specified for internal tables and references

REPORT z_abap101_036.

TYPES ty_integer TYPE i.

TYPES ty_integers TYPE TABLE OF ty_integer.

CONSTANTS itab_integers TYPE ty_integers VALUE 1. " Syntax error


Declare all types and constants from type-pools ABAP and ICON.

Solution:

REPORT z_abap101_037.

TYPE-POOLS: icon, abap.

CONSTANTS c_favorite_icon LIKE icon_information VALUE '@0S@'.

DATA is_true TYPE abap_bool.


Declare a constant which type is the same of another constant.

Solution:

REPORT z_abap101_038.

CONSTANTS c_a TYPE c VALUE 'A'.

CONSTANTS c_z LIKE c_a VALUE 'Z'.


Declare a type which is used in another type, variable, work area, internal table and

constant.

Solution:

REPORT z_abap101_039.

TYPES ty_reused_date TYPE d.

TYPES ty_creation_date TYPE ty_reused_date.

DATA v_creation_date TYPE ty_reused_date.

DATA v_update_date TYPE ty_reused_date.

TYPE: BEGIN OF wa_document,

name TYPE string,

creation_date TYPE ty_reused_date,

update_date TYPE ty_reused_date,

END OF wa_document.

DATA: itab_documents LIKE TABLE OF wa_document.

CONSTANTS c_update_date TYPE ty_reused_date VALUE '99991231'.


Declare a variable which is used in another variable, type, work area, internal table and

constant.

Solution:

REPORT z_abap101_040.

DATA v_reused_time TYPE t.

DATA v_last_changed_at LIKE v_reused_time.

TYPES ty_last_changed_at LIKE v_reused_time.

DATA: BEGIN OF wa_employee,

name TYPE string,

next_meeting_at LIKE v_reused_time,

END OF wa_employee.

DATA: BEGIN OF itab_employees OCCURS 0,

name TYPE string,

next_meeting_at LIKE v_reused_time,

END OF itab_employees.

CONSTANTS c_lunch_time LIKE v_reused_time VALUE  '130000'.

No comments:

Post a Comment