CDS, Amdp & Abap 7.4
CDS, Amdp & Abap 7.4
CDS, Amdp & Abap 7.4
Attribute View
It is used to create data view based on Join condition.
Mostly used for Master Data such as Customer, Vendor, Material and so on.
Allowed Joins are mentioned below
c. Derived:
Used for reading existing attribute view (Read-Only Attribute View). You can only
change the description of the attribute when using this type.
b. Data Foundation:
It is used for adding tables, creating table joins, creating output columns and calculated
columns, Add filter to attributes.
Points to Remember:
Valid scenario for Referential Join is
i. It is 100% guaranteed that for each row in one table, there is at least one join partner in
the other table
ii. That holds true in both directions
iii. At all times.
Referential Join is a feature available only in OLAP engine. When testing Attribute View outside
the context of an Analytical view then the join engine will perform an inner join.
Referential Join cannot be used if a filter is set on a field in the right table.
Text Join is available only for SAP ERP tables with SPRAS field.
Next step is to select fields which will form the part of output list as shown below,
To add new calculated field, select Data Foundation and then click on "+" sign in the output
section as shown below,
Enter the required details for newly created column along with its formula as shown below,
Always validate your
formula before
saving or activating.
Next step is mandatory for Attribute View i.e. selection of Key Fields.
Click on Semantics and then from Columns tab, select the key fields as shown below,
To preview output of your attribute view, click on below icon and select any one of the options
as shown below,
Choose the Schema "SAP" followed by your ABAP system ID from which table is coming. In our example,
it is SAPEH2. In the required schema, find the structure by the name "Tables" as shown below,
Select "Tables" from tree structure and right click on it and select Filters so that we can narrow down
our search to required tables. In our case, we want to search MARA table as shown below,
Drag MARA table and drop it on Data Foundation.
Follow same process of filtering for MAKT table and drag and down it on Data Foundation as shown
below,
Now that required tables are selected in Data Foundation, next step is to create Joins between them.
Select any field and right click on it and choose "Create Join" as shown below,
Now apply the required join conditions to as many fields as you want as shown below,
You can add the required column to output either from Details of Data Foundation or Details from
Semantics as shown below,
Now we want to restrict the "Material Description" only to "English Language" so we need filter at
MAKT-SPRAS = "EN" which is achieved as shown below,
Now select the key field (as it is mandatory for Attribute View) from details section in Semantics as
shown below,
To create New Calculated Column, go back to Data Foundation and in Output Section, click on "+"
Button as shown below,
Now enter the name, data type and required formula for new calculated field as shown below,
In our case, we want the new calculated column to show Material Number followed by Material Type
followed by Material description as shown below,
New calculated column is shown with "Calculator indicator" as shown below,
Now go back to Semantics section and click on "View Properties" to define
Default Client.
Privileges.
Now you can either "Save and Validate" or "Save and Activate" our Attribute View as shown,
Once Attribute is activated without errors, we can check the results of our view in 2 ways namely,
Data Preview Editor.
Open SQL Editor.
Drag and drop Time tables from Schema "_SYS_BI" as shown below,
Select the field which needs to be shown in output.
We normally receive below warning message when creating Derived Attribute View.
Semantic section of existing view is copied to Derived Attribute View as shown below,
Data Preview of Output:
b. Output Pane:
This is the place where you define Input structure (Variables or Input Parameters) and
output structure.
To create output structure, right click on "Output Parameters and define the fields which needs
to be output as shown below,
Once Output table is defined, we can use it in Script View for passing the final value as shown
below,
To test the Procedure, Click on Procedure name in Content and select the SQL Console as shown
below,
Empty SQL console will appear as shown below,
Output of Procedure:
Get Database
Connection for SQL
Mapping of Internal
Table and Procedure
Output Table
Filling the Internal Table
with Procedure Output
Data
Output of Report:
You can use the shortcut for getting the Full name of HANA view as shown below,
If you are using Transport Number then mention it in below step,
Use Synchronize Button, if you want to sync views from HANA Studio to ABAP.
View is still not active in SAP. It
needs to be activated in SAP
HANA ABAP Perspective so that it
becomes Active in SAP.
Private Attribute:
These are columns selected from Data Foundation (FACT table) and appear in
final output.
Navigation Attributes:
These are columns selected from Attribute Views in Star or Logical Join Node
and appear in final output.
Calculated Column:
It is the new column that is created with extra conditions from existing columns
but it is not considered as MEASURES.
ii. Measures:
Measures are simple, calculated or restricted as mentioned below,
Simple:
These are columns that can be quantified using existing columns. No need for
creating new columns.
Calculated:
These are new columns that are created using "Column Type" as "MEASURE".
COUNTER is special case of CALCULATED MEASURE. Counters are used to obtain
the count of distinct values for given set of attributes.
Restricted:
It helps you restrict MEASURE values based on Attribute Restriction.
In other words, Add filter or expression on Attribute (or column) and assign it to
MEASURE.
It follows same aggregation as related MEASURE column.
Data Flow
a. Data Foundation:
It is used to create a fact table by adding and joining the tables.
In other words, you can add columns, create column filters and create JOINS if more than one
table.
If there are more than one table in the data foundation node, specify the central table (FACT
table) from which the modeler must derive the measures.
MEASURES can be identified from one table alone.
c. Semantic View:
It is used to classify the columns and calculated columns as attribute and measures.
You can also create variables/input parameters and assign variables to the columns in the
semantics node.
i. Input Parameters:
It helps you parameterize views and execute them based on the values you
provide at query runtime.
It is used in PLACEHOLDER clause of the SQL statement.
Example:
SELECT "PRODUCT",
"REGION",
SUM ("SALES_AMOUNT") AS "SALES_AMOUNT"
FROM "SYS_BIC"."Package Name/View Name"
('PLACEHOLDER' = ($$DISCOUNT$$,'15')
Group by "PRODUCT","REGION";
ii. Variables:
Variables are bound to a specific attributes.
They are used to filter the results.
Variables are interpreted as the "WHERE" Clause of the SQL statement.
Example:
SELECT "PRODUCT",
"REGION",
SUM ("SALES_AMOUNT") AS "SALES_AMOUNT" Variable used is
FROM "SYS_BIC"."Package Name/View Name" LV_REGION.
WHERE ("REGION" = :LV_REGION)
Group by "PRODUCT","REGION";
Business Scenario:
User wants to get sales per material for a given range of sales order.
Get material and quantity from VBAP tables and sum it up as per material no and display the
output.
In content folder, right click on the package where you intend to create the view and select the
required view as shown below,
Enter the view name and label.
Click on Data Foundation and go through the path of Catalog => Schema => Tables as shown
below,
Filter on VBAP table, drag and drop it to Data Foundation. Follow same process for VBAK as well.
In case, you add more than one table in Data Foundation, it is necessary to decide which table
will be the Central Entity.
Hint: Always use the Central Entity as the table which will have the MEASURES.
In our case, "NETWR" is the measure and it belongs to table VBAP so Central Entity should be
VBAP.
Once "Central Entity" is done, create Join between the Data Foundation tables and select the
required fields which should be part of final FACT table as shown below,
Creating Joins
between Tables
Below is screenshot of selected fields of FACT table,
If possible, always
validate your Join
Conditions.
Now that FACT table is ready, go ahead and add the required Attribute View to "Logical Join"(or
Star Join) by selecting it from below path,
As shown below, we now have Data Foundation and Attribute View in Details section of "Star
Join".
Star Join consist of
Fields from Attribute Views.
Selected Columns from Data Foundation.
Calculated Columns.
Restricted Columns.
Input Parameters.
Below mentioned output section of Star Join plays an important role in Analytic View,
Create Join condition between Data Foundation's selected fields and Attribute view fields as
shown below,
Always validate your join by clicking on "Validate Join" button as shown below,
It's mandatory that Analytic View must have at least one MEASURE field so click on Semantics
section and define the columns as either Attribute or Measure.
In our example, NETWR will be Measure and remaining display fields will be Attribute as shown
below,
Since it is possible to declare variables which are used in WHERE clause when fetching data from
Analytic view, please declare a suitable variable for VBELN (Sales Order) as shown below,
Click on "Create Variable" and mention the details in below popup screen.
Value Help from View and its
Attribute Column
Now Save and Activate the View and execute in HANA Studio as shown below,
Enter the required data and execute as shown below,
Output:
Comma
Separator
2. Currency Conversion from USD to INR in Analytic View
There are 2 ways for Currency Conversion which are mentioned below,
Click on the RED button as highlighted above, you will receive a pop-up for selecting different
Semantic Types. In our case, Currency Conversion is related to "Amount with Currency Code" so
select this option.
Now, select Decimal Shift and Conversion checkboxes. Fill the required fields for conversion.
Here, you have an option either to hardcode values for conversion (which will never be case in
live system) or create "Input Parameter" for each field which will be know during execution as
shown below,
Definition of Input Parameters such as GV_CLIENT,GV_UNIT,GV_EXCHANGE_TYPE and
GV_CONV_DATE is mentioned below,
Now "Save and Activate" the view and execute the query. On execution, you will receive below
pop-up for Variables and Input Parameter,
Fill them as per your requirement as shown below,
Since we are performing conversion of MEASURE field (NETWR), we can select Column Type as
"Measure" and fill the required fields as shown below,
Name of the field whose
value needs to be converted
to Target Currency
As per above screen, Aggregation should happen "After calculation is complete" as checkbox
"Calculate before Aggregation" is not selected.
In expression, enter the field which contains original value (in our case NETWR).Now click on tab
"Semantics" highlighted in RED color in above screenshot,
Select Decimal Shift and Conversion checkboxes; fill the required fields for conversion.
Here, you have an option either to hardcode values for conversion (which will never be case in
live system) or create "Input Parameter" for each field which will be know during execution as
shown below,
Now "Save and Activate" the view and execute the query. On execution, you will receive below
pop-up for Variables and Input Parameter,
Fill them as per your requirement as shown below,
Pass dynamic
PLACEHOLDER values
in Procedure.
Calling
Procedure
in SQL
Console Save and Activate the Procedure and execute it in SQL Console as shown below,
Output of Procedure:
Please keep in mind the below comments mentioned for PLACEHOLDERS and VARIABLES when using CE
Operators.
Create new procedure which can output same result as Analytic View - AN_SALES_TRAINING_ATEST
using CE Operator.
Use of variables
Testing Procedure in SQL Console:
Output:
Output:
Use of
CE_CALC
Use of Input Parameter
Output:
7. Calling Procedure in Native SQL with Scalar Input
Consider calling Procedure defined in Analytic View steps 5 namely
PROCEDURE_AN_VW_CE_CONVERSION in ABAP.
Delete Local Temporary
Table in HANA
Call HANA
Procedure from
ABAP
Read data from HANA Temporary Table
Business Scenario:
Fetch data from KNB1 table based on list of Customer no entered on selection screen.
Transport Request
Details
Synchronize the procedure from HANA to ABAP and then activate it as shown below,
Synchronize
and Activate.
You can check the newly created interface in SE24 as shown below,
Use of Interface (from SE24)
for referencing input and
output parameters of
Procedure
Calling of Proxy
Procedure in ABAP
Calculation View
They are used to create your own data foundation using database tables, attribute views and
analytic views to address a complex business requirement.
A calculation view is based on a calculation model that consists of Nodes and Operations.
Nodes are any type of tables or views.
Operations that can be performs on nodes are
a. Join
b. Projection
c. Aggregation
d. Union
e. Rank.
There are 2 ways in which you can implement Calculation view namely
Graphical Way.
SQLScript Implementation.
It does not allow base column tables, Attribute Views or Analytic views to be added at data
foundation. All tables must be changed to Dimension Calculation views to use in Star Join. In
other words, create Calculation View of Data Category "Dimension" for tables or view which
needs to be added in Star Join.
FACT tables have no such restriction in Calculation View with Star Join.
Below is example of how to create Dimension Calculation View,
Dimension Calculation
View => Data Category as
Dimension
Business Scenario:
To display Sales Data of VBAK and VBAP tables with summation at NETWR per material and
KWMENG per material for a sales order.
Here, we create a Calculation View with Data Category as "CUBE" and not selecting the "With
Star Join" as shown below,
Default Node is
Aggregation for such
Calculation View
First create two Projection nodes and then drag and drop tables VBAK and VBAP in Projection as
shown below,
Projection of VBAK Projection of VBAP
Table Table
Select output fields from each Projection Nodes of VBAK and VBAP tables as shown below,
Next step is to create a join on projections of VBAK and VBAP. So create join node as shown
below,
Empty Join
Node
Now add the projections of VBAK and VBAP to Join Node as shown below,
Now, create join between the selected projections of VBAK and VBAP as shown below,
Now select the fields in join node which will be visible to higher nodes as shown below,
Click on VBAK_VBAP_JOIN node and drag and drop the arrow towards the Default Aggregation
Node as shown below,
Select the columns from Aggregation Node which needs to be part of final output of Calculation
View as shown below,
Since it is Aggregation Node, we need to add at least one column as "Aggregated Column" as
shown below,
Aggregated
Columns or
MEASURES
Now, enter the required code for fetching data from Calculation View
"CA_VW_CUBE_EXAMPLE" as shown below,
Input Parameters
of Procedure
Testing of Procedure:
Call the procedure in SQL console and enter values mentioned below,
Output:
You can enter your Transport Number here if applicable. In our case, it is temporary object and
hence no transport request.
Activate your external view using below button,
Here, we will be using VBAP table and project few of its fields as output as shown below,
HANA View Name
Drag and drop VBAP table from required schema into Projection Node as show below,
Select Output fields from VBAP table
Testing of Procedure:
Now execute the view and enter valid values for Client and Sales Order no fields as shown
below,
Output:
a) Implementation via Procedure and Single Value Inputs for Sales Order and Client
Create a new Procedure to implement CA view (CA_VW_DIMENSION_EXAMPLE) as shown
below,
Its Input and Output Structure.
Fetching data from CA View "CA_VW_DIMENSION_EXAMPLE" as show below,
Scalar Input
Parameters
Testing of Procedure:
Call the Procedure CA_VW_DIMENSION_EXAMPLE in SQL Console as shown below,
Output:
b) Implementation via Procedure and Dynamic Where Clause
In this example, we will be passing dynamic where clause so that we can pass "SELECT-
OPTIONS" from ABAP to HANA Procedure.
Create a single field input parameter which should be type string as shown below,
Default Node is
Star Join.
Add VBAK and VBAP
tables and then use
projection node
Select the fields from each projection of VBAK and VBAP as shown below,
Next step is to create Join between projection of VBAK and VBAP as shown below,
Creating Join
between
Projections of
VBAK and VBAP
Field
selection
from each
Projection
view
Data Foundation is
added in Star Join
in this step.
Next step is addition of other tables or Attribute View or Analytic View in Star Join. But, as per
rule, you can't directly use them in Star Join. Instead they need to be converted into Dimension
Calculation View and then you can add them in Star Join.
In our example, we want to add Attribute View "AT_MATERIAL_TRAINING_ATEST" in Star Join.
So, first step is to convert the Attribute View into Dimension Calculation View and then use it in
Star Join.
Below are the steps for converting Attribute View to Dimension Calculation View as shown
below,
Save and activate the new Dimension Calculation View as shown below,
Projection Node
for VBAK and
VBAP
Click on Union Node to get the below screen,
We need to define the mapping for target fields using union. We have 2 options namely,
Auto Map by Name => It will automatically map each field by its name.
Manually Drag and Drop the fields to Target Pattern.
Click on auto mapping or you
can drag and drop required
fields to Target.
Add the Union and then
select the required fields.
Set as
MEASURE
Save the changes at this point and create the variables for sales order no and client fields.
Execute the view and enter the required values as shown below,
Warning as Variables
and Input Parameter
are not mapped
After selecting the output fields, Select Input Parameters and right click on it and choose
Manage Mappings as shown below,
You can see that variables / Input Parameters are added in Semantic => Parameters / Variables
of Calculation View after Mapping is completed as shown below,
Create Variables for Calculation View as shown below,
Execute the view and enter the required fields as shown below,
In our example, we want to display the output based on Sales Order Type as shown below,
MEASURE
Execute the view and enter the required values as shown below,
Create a new class by using the path Package Name => New => ABAP Class as shown below,
Enter the Class Name and its details as shown below,
ABAP Package
Name
By adding DATABASE
PROCEDURE statement in
method implementation
we ensures that it is
AMDP method
Pass by Value
The newly created class is now available in path Source Library -> Classes of the package
where it was saved as shown below,
Calling of
AMDP
Method
Create a new class following the same process mentioned in AMDP Process Overview topic.
Below is the code for the class where AMDP is called inside AMDP,
AMDP Method
AMDP Method
Calling AMDP
Method
inside AMDP
Method
Now next step is to consume the above AMDP method in ABAP report as shown below,
Calling AMDP
Method
Execute the report and entered the required customer no on selection screen as shown below,
Changing
Parameter in
Methods
Same rule
Now, method "CALL_GET_CARRIERS" internally calls the method "GET_CARRIERS". applies as
Below is syntax of calling it, calling AMDP
methods
Mentioning
inside AMDP
"_IN_" is
required if
calling AMDP
with Changing
Parameter's in Changing Parameter = Same Parameter as
other methods. Input as well as Output.
You can refer below link for more documents and examples of AMDP,
http://help.sap.com/abapdocu_740/en/index.htm?file=abenamdp.htm
In the next popup, enter the name of the CDS view name as shown below,
CDS View Name
In next popup, select the Transport Request in which you want to save the CDS view and click on
Finish button as shown below,
Consider creating simple example of inner join between VBAK and VBAP table as shown below,
View name which
can seen in SE11
Report output:
Below sections summarize the language elements of the DDL and DCL of the ABAP CDS,
Expressions
Operators Meaning
MAX, MIN, AVG, SUM, COUNT Aggregate expressions in a SELECT statement
+, -, *, / Arithmetic expressions in a SELECT statement
CASE, WHEN, THEN, ELSE, END Case distinctions in a SELECT statement
CAST Casting expression in a SELECT statement
=, <>, <, >, <=, >=, BETWEEN, LIKE, IS
NULL, NOT, AND, OR Relational expressions in a SELECT statement
@AbapCatalog.sqlViewName: 'CDS_DB_VIEW'
...
[DEFINE] VIEW cds_entity
Name of CDS Entity
[name_list]
Column name of final output [parameter_list] Input Parameters for the view
AS select_statement [;]
Select statement
a. Example 1: Simple Select Statement
ABAP View name
found in SE11
@AbapCatalog.sqlViewName: 'ZV_ATEST_01'
Define View ZV_ATEST_01_CDS
Name of CDS Entity
As select from VBAP
{
Key VBAP.MANDT,
Key VBAP.VBELN,
Key VBAP.POSNR,
VBAP.MATNR
};
We can provide
@AbapCatalog.sqlViewName: 'ZV_ATEST_02' column name here
Define View ZV_ATEST_02_CDS
(client, sales_order_no, item_no, material)
As select from VBAP
{
Key VBAP.MANDT,
Key VBAP.VBELN,
Key VBAP.POSNR,
VBAP.MATNR
};
Input Parameters
@AbapCatalog.sqlViewName: 'ZV_ATEST_02' for the view
Define View ZV_ATEST_02_CDS
With parameters P_VBELN: ABAP.CHAR (10),
P_POSNR : ABAP.NUMC (6)
As select from VBAP
{
Key VBAP.MANDT,
Key VBAP.VBELN,
Key VBAP.POSNR,
VBAP.MATNR
}
WHERE VBELN = :P_VBELN
Two different ways
AND POSNR = $PARAMETERS.P_POSNR
to address input
parameters
Restriction on Parameter List:
We can use it as operand in below places,
i. Element in the select list.
ii. Operand of an arithmetic expression.
iii. Operand "operand" directly after case in a case distinction.
iv. Right side of an expression in
Where Clause or Having Clause.
On condition in an ABAP join or an association.
In a filter condition of a path expression.
Extend View:
Syntax:
@AbapCatalog.sqlViewAppendName: 'CDS_APPEND_VIEW'
...
EXTEND VIEW cds_entity WITH cds_view_extension
{
select_list_extension
} [;]
Example : Extend Existing View
@AbapCatalog.sqlViewAppendName: 'ZV_ATEST_01_EXT'
ABAP data objects used in Open SQL statements (usually variables) are now interpreted as host
variables.
From Release 7.40, SP05, host variables can and should be prefixed with the escape character @.
Host variables without the escape character are obsolete.
If the escape character is used in front of a name of an Open SQL statement, the syntax check is
performed in a strict mode, which handles the statement more strictly than the regular syntax check.
The escape character can only be used in Unicode programs, in which the program property fixed point
arithmetic is activated.
EXAMPLE:
SELECT VBELN ,
POSNR ,
MATNR
FROM VBAP
INTO TABLE @LIT_VBAP
WHERE VBELN = @P_VBELN.
2. String Templates
The purpose of a string template is to create a new character string out of literal texts and embedded
expressions.
A string template is defined by using the | (pipe) symbol at the beginning and end of a template.
The added value of a string template becomes clear when combining literal texts with embedded
expressions and control characters.
Embedded expressions are defined within a string template with curly brackets { expression }. Note that
a space between bracket and expression is obligatory.
Output:
3. Chaining Operator
The Chaining Operator && can be used to create one character string out of multiple other
strings and string templates.
The use of the chaining operator is to replace CONCATENATE statement.
In this example, a number text, a space, an existing character string and a new string template
are concatenated into a new character string.
4. Inline Declaration
You can now directly define variables, field symbols or internal table will using them.
Field Symbol Declaration DATA: <LFS_LINE> TYPE -------- LOOP AT ITAB ASSIGNING FIELD-SYMBOL(<LFS_LINE>).
-----------
LOOP AT ITAB ASSIGNING <LFS_LINE>. ENDLOOP.
-----------
ENDLOOP.
Select Into Table Declaration (ONLY DATA: ITAB TYPE TABLE OF VBAK, DATA: LV_VBELN TYPE VBAK-VBELN.
POSSIBLE IN ABAP 7.50 RELEASE) LV_VBELN TYPE VBAK-VBELN.
SELECT *
SELECT * FROM VBAK
FROM VBAK INTO TABLE @DATA(ITAB)
INTO TABLE ITAB WHERE VBELN = @LV_VBELN.
WHERE VBELN = LV_VBELN.
Select Single Into Declaration(ONLY PARAMETERS: P_VBELN TYPE VBAK-VBELN. PARAMETERS: P_VBELN TYPE VBAK-VBELN.
POSSIBLE IN ABAP 7.50 RELEASE)
DATA: LV_VBELN TYPE VBAK-VBELN, SELECT SINGLE VBELN,
LV_AUART TYPE VBAK-AUART. AUART
FROM VBAK
SELECT SINGLE VBELN AUART INTO @DATA(LWA_VBAK)
FROM VBAK WHERE VBELN = @P_VBELN.
INTO ( LV_VBLEN, LV_AUART) IF SY-SUBRC = 0.
WHERE VBELN = P_VBELN. WRITE : / LWA_VBAK-VBELN , LWA_VBAK-AUART.
IF SY-SUBRC = 0. ENDIF.
WRITE : / LV_VBELN , LV_AUART.
ENDIF.
5. Table Expression
Table expression is a new way to read data from internal table.
Example: Example:
"Record Found.
Example:
TRY.
LWA_VBAK = LIT_VBAK[VBELN = '1'].
………………
CATCH CX_SY_ITAB_LINE_NOT_FOUND.
……………….
ENDTRY.
There will be a short dump if you use an inline expression that references a non-existent record.
SAP says you should therefore assign a field symbol and check sy-subrc.
ASSIGN LIT_ITAB[ 1 ] to FIELD-SYMBOL(<LFS_ITAB>).
IF sy-subrc = 0.
...
ENDIF.
Example:
DATA: itab TYPE TABLE OF I,
lwa_tab TYPE I.
lwa_tab = 3.
Append lwa_tab to itab.
lwa_tab = 5.
Append lwa_tab to itab.
"Bad example
itab[table_line = 3] =itab[ table_line = 3 ] * itab[ table_line = 3 ].
"Good example
ASSIGN itab[ table_line = 5 ] TO FIELD-SYMBOL(<fs>).
<fs> = <fs> * <fs>.
Example of Index:
Index Table Expression
Selection Screen:
OUTPUT:
Selection Screen:
OUTPUT:
Selection Screen:
Output:
Example of Record exist in Table:
Selection Screen:
Output:
Output:
6. Conversion Operator
Syntax:
Before 7.40:
After 7:40:
OR
7. Value Operator
The value operator VALUE is used to fill complex data objects with values in operand positions,
create initial values of any data type, or control the results of table expressions.
Syntax:
Tables: VALUE dtype( (comp1 = a1 comp2 = a2) (comp1 = a3 comp2 = a4) ... ) ) OR
VALUE #( (comp1 = a1 comp2 = a2 ) (comp1 = a3 comp2 = a4) ... ))
Example of Table(unstructured):
Output:
Description Before 7.40 After 7.40
Value TYPES: BEGIN OF TY_STRUCT, TYPES: BEGIN OF TY_STRUCT,
Operator FIRST(4) TYPE C, FIRST(4) TYPE C,
Structure SEC(4) TYPE C, SEC(4) TYPE C,
Example LAST(4) TYPE C, LAST(4) TYPE C,
END OF TY_STRUCT. END OF TY_STRUCT.
LWA_STRUCT-FIRST = 'ONE' . LWA_STRUCT = VALUE #( FIRST = 'ONE' SEC = 'TWO' LAST = 'THREE' ).
LWA_STRUCT-SEC = 'TWO'.
LWA_STRUCT-LAST = 'THREE' . CL_DEMO_OUTPUT=>DISPLAY( LWA_STRUCT ).
CL_DEMO_OUTPUT=>DISPLAY( LWA_STRUCT ).
LWA_STRUCT-FIRST = '1' .
LWA_STRUCT-SEC = '2'.
LWA_STRUCT-LAST = '3' .
CL_DEMO_OUTPUT=>DISPLAY( LIT_STRUCT ).
8. New Operator
A constructor expression with the instance operator NEW creates an anonymous data object or
an instance of a class. The result is a reference variable that points to the new object.
Syntax:
NEW type(…….)
DATA : t_vendors TYPE SORTED TABLE OF ty_vendor WITH TYPES: tt_vendors TYPE SORTED TABLE OF ty_vendor
UNIQUE KEY vendor, WITH UNIQUE KEY vendor,
lit_citys TYPE STANDARD TABLE OF char30 WITH EMPTY KEY, tt_citys TYPE STANDARD TABLE OF char30 WITH EMPTY
lwa_citys LIKE LINE OF lit_citys, KEY.
lwa_vendor LIKE LINE OF t_vendors.
DATA(t_vendors) = VALUE tt_vendors(
lwa_vendor-vendor = 'A0001'. ( vendor = 'A0001' city = 'MH' route = 'R0007' )
lwa_vendor-city = 'MH'. ( vendor = 'A0002' city = 'TM' route = 'R0008' )
lwa_vendor-route = 'R0007'. ( vendor = 'A0003' city = 'RS' route = 'R0007' )
append lwa_vendor to t_vendors. ( vendor = 'A0004' city = 'BA' route = 'R0004' ) ).
lwa_vendor-vendor = 'A0004'.
lwa_vendor-city = 'BA'.
lwa_vendor-route = 'R0004'.
append lwa_vendor to t_vendors.
lwa_citys = lwa_vendor-city.
APPEND lwa_citys to lit_citys.
ENDLOOP.
CL_DEMO_OUTPUT=>DISPLAY( lit_citys ).
OUTPUT:
DATA : itab TYPE STANDARD TABLE OF line WITH EMPTY KEY, TYPES : itab TYPE STANDARD TABLE OF line WITH EMPTY KEY.
wa LIKE LINE OF itab.
DATA(itab) = VALUE itab(
wa-col1 = 11. FOR j = 11 THEN j + 10 UNTIL j > 40
wa-col2 = 12. ( col1 = j col2 = j + 1 col3 = j + 2 ) ).
wa-col3 = 13.
APPEND wa TO itab.
CL_DEMO_OUTPUT=>DISPLAY( itab ).
wa-col1 = 21.
wa-col2 = 22.
wa-col3 = 23. OUTPUT :
APPEND wa TO itab.
COL1 COL2 COL3
wa-col1 = 31. 11 12 13
wa-col2 = 32. 21 22 23
wa-col3 = 33. 31 32 33
APPEND wa TO itab.
CL_DEMO_OUTPUT=>DISPLAY( itab ).
DATA : itab TYPE STANDARD TABLE OF line WITH EMPTY KEY, TYPES : itab TYPE STANDARD TABLE OF line WITH EMPTY KEY.
wa LIKE LINE OF itab.
itab = VALUE #( FOR j = 1 WHILE j <= 3 ( col1 = j col2 = j + 1
wa-col1 = 1. col3 = j + 2 ) ).
wa-col2 = 2.
wa-col3 = 3. CL_DEMO_OUTPUT=>DISPLAY( itab ).
APPEND wa TO itab.
OUTPUT:
wa-col1 = 2.
wa-col2 = 3.
wa-col3 = 4. COL1 COL2 COL3
APPEND wa TO itab. 1 2 3
2 3 4
wa-col1 = 3. 3 4 5
wa-col2 = 4.
wa-col3 = 5.
APPEND wa TO itab.
CL_DEMO_OUTPUT=>DISPLAY( itab ).
... FILTER type( itab [EXCEPT] [IN ftab] [USING KEY keyname]
WHERE c1 op f1 [AND c2 op f2 [...]] ) ...
REPORT demo_filter_value_condition.
SELECT *
FROM spfli
INTO TABLE @spfli_tab.
DATA(extract) =
FILTER #( spfli_tab USING KEY carr_city
WHERE carrid = CONV #( to_upper( carrid ) ) AND
cityfrom = CONV #( to_upper( cityfrom ) ) ).
cl_demo_output=>display( extract ).
DATA(rest) =
FILTER #( spfli_tab EXCEPT USING KEY carr_city
WHERE carrid = CONV #( to_upper( carrid ) ) AND
cityfrom = CONV #( to_upper( cityfrom ) ) ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Those rows are filtered from an internal table spfli_tab filled with flight data that have a specific value in
the columns carrid and cityfrom and the result is assigned to an internal table extract. A suitable
secondary key carr_city is defined for the internal table here.
A further internal table, rest, is given the same table filtering, but the addition EXCEPT is specified. The
assertion statements ASSERT demonstrates that rest contains all rows from spfli_tab that are not
in extract.
REPORT demo_filter_table.
ENDMETHOD.
METHOD init.
cl_demo_input=>add_field( CHANGING field = cityfrom ).
cl_demo_input=>request( CHANGING field = cityto ).
cityfrom = to_upper( cityfrom ).
cityto = to_upper( cityto ).
IF cityfrom = cityto.
LEAVE PROGRAM.
ENDIF.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Those rows are filtered out of an internal table spfli_tab filled with flight data that have specific
values in the columns cityfrom and cityto.
The filter is applied using a filter table of the type filter_tab constructed using VALUE filled with
input values. The result is all the outbound and return flights for the cities in question.
The casting operator CAST is a constructor operator that executes an up or down cast for reference variables with
the type specified in type.
You use CAST for a down cast where you needed helper variables before in order to cast with ?= to a requested
reference type.
You use CAST for an up cast, e,g, with an inline declaration, in order to construct a more general type.
You can write a component selector -> directly behind CAST type( ... ).
Output:
The static type of the reference variable iref declared inline should be the interface not the class.
INTERFACE if.
...
ENDINTERFACE.
CLASS cl IMPLEMENTATION.
METHOD factory.
ref = NEW #( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(iref) = CAST if( cl=>factory( ) ).
Example with data objects
A constructor expression with CAST followed by -> is an LHS-expression, you can assign values to it.
SYNTAX:
OUTPUT:
SWITCH
... SWITCH dtype|#( operand
WHEN const1 THEN result1
[ WHEN const2 THEN result2 ]
...
[ ELSE result ] ) ...
OR
13. Is Instance of
The new predicate expression IS INSTANCE OF can be used to detect the dynamic type of an object
reference variable. This makes it possible to check the feasibility of a down cast before it is executed.
In the below example, a check is done to determine if variable PARAM is a table, a structure or referring
to a data element.
14. Move-Corresponding
Syntax :
Variant for Structures
struc1-itab = VALUE #(
( col1 = 11 col2 = 12 )
( col1 = 21 col2 = 22 ) ).
Output:
MOVE-CORRESPONDING
The original content of itab2 is deleted. After the assignment, the columns col2 and col3 have
the same content as in itab1, whereas col4remains initial.
The original content of itab2 is preserved. Three new rows are added in which the
columns col2 and col3 have the same content as in itab1, whereas col4 remains initial .
The original content of itab2 is deleted. After the assignment, the column col2 has the same
content as in itab1 and col4 remains initial. The tabular component col3 is resolved and the
identically named component col2 found there. The original content of itab2-col3 is deleted.
After the assignment, the column col2 has the same content as in itab1-col3, whereas the
column col3 remains initial.
The original content of itab2 is preserved. Three new rows are added, to which the same applies
as to the results of the previous assignment.
15. CL_ABAP_CORRESPONDING - SYSTEM CLASS in ABAP 7.50
The system class CL_ABAP_CORRESPONDING enables assignments of components between structures or between
internal tables with dynamically specified mapping rules.
The factory method CREATE of a class must be used to create a mapping object before the class can be used:
Structures struct or internal table's itab of the assigned data types must be passed to the
parameters source and destination.
An internal table of the type CL_ABAP_CORRESPONDING=>MAPPING_TABLE, containing the mapping rule, must be
passed to the parameter mapping. If an initial mapping table is passed, only the identically named components are
assigned. The mapping table has the following components:
LEVEL
Level of the components in the structure or row structure. The value 0 stands for the top level.
KIND
SRCNAME
DSTNAME
The rows of the internal table must be constructed so that they produce a mapping rule in the correct order.
Components of the source structure for which no mapping is defined and that were not excluded are assigned to
identically named components of the target structure.
The method EXECUTE of a mapping object can be used to perform any number of assignments between structures
or internal tables src and dst whose data type matches the source type or target type specified when the object
was created:
between the remaining identically named components at the same level (if not excluded in the mapping
rule).
In assignments between structures, components of the target structure to which no components of the source
structure are assigned keep their previous value, like the statement MOVE-CORRESPONDING but unlike the
operator CORRESPONDING without the addition BASE.
Any nested internal tables are always resolved, as when the addition EXPANDING NESTED TABLES is specified
in MOVE-CORRESPONDING or the addition DEEP for the operator CORRESPONDING. In assignments between
internal tables, the target table is always initialized first.
There is no matching addition for the addition KEEPING TARGET LINES in MOVE-
CORRESPONDING or BASE in CORRESPONDING.
Source and target must not be the same otherwise the runtime error CORRESPONDING_SELF can occur. Incorrect
parameters passed to the methods of the class CL_ABAP_CORRESPONDING raised exceptions of the
class CX_CORR_DYN_ERROR.
Example:
Uses the class CL_ABAP_CORRESPONDING for assignments of components to a simple structure. The mapping rule
dictates that the components a3are assigned to b1 and a1 to b3. The component a2 is ignored since there are no
identically named components in the target structure and b2 keeps its value. a4 and a5 in the target structure also
keep their values, however, even though the source structure contains identically named components. This is
because the value of CL_ABAP_CORRESPONDING=>MAPPING_EXCEPT_ALL is specified for the mapping type for all
non-specified components. The executable example for simple structures enables interactive input of the
component names that are mapped to each other.
DATA:
BEGIN OF struct1,
a1 TYPE string VALUE 'a1',
a2 TYPE string VALUE 'a2',
a3 TYPE string VALUE 'a3',
a4 TYPE string VALUE 'a4',
a5 TYPE string VALUE 'a5',
END OF struct1,
BEGIN OF struct2,
b1 TYPE string VALUE 'b1',
b2 TYPE string VALUE 'b2',
b3 TYPE string VALUE 'b3',
a4 TYPE string VALUE 'b4',
a5 TYPE string VALUE 'b5',
END OF struct2.
DATA(mapper) =
cl_abap_corresponding=>create(
source = struct1
destination = struct2
mapping = VALUE cl_abap_corresponding=>mapping_table(
( level = 0 kind = 1 srcname = 'a1' dstname = 'b3' )
( level = 0 kind = 1 srcname = 'a3' dstname = 'b1' )
( level = 0 kind = 3 ) ) ).
16. Corresponding
Syntax
Basic Form
Lookup Table
results in a data reference pointing to dobj with the static type specified by type. With other
words, REF is the short form for GET REFERENCE OF dobj INTO.
Syntax
A LET expression defines variables var1, var2 ... or field symbols <fs1>, <fs2>, ... as local auxiliary fields in
an expression and assigned values to them.
When declared, the auxiliary fields can be used in the operand positions of the expression. There is no
way of accessing an auxiliary field statically outside its expression.
The documentation of an expression specifies whether it contains a LET expression and in which
positions. Any LET expressions in an expression or sub expression are evaluated first.
Example:
OUTPUT:
The lossless operator EXACT is a constructor operator that executes either a lossless calculation or a
lossless assignment.
arith_exp is an arithmetic expression that is calculated lossless with calculation type decfloat34 and the
result is converted to the specified type.
... EXACT dtype|#( arg ) ...
arg is not an arithmetic expression and its value is assigned to the result of the specified type according
to the rules for lossless assignments.
Lossless calculations
Lossless calculations were introduced for decimal floating point numbers in Release 7.02 with the
addition EXACT to COMPUTE. This addition (better the whole statement COMPUTE) became obsolete
now.
A lossless calculation must not perform any rounding. If it does, an exception occurs.
OUTPUT:
Lossless assignments
Lossless assignments were introduced for conversions in Release 7.02 with the addition EXACT to MOVE.
This addition (better the whole statement MOVE) became obsolete now.
A lossless assignment is an assignment where
the value of the source is valid for its type
there are no data losses during the assignment
the converted value of the target is valid for its type
OUTPUT:
20. MESHES
The meshes are special structures whose components are internal tables, which can be linked to each other
using associations. Associations are evaluated by specifying mesh paths in suitable expressions and
statements.
Mesh types can be created using the following new variant of the TYPES statement:
Meshes are instances of these types and can be used in suitable expressions and statements.
Processing Meshes
These relationships (defined using associations) between mesh nodes in a mesh can be evaluated in mesh
paths. These mesh paths can be used in the following expressions and statements:
LOOP AT mesh_path
EXAMPLE:
REPORT demo_mesh_expressions_flights.
TRY.
out->begin_section(
'Forward Association scarr\_spfli' ).
DATA(spfli_wa) =
flights-scarr\_spfli[ flights-scarr[ carrname = name ]
connid = connection ].
out->write( spfli_wa ).
out->next_section(
'Inverse Association spfli\^_spfli~scarr' ).
DATA(scarr_wa) =
flights-spfli\^_spfli~scarr[
flights-spfli[ carrid = ID connid = CONNECTION ] ].
out->write( scarr_wa ).
out->next_section(
'Chained Association scarr\_spfli\_sflight' ).
DATA(sflight_wa) =
flights-scarr\_spfli[ flights-scarr[ carrname = name ]
connid = connection
]\_sflight[ fldate = date ].
out->write( sflight_wa ).
out->next_section(
'Addressing Component' ).
DATA(price) =
flights-scarr\_spfli[ flights-scarr[ carrname = name ]
connid = connection
]\_sflight[ fldate = date ]-price.
out->write( price ).
out->next_section(
'Assigning to Field Symbol' ).
ASSIGN
flights-scarr\_spfli[ flights-scarr[ carrname = NAME ]
connid = CONNECTION
]\_sflight[ fldate = DATE ]
TO FIELD-SYMBOL(<sflight_wa>).
IF sy-subrc = 0.
out->write( 'Field symbol OK' ).
ENDIF.
out->next_section(
'Write Access and Check Existence' ).
flights-scarr\_spfli[ flights-scarr[ carrname = NAME ]
connid = CONNECTION
]\_sflight[ fldate = DATE
]-price = price - 10.
IF line_exists(
flights-scarr\_spfli[ flights-scarr[ carrname = name ]
connid = connection
]\_sflight[ fldate = date
price = price - 10 ] ).
out->write( 'Line found!' ).
ENDIF.
CATCH cx_sy_itab_line_not_found.
out->write( 'Exception!' ).
ENDTRY.
out->display( ).
ENDMETHOD.
METHOD class_constructor.
in = cl_demo_input=>new( ).
out = cl_demo_output=>new( ).
SELECT *
FROM scarr
INTO TABLE @flights-scarr.
SELECT *
FROM spfli
INTO TABLE @flights-spfli.
SELECT *
FROM sflight
INTO TABLE @flights-sflight.
SELECT *
FROM sairport
INTO TABLE @flights-sairport.
ENDMETHOD.
METHOD input.
TRY.
date = flights-sflight[ carrid = id
connid = connection ]-fldate.
CATCH cx_sy_itab_line_not_found.
LEAVE PROGRAM.
ENDTRY.
in->add_field( CHANGING field = name
)->add_field( CHANGING field = connection
)->add_field( CHANGING field = id
)->add_field( CHANGING field = date
)->request( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
OUTPUT:
The following mesh paths are used as mesh path expressions:
Mesh path with forward association as an initial association on the right side of an assignment. The source
in the square brackets is a table expression, plus a condition.
Mesh path with inverse association as an initial association on the right side of an assignment. The source
in the square brackets is a table expression.
Mesh path with path extension after the initial association on the right side of an assignment.
The mesh path with path extension is also used to demonstrate the addressing of a component using the structure
component selector, assignments to a field symbol, reads, and usage in the predicate function line_exists.
Comma-Separated Lists
In Open SQL, all operands in lists can now be separated by commas and this is also the recommended
way of separating them from Release 7.40, SP05.
Comma-separated lists are now also possible in Unicode programs where the program attribute fixed
point arithmetic is activated:
This makes blank-separated lists obsolete.
SQL Expressions
From Release 7.40, SP05, SQL expressions can be specified in a comma-separated SELECT list.
The result of an expression of this type (whose operands can be the names of columns or host variables)
is determined by the database system and passed to the application server in the appropriate column of
the results set.
EXAMPLE:
REPORT demo_sql_expr_arith.
cl_demo_output=>display( results ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
OUTPUT:
The coalesce function in Open SQL returns the value of the argument arg1 (if this is not the null value);
otherwise it returns the value of the argument arg2.
EXAMPLE:
START-OF-SELECTION.
demo=>main( ).
OUTPUT:
Switching Automatic Client Handling
The new addition USING CLIENT clnt switches automatic client handling to the client specified in clnt.
It can be specified in all Open SQL statements as an alternative to CLIENT SPECIFIED.
From Release 7.40, SP05, the right side of a join expression is no longer restricted to single
tables or views. The right side can itself be a (parenthesized) join expression whose results set is
then evaluated.
In Release 7.40, SP05 and higher, LEFT OUTER JOIN and RIGHT OUTER JOIN can be used., ,
From Release 7.40, SP05, fields from the right side in the WHERE condition of the
current SELECT statement can be specified in LEFT OUTER JOIN. Fields from the left side can be
specified in RIGHT OUTER JOIN.
If all required components are statically identifiable, the assignment of the fields in the
addition CORRESPONDING after INTO is now determined when the program is generated and is not
delayed until runtime.
The addition INTO CORRESPONDING now affects the results set defined in the SELECT list. If one or more
names match, all the columns for which there are no name matches are removed from the results set. If
there are no name matches, none of the columns are removed from the results set.
Examples