1.1 Inline declarations
Data Declarations
In ABAP you have many operand positions, where the value of the operand is changed by the statement. The most typical of these "write positions" is the left hand side lhs of an assignment.
lhs = rhs.
But of course there are more. The data objects you can use at these write positions are either writable formal parameters of the procedure you are working in or variables declared with DATA in front of the statement.
In many cases the variables filled by a statement are helper variables that you only need close to the statement. For each of these helper variables you had to write a data declaration with the DATA statement and of course it was your task to give the variable an adequate type.
Well, the operand type of most write positions is statically fixed and well known to the compiler. And this is why ABAP can offer inline data declarations with Release 7.40. The ingredients are so called declaration positions (write positions with fully known operand type) and the new declaration operator DATA(...).
Let's look at some examples.
Declaration of a lhs-variable for a simple assignment
Before 7.40
DATA text TYPE string.
text = `...`.
With 7.40
DATA(text) = `...`.
Declaration of table work areas
Before 7.40
DATA wa like LINE OF itab.
LOOP AT itab INTO wa.
...
ENDLOOP.
With 7.40
LOOP AT itab INTO DATA(wa).
...
ENDLOOP.
Declaration of a helper variable
Before 7.40
DATA cnt TYPE i.
FIND ... IN ... MATCH COUNT cnt.
With 7.40
FIND ... IN ... MATCH COUNT DATA(cnt).
Declaration of a result
Before 7.40
DATA xml TYPE xstring.
CALL TRANSFORMATION ... RESULT XML xml.
With 7.40
CALL TRANSFORMATION ... RESULT XML DATA(xml).
Declaration of actual parameters
Before 7.40
DATA a1 TYPE ...
DATA a2 TYPE ...
oref->meth( IMPORTING p1 = a1 IMPORTING p2 = a2 ... )
With 7.40
oref->meth( IMPORTING p1 = DATA(a1) IMPORTING p2 = DATA(a2) ... ).
Declaration of reference variables for factory methods
Before 7.40
DATA ixml TYPE REF TO if_ixml.
DATA stream_factory TYPE REF TO if_ixml_stream_factory.
DATA document TYPE REF TO if_ixml_document.
ixml = cl_ixml=>create( ).
stream_factory = ixml->create_stream_factory( ).
document = ixml->create_document( ).
With 7.40
DATA(ixml) = cl_ixml=>create( ).
DATA(stream_factory) = ixml->create_stream_factory( ).
DATA(document) = ixml->create_document( ).
Field Symbols
For field symbols there is the new declaration operator FIELD-SYMBOL(...) that you can use at exactly three declaration positions.
ASSIGN ... TO FIELD-SYMBOL(<fs>).
LOOP AT itab ASSIGNING FIELD-SYMBOL(<line>).
...
ENDLOOP.
READ TABLE itab ASSIGNING FIELD-SYMBOL(<line>) ...
I guess it is clear to you what happens here.
Outlook
In my upcoming blogs I will make use of inline declarations when introducing other new features. Be prepared for code like this:
TYPES t_itab TYPE TABLE OF i WITH EMPTY KEY.
DATA(itab) = VALUE t_itab( ( 1 ) ( 2 ) ( 3 ) ).
Yes, this is ABAP 7.40 ...
1.2 Constructor expression CORRESPONDING
MOVE-CORRESPONDING FOR Internal Tables
You can use MOVE-CORRESPONDING not only for structures but also for internal tables now. Components of the same name are are assigned row by row. New additions EXPANDING NESTED TABLES and KEEPING TARGET LINES allow to resolve tabular components of structures and to append lines instead of overwriting existing lines.
Example
MOVE-CORRESPONDING itab1 TO itab2 EXPANDING NESTED TABLES
KEEPING TARGET LINES.
More About
Expressions and Functions
LET Expressions in Constructor Expressions
New LET expressions as sub expressions of constructor expressions allow you to define local variables or field symbols as auxiliary fields of constructor expressions.
Example
LET expression in a table construction using the VALUE operator.
cl_demo_output=>new( )->write(
VALUE text( LET it = `be` IN
( |To { it } is to do| )
( |To { it }, or not to { it }| )
( |To do is to { it }| )
( |Do { it } do { it } do| ) ) )->display( ).
More About
CORRESPONDING Operator
The new constructor operator CORRESPONDING allows to execute a "MOVE-CORRESPONDING" for structures or internal tables at operand positions. Besides assigning components of the same name you can define your own mapping rules.
Example
struct2 = CORRESPONDING #( struct1 MAPPING b4 = a3 ).
More About
Table Comprehensions
A new FOR sub expression for constructor expressions with operators NEW and VALUE allows to read existing internal tables and to construct new tabular contents from the lines read.
Example
Construction of an internal table itab2 from lines and columns of an internal table itab1. You can of course also use the CORRESPONDING operator to construct the lines.
DATA(itab2) = VALUE t_itab2( FOR wa IN itab1 WHERE ( col1 < 30 )
( col1 = wa-col2 col2 = wa-col3 ) ).
More About
Meshes
This one is a little bit tricky. 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 .The full power of meshes will become more clear in the moment when associations will be supported by Open SQL for database views (CDS views, see below) in the future.
Example
A mesh flights is declared from a mesh type T_FLIGHTS. In T_FLIGHTS you have tabular components as so called mesh nodes that are linked by associations. A structured data object root is constructed to serve as the start line for the following LOOP over a mesh path. The results are lines from SFLIGHT that are found by following the mesh path evaluating the associations between its mesh nodes.
TYPES: t_scarr TYPE SORTED TABLE OF scarr WITH UNIQUE KEY carrid,
t_spfli TYPE SORTED TABLE OF spfli WITH UNIQUE KEY carrid connid,
t_sflight TYPE SORTED TABLE OF sflight WITH UNIQUE KEY carrid connid fldate.
TYPES: BEGIN OF MESH t_flights,
scarr TYPE t_scarr ASSOCIATION to_spfli TO spfli ON carrid = carrid
USING KEY primary_key,
spfli TYPE t_spfli ASSOCIATION to_sflight TO sflight ON carrid = carrid
AND connid = connid USING KEY primary_key,
sflight TYPE t_sflight,
END OF MESH t_flights.
DATA: flights TYPE t_flights.
...
DATA(root) = flights-scarr[ carrname = 'United Airlines' ].
LOOP AT
flights-scarr\to_spfli[ root ]\to_sflight[ ]
INTO DATA(wa).
...
ENDLOOP.
More About
Open SQL(New Syntax)
From 7.40, SP05
l Lists in Open SQL statements can and should be separated by a comma.
l Host variables in Open SQL statements can and should be escaped by a @.
You will be able to use other new functions that are based on a new SQL parser in the ABAP kernel.
Example
SELECT carrid, connid, fldate FROM sflight
INTO CORRESPONDING FIELDS OF TABLE @sflight_tab
WHERE carrid = @carrier AND connid = @connection
ORDER BY carrid, connid.
More About
SQL Expressions
You can use SQL expressions in a column list behind SELECT. The result of such an expression is calculated on the database (code push down!) and written into the respective columns of the result set. Operands can be data base columns or host variables.
Possible expressions are
l elementary values
l arithmetic expressions
l arithmetic functions abs, ceil, floor, div, mod
l castings with cast
l string concatenations with &&
l coalesce
l case
Expressions can be mixed and proritized with parentheses.
Examples
SELECT id, num1, num2, cast( num1 AS fltp ) / cast( num2 AS fltp ) AS ratio,
div( num1, num2 ) AS div, mod( num1, num2 ) AS mod,
@offset + abs( num1 - num2 ) AS sum
FROM demo_expressions INTO CORRESPONDING FIELDS OF TABLE @results
ORDER BY SUM DESCENDING.
SELECT id, CASE char1
WHEN 'aaaaa' THEN ( char1 && char2 )
WHEN 'xxxxx' THEN ( char2 && char1 )
ELSE @else
END AS text
FROM demo_expressions INTO CORRESPONDING FIELDS OF TABLE @results.
More About
CDS Views
The new CDS (Core Data Services) enable you to define Views of the ABAP Dictionary with a DDL (Data Definition Language) in ADT. This DDL encompasses the DDL of SQL and enhances it with the possibility to define annotations and associations. CDS-Views in the Dictionary are not bound to a specific database platform. They provide another way of database independent code push down .You can access CDS-Views with Open SQL.
Example
Definitiion of a simple view based on only one database table. Of course, you can join as you like ...
@AbapCatalog.sqlViewName: 'BPA_VW'
define view business_partner as
select from snwd_bpa { key bp_id, company_name, bp_role }
You can access the view in ABAP programs e.g. as follows:
SELECT * FROM business_partner INTO TABLE itab ...
More About
See also the dedicated blog:
New Data Modeling Features in SAP NW ABAP 7.4 SP5 | SAP Blogs
ABAP Managed Database Procedures
ABAP Managed Database Procedures (AMDP) are a class based framework for maintaining and calling stored procedures as so called AMDP procedures from ABAP. An AMDP procedure is implemented in its native DB-language in an AMDP method of an AMDP class. Currently, the only database that supports AMDP is SAP's HANA database.
An AMDP class must implement a specific tag interface. Currently, there is only one namely IF_AMDP_MARKER_HDB. An AMDP class can be maintained in ADT only. An AMDP method looks from its declaration like a normal ABAP method and can be used like that. Only when implementing the method, you denote the database and the DB language. You also must denote the entities to be used. The following is an example for using the language SQLScript of the HANA database:
CLASS cl_demo_amdp DEFINITION PUBLIC.
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb .
METHODS increase_price
IMPORTING
VALUE(clnt) TYPE sy-mandt
VALUE(inc) TYPE sflight-price .
ENDCLASS.
CLASS cl_demo_amdp IMPLEMENTATION.
METHOD increase_price BY DATABASE PROCEDURE FOR HDB
LANGUAGE SQLSCRIPT
USING sflight.
update sflight set price = price + :inc
where mandt = :clnt;
ENDMETHOD.
ENDCLASS.
Please note that a simple example as the above one is only to show the syntax. Implementing functionality in an AMDP method means the usage of Native SQL. And for the usage of Native SQL the same holds as before: Stay open as long as possible! It makes absolutely no sense to push down statements to the database that you can also express in Open SQL, because those are pushed down by the Native SQL Interface in exactly the same way or even better!
You use AMDP as a convenient way of programming and handling Native SQL in ABAP only if you really gain something from it. And for that, simple examples are not too easy to find, especially since Open SQL is empowered by new features now (see above).
Example
A simple example of a database functionality that is not readily available in Open SQL is the predefined currency conversion of HANA. An AMDP method that uses this conversion might look as follows:
METHOD convert BY DATABASE PROCEDURE FOR HDB
LANGUAGE SQLSCRIPT
USING demo_prices.
PRICES = select * from DEMO_PRICES;
PRICES =
CE_CONVERSION (
:PRICES,
[ family = 'currency',
method = 'ERP',
steps = 'shift,convert,shift_back',
target_unit = :TO_CURRENCY,
client = :MANDT,
source_unit_column = "CURRENCY",
reference_date = :DATE,
output_unit_column = "CURRENCY",
error_handling = 'keep unconverted' ],
[ amount AS amount ] );
replace DEMO_PRICES select * from :PRICES;
ENDMETHOD.
An alternative implementation for databases other than the SAP HANA database must then be provided, e.g. as follows:
METHOD abap_convert.
DATA prices TYPE STANDARD TABLE OF demo_prices.
SELECT *
&nb

最低0.47元/天 解锁文章
1075

被折叠的 条评论
为什么被折叠?



