65
Horst Keller, Jens Lieberum/ TIP Core ABAP Platform September, 2011 CD266

CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Embed Size (px)

Citation preview

Page 1: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Horst Keller, Jens Lieberum/ TIP Core ABAP Platform

September, 2011

CD266Modern ABAP Programming: Speaking the ABAP Language Today

Page 2: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 2

Disclaimer

This presentation outlines our general product direction and should not be relied on in making a

purchase decision. This presentation is not subject to your license agreement or any other agreement

with SAP. SAP has no obligation to pursue any course of business outlined in this presentation or to

develop or release any functionality mentioned in this presentation. This presentation and SAP's

strategy and possible future developments are subject to change and may be changed by SAP at any

time for any reason without notice. This document is provided without a warranty of any kind, either

express or implied, including but not limited to, the implied warranties of merchantability, fitness for a

particular purpose, or non-infringement. SAP assumes no responsibility for errors or omissions in this

document, except if such damages were caused by SAP intentionally or grossly negligent.

Page 3: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 3

Agenda

Motivation

Modern ABAP

Selected Topics

Modularization

Expressions

String Processing

Secondary Keys for Internal Tables

Exact move

Open SQL Locators

Outlook

Page 4: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Motivation

Page 5: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 5

ABAP Evolution

R/2 ABAP ABAP/4 Modern ABAP ABAP Objects Unicode enabled Future ABAP

Page 6: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Modern ABAP

Page 7: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 7

What Is Modern ABAP?

Using modern ABAP means using modern concepts and features and to follow some rules

Modeling

Separation of Concerns (SoC)

KISS

Structuring

ABAP Objects

Programming

Use new language elements

Don’t use obsolete language elements

Follow programming guidelines

Page 8: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 8

New Language Elements

It‘s there,

get it, use it!

Page 9: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 9

Obsolete Language Elements

Still there,

but don‘t

use them ...

Page 10: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 10

ABAP Programming Guidelines

The ABAP Programming Guidelines

consist of about 120 rules divided into the subjects

General Basic Rules

ABAP Specific Basic Rules

Structure and Style

Architecture

Safe and robust ABAP

cover an area that spans

from very general subjects like

Separation of Concerns

to very special subjects like

Usage of COLLECT for internal tables

Page 11: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Selected TopicsModularization

Page 12: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 12

Modularization Basics

SoC and KISS are naturally achieved by modularization of applications

Basic rules

Modularize, don’t atomize

Restrict nesting depth and number of operational statements of a processing block

Page 13: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 13

Source Code Modularization

Include Programs

Use Include programs to organize large programs

Do not reuse code of Include programs

Macros

Use only simple macros to make code more readable

Do not use macros to replace procedures

Modularization Techniques

DEFINE get_data.

DATA wa TYPE &1.

SELECT SINGLE *

FROM &1

INTO wa

WHERE &2 = &3 AND

&4 = &5.

END-OF-DEFINITION.

DEFINE app.

APPEND &1 TO source.

END-OF-DEFINITION. app `program.`.

app `class main definition.`.

app ` public section.`.

app ` class-data`.

...

Page 14: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 14

Procedures

Subroutines

Simply obsolete

Function Modules

Use only where needed for technical reasons

No implementations in function modules, call methods instead

Methods

The only recommended way for modularization

Enable modern source code notation

Automatically ensure stricter syntax rules

Modularization Techniques

FORM do_something.

...

ENDFORM.

CLASS class IMPLEMENTATION.

METHOD do_something.

...

ENDMETHOD.

ENDCLASS.

Page 15: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 15

Method Calls

Functional calls of methods

Modularization Techniques

CALL FUNCTION 'ABAP_BROWSER_SHOW_HTML'

EXPORTING

html_string = html

title = title

modal = abap_true.

cl_abap_browser=>show_html(

html_string = html

title = title

modal = abap_true ).

CALL METHOD cl_abap_browser=>show_html

EXPORTING

html_string = html

title = title

modal = abap_true.

Page 16: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 16

Bottom Line

ABAP Objects …

Modularization Techniques

… don’t fear it, use it!

Page 17: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

ExerciseWarm up - From function groups and subroutines to classes and methods

Page 18: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Selected TopicsExpressions

Page 19: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 19

Expressions

Old fashioned ABAP: Lots of keywords …

Arithmetic expressions behind COMPUTE only.

Logical expressions with variables as operands only.

Small set of built-in function in very few operand positions only.

Functional methods in very few operand positions only.

Code littered with helper variables.

DATA offlen TYPE i.

DATA textlen TYPE i.

DATA section TYPE string.

offlen = off + len.

textlen = STRLEN( text ).

IF offlen <= textlen.

section = text+off(len).

CALL METHOD meth

EXPORTING

section = section.

ENDIF.

Page 20: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 20

Expressions

Modern ABAP, supported with NW 7.0, EhP2

Lots of positions for expressions and functions.

String expressions.

Debugger support for expressions.

Large set of built-in functions.

Nested and chained method calls.

IF off + len <= strlen( text ).

meth( substring( val = text

off = off

len = len ) ).

ENDIF.

Page 21: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 21

Expressions

More operands and brackets,

less keywords …METHOD create_html_71.

DATA html TYPE string .

html = |<html lang="{ get_iso_langu( sy-langu ) }">| &&

`<head>` &&

`<meta name="Demo" content="Demo">` &&

`<style type="text/css">` &&

`span.cn {font-family: Courier New;}` &&

`</style>` &&

`</head>` &&

`<body>` &&

|<h3>{ text-tit }</h3>| &&

'<span class ="cn">' &&

|<hr width={ get_width( ) * 16 } align=left>| &&

replace( val = |<i>{ text-hd1

WIDTH = get_width( ) / 2

ALIGN = RIGHT

PAD = '#' }{

text-hd2

WIDTH = get_width( )

ALIGN = RIGHT

PAD = '#' }</i>|

sub = '#'

with = '&nbsp;'

occ = 0 ) &&

|<hr width={ get_width( ) * 16 } align=left>|.

DO 10 TIMES.

html = html &&

replace( val = |{ sy-index

WIDTH = get_width( ) / 2

ALIGN = RIGHT

PAD = '#' }{

sy-index ** 2

WIDTH = get_width( )

ALIGN = RIGHT

PAD = '#' }<br>|

sub = '#'

with = '&nbsp;'

occ = 0 ).

ENDDO.

html = html &&

|<hr width={ get_width( ) * 16 } align=left>| &&

'</span>' &&

`</body>` &&

`</html>`.

show_html( EXPORTING html = html

title = text-stp && ` Release 7.1` ).

ENDMETHOD.

METHOD create_html_70.

DATA html TYPE string .

DATA iso_langu type t002-laiso.

DATA width TYPE i.

DATA length1 TYPE i.

DATA length2 TYPE i.

DATA buffer TYPE c LENGTH 100.

DATA buffer1 TYPE string.

DATA buffer2 TYPE string.

DATA result TYPE i.

DATA title TYPE cl_abap_browser=>title.

iso_langu = get_iso_langu( sy-langu ).

CONCATENATE html `<html lang="` iso_langu`EN">` INTO html.

CONCATENATE html `<head>` INTO html.

CONCATENATE html `<meta name="Demo" content="Demo">` INTO html.

CONCATENATE html `<style type="text/css">` INTO html.

CONCATENATE html `span.cn {font-family: Courier New;}` INTO html.

CONCATENATE html `</style>` INTO html.

CONCATENATE html `</head>` INTO html.

CONCATENATE html `<body>` INTO html.

CONCATENATE html `<h3>` text-tit`</h3>` INTO html.

CONCATENATE html '<span class ="cn">' INTO html.

width = get_width( ) * 16.

WRITE width TO buffer LEFT-JUSTIFIED.

CONCATENATE html `<hr width=` buffer ` align=left>` INTO html.

length1 = get_width( ) / 2.

WRITE text-hd1 TO buffer LEFT-JUSTIFIED.

SHIFT buffer(length1) RIGHT DELETINGTRAILING ` `.

buffer1 = buffer(length1).

REPLACE ALL OCCURRENCES OF ` ` IN buffer1 WITH '&nbsp;'.

length2 = get_width( ).

WRITE text-hd2 TO buffer LEFT-JUSTIFIED.

SHIFT buffer(length2) RIGHT DELETINGTRAILING ` `.

buffer2 = buffer(length2).

REPLACE ALL OCCURRENCES OF ` ` IN buffer2 WITH '&nbsp;'.

CONCATENATE html `<i>` buffer1 buffer2 `</i>` INTO html.

width = get_width( ) * 16.

WRITE width TO buffer LEFT-JUSTIFIED.

CONCATENATE html `<hr width=` buffer ` align=left>` INTO html.

DO 10 TIMES.

result = sy-index ** 2.

length1 = get_width( ) / 2.

WRITE sy-index TO buffer LEFT-JUSTIFIED.

SHIFT buffer(length1) RIGHT DELETING TRAILING ` `.

buffer1 = buffer(length1).

REPLACE ALL OCCURRENCES OF ` ` IN buffer1WITH '&nbsp;'.

length2 = get_width( ).

WRITE result TO buffer LEFT-JUSTIFIED.

SHIFT buffer(length2) RIGHT DELETING TRAILING ` `.

buffer2 = buffer(length2).

REPLACE ALL OCCURRENCES OF ` ` IN buffer2WITH '&nbsp;'.

CONCATENATE html buffer1 buffer2`<br>` INTO html.

ENDDO.

width = get_width( ) * 16.

WRITE width TO buffer LEFT-JUSTIFIED.

CONCATENATE html `<hr width=` buffer ` align=left>` INTO html.

CONCATENATE html '</span>' INTO html.

CONCATENATE html `</body>` INTO html.

CONCATENATE html `</html>` INTO html.

CONCATENATE text-stp'Release 7.0' INTO title SEPARATED BY space.

show_html( EXPORTING html = html

title = title ).

ENDMETHOD.

Page 22: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 22

Expressions

Calculations in logical expressions

v1 = a + b.

v2 = c - d.

v3 = meth( v2 ).

IF v1 > v3.

... IF a + b > meth( c – d ).

...

Page 23: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 23

Expressions

Predicate functions

DATA matcher TYPE REF TO cl_abap_matcher.

matcher = cl_abap_matcher=>create( text = `abcd`

pattern = `.+bc.+` ).

IF matcher->match( ) = abap_true.

...

ENDIF.

IF matches( val = `abcd`

regex = `.+bc.+` ).

...

ENDIF.

Page 24: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 24

Expressions

Boolean functions

IF field1 = field2.

flag = abap_true.

ENDIF.

oref->meth( flag ).

flag = boolc( field1 = field2 ).

oref->meth(

boolc( field1 = field2 ) ).

Page 25: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 25

Expressions

Expressions in index positions

len = strlen( txt ) - 1.

DO len TIMES.

...

idx = lines( itab ).

READ TABLE itab INDEX idx ...

DO strlen( txt ) – 1 TIMES.

...

READ TABLE itab

INDEX lines( itab ) ...

Page 26: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 26

Expressions

Functional methods as operands

regex = oref->get_regex( ... ).

FIND REGEX regex IN text.

wa = oref->get_wa( ... ).

DELETE TABLE itab FROM wa.

FIND REGEX oref->get_regex( ... )

IN text.

DELETE TABLE itab

FROM oref->get_wa( ... ).

Page 27: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 27

Expressions

Expressions as arguments

CONCATENATE txt1 txt2 INTO txt.

CONDENSE txt.

txt = condense( txt1 && txt2 ).

Page 28: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 28

Expressions

Method chaining

DATA oref TYPE REF TO c1.

oref = c2=>m2( ).

oref->m1( ). c2=>m2( )->m1( ).

Page 29: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 29

Expressions

Warning

The aim is readability not obfuscation.

Do not jeopardize performance.

LOOP AT itab ASSIGNING <wa>.

IF strlen( oref->meth( <wa> ) ) < off + len.

...

... = oref->meth( <wa> ).

...

ENDIF.

ENDLOOP.

limit = off + len.

LOOP AT itab ASSIGNING <wa>.

text = oref->meth( <wa> ).

IF strlen( text ) < limit.

...

... = text.

...

ENDIF.

ENDLOOP.

Page 30: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

ExerciseRace - From statements and additions to functions and expressions

Page 31: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Selected TopicsString Processing

Page 32: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 32

String Processing

Old fashioned ABAP:

Statement based …

Requires helper variables.

Formatting with WRITE TO flat text field.

Small set of built-in describing functions.

Substring access via awkward +off(len).

Comparisons with CS, NS, CA, NA, CP, NP.

Little support for case sensitivity.

DATA timestamp TYPE timestamp.

DATA output TYPE string.

DATA buffer TYPE c LENGTH 255.

DATA date TYPE string.

DATA time TYPE string.

GET TIME STAMP FIELD timestamp.

SET COUNTRY 'US'.

WRITE timestamp TO buffer TIME ZONE sy-zonlo.

SPLIT buffer AT ` ` INTO date time.

CONCATENATE `Date:` date

`Time:` time

INTO output SEPARATED BY ` `.

Page 33: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 33

String Processing

Modern ABAP: Expressions and functions, of course …

String expressions and string templates.

Concatenation operator.

Large set of built-in string functions to be used at operand positions.

Logical functions for strings.

DATA timestamp TYPE timestamp.

DATA output TYPE string.

GET TIME STAMP FIELD timestamp.

output = |{ timestamp TIMEZONE = sy-zonlo COUNTRY = 'US ' }|.

output = |Date: { substring_before( val = output sub = ` ` ) } |

&& |Time: { substring_after( val = output sub = ` ` ) } |.

Page 34: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 34

String Processing

Bye, bye CONCATENATE …

String operator && in string expressions.

String templates in string expressions.

Built-in function concat_lines_of for internal tables.

CONCATENATE prename ` = ` `Sean` `, ` surname ` = ` `O'Connor` INTO output.

output = prename && ` = ` && `Sean` && `, ` && name && ` = ` && `O'Connor`.

output = |{ prename } = Sean, { name } = O'Connor|.

Page 35: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 35

String Processing

Bye, bye WRITE TO …

String templates with embedded expressions.

DATA output TYPE c LENGTH 255.

DATA number TYPE p LENGTH 8 DECIMALS 3 VALUE '-123.456'.

WRITE 'Number:' TO output(10) RIGHT-JUSTIFIED.

IF number < 0.

WRITE '-' TO output+11.

ENDIF.

WRITE number TO output+12 NO-SIGN LEFT-JUSTIFIED.

output = |{ 'Number:' WIDTH = 10 ALIGN = RIGHT } { number SIGN = LEFT }|.

Page 36: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 36

String Processing

String Templates

Constructing strings from literals and ABAP expressions.

Mixing static text elements with variable content

Support of control characters (like \n for newline)

|…| behave like literals: They cannot stretch over multiple lines but can be concatenated with &

Note: embedded expressions in { … } can stretch over multiple lines

... |...literal...{ expression format = ... }...literal...| ...

Literal

textEmbedded

expression

Literal

textFormat

options

Page 37: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 37

String Processing

Embedded Expressions in String Templates

Similar to WRITE TO but much more powerful

Expression can be a data object, a function call, or an operation of any elementary type

General and type specific format options govern conversion to character string (if not specified, default settings)

Result is a string that is seamlessly concatenated into template

Lots of examples in example library of the ABAP Keyword documentation

...|...{ expression format1 = ...

format2 = ...

... }...| ...

Spaces

part of text

Spaces mandatory

part of syntax

Page 38: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 38

String Processing

Bye, bye statements and offset/length access …

String functions for in-place usage.

distance, condense, concat_lines_of, escape, find, find_end, find_any_of, find_any_not_of, insert, repeat,

replace, reverse, segment, shift_left, shift_right, substring, substring_after substring_from, substring_before,

substring_to, to_upper, to_lower, to_mixed, from_mixed, translate - contains, contains_any_of,

contains_any_not_of, matches

Lots of examples in example library of the ABAP Keyword documentation

Do this in old fashioned ABAP? Better don’t think about it!

html = `<title>This is the <i>Title</i></title>`.

repl = `i`.

html = replace( val = html

regex = repl && `(?![^<>]*>)`

with = `<b>$0</b>`

occ = 0 ). → "<title>Th<b>i</b>s <b>i</b>s the <i>T<b>i</b>tle</i></title>"

Page 39: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 39

String Processing

Regular expressions

State-of-the-art pattern matching.

Usable in FIND, REPLACE, many built-in functions, and with CL_ABAP_REGEX, CL_ABAP_MATCHER

Extensive and powerful syntax (note: * is not the usual wildcard character …)

Thoroughly documented in keyword documentation, examples in example library

Playgrounds: Programs DEMO_REGEX and DEMO_REGEX_TOY

Go exploring!

Page 40: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

ExerciseStunts – String processing can be fun

Page 41: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Selected TopicsSecondary Keys for Internal Tables

Page 42: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 42

Secondary Keys

Using modern ABAP also means applying latest features to existing programs

Boost your internal table accesses by using secondary keys

Secondary keys can be easily integrated in existing and new coding to improve the performance

Few syntax additions to define and use secondary keys

Syntax check support by redundancy warnings and performance hints

Performance of existing programs can easily be improved by supplementing secondary keys

Automatic delta management by lazy and delayed update handling

DATA ... TYPE SORTED TABLE OF ...

WITH NON-UNIQUE KEY ... ...

WITH UNIQUE HASHED KEY name COMPONENTS ... ...

LOOP AT ... USING KEY name

...

ENDLOOP.

Page 43: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 43

Secondary Keys

Traditional ABAP – three table kinds, two of them supporting key access

Standard tables have a primary key, but key access is never optimized, but always linear (O(n)) .

For sorted tables and hashed tables access to primary key is optimized (O(log n) and O(1)) .

Optimized access is restricted to usage of primary key fields

Uniqueness can be defined only for fields of the primary key

DATA itab TYPE HASHED TABLE OF ...

WITH UNIQUE KEY col1 col2.

...

READ TABLE itab WITH KEY col3 = ... col4 = ...

tim

e

size

O(n) O(log n) O(1)

Page 44: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 44

Secondary Keys

Modern ABAP – three table kinds, each of them supporting two kinds of secondary keys

Secondary key access is always optimized

Sorted secondary keys (O(log n)), unique and non-unique, managed by secondary index

Hashed secondary keys (O(1)), only unique, managed by hash algorithm

Secondary keys enable:

Different key accesses to one internal table

Optimized key access to standard tables

Index access to hashed tables

Each secondary key is declared with a name and used by the name.

In the statements for accessing internal tables, you explicitly specify the key to be used.

No optimizer as for database tables – the default key is always the primary key.

The primary key also has a name now: primary_key.

Page 45: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 45

Secondary Keys

Declaring secondary keys in programs

DATA itab TYPE HASHED TABLE OF spfli WITH UNIQUE KEY carrid connid

WITH NON-UNIQUE SORTED KEY cities COMPONENTS cityfrom cityto

WITH NON-UNIQUE SORTED KEY departure COMPONENTS deptime.

DATA itab TYPE HASHED TABLE OF spfli

WITH UNIQUE KEY primary_key COMPONENTS carrid connid

WITH NON-UNIQUE SORTED KEY cities COMPONENTS cityfrom cityto

WITH NON-UNIQUE SORTED KEY departure COMPONENTS deptime.

=

Syntactical sugar allows you to use similar syntax for good old primary key.

Page 46: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 46

Secondary Keys

Declaring secondary keys for table types

in the ABAP Dictionary

Same possibilities and same usage

as for declaration in programs.

Page 47: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 47

Secondary Keys

Usage

Additions for internal table statements

… WITH TABLE KEY ... COMPONENTS …

… USING KEY …

READ TABLE itab

WITH TABLE KEY name

COMPONENTS col1 = ...

col2 = ...

READ TABLE itab

WITH KEY name

COMPONENTS col1 = ...

col2 = ...

DELETE TABLE itab

WITH KEY name

COMPONENTS col1 = ...

col2 = ...

LOOP AT itab ... USING KEY name.

...

ENDLOOP

... INDEX ... USING KEY name ...

... FROM wa USING KEY name ...

Page 48: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 48

Secondary Keys

Proof of concept

DATA itab LIKE SORTED TABLE

OF tline

WITH NON-UNIQUE KEY primary_key

COMPONENTS col1

WITH UNIQUE HASHED KEY secondary_key

COMPONENTS col2.

DO lines TIMES.

tline-col1 = rand->get_next( ).

tline-col2 = sy-index.

INSERT tline INTO TABLE itab.

ENDDO.

DO lines TIMES.

READ TABLE itab

WITH KEY col2 = sy-index

TRANSPORTING NO FIELDS.

ENDDO.

DO lines TIMES.

READ TABLE itab

WITH KEY secondary_key

COMPONENTS col2 = sy-index

TRANSPORTING NO FIELDS.

ENDDO.

Page 49: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 49

Secondary Keys

Recommendation

Usage of a secondary key causes slightly increased memory consumption and costs for key update

Unique secondary keys are updated directly

Non-unique secondary keys have a lazy update

Optimal usage scenario for secondary keys

Large tables

No or only few modifications after initial build-up phase

If mainly fast read access is required: non-unique sorted secondary keys (applicable also to existing tables)

If data integrity (uniqueness) is important: hashed secondary keys (applies also for small tables)

Secondary keys should not be used

For small tables (less than 50 lines) because of administrative and memory overhead

If modifications dominate the table processing

Page 50: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

ExerciseBoosting internal table access

Page 51: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Selected TopicsExact move

Page 52: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 52

Validation Of Field Contents

Traditional ABAP

Conversion rules for all 144 possible assignments between elementary types except two (d and t).

Philosophy: No exceptions!

Comfortable, but also robust?

DATA numbers TYPE n LENGTH 8.

...

numbers = ‘4 Apples + 2 Oranges‘.

...

METHOD meth.

"IMPORTING value(i_birthday) TYPE clike

DATA l_birthday TYPE d.

DATA msg TYPE string.

l_birthday = i_birthday.

msg = |Your age is { floor(

( sy-datlo - l_birthday ) / 365 ) - 1 }|.

MESSAGE msg TYPE 'I'.

ENDMETHOD.

Page 53: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 53

Validation Of Field Contents

Modern ABAP → robust ABAP

MOVE EXACT, exception if

Invalid value in source before move

Loss of information during move

Invalid value in target after move

DATA numbers TYPE n LENGTH 8.

...

TRY.

MOVE EXACT text TO numbers.

CATCH cx_sy_conversion_error.

...

ENDTRY.

...

METHOD meth.

DATA l_birthday TYPE d.

DATA msg TYPE string.

TRY.

MOVE EXACT i_birthday TO l_birthday.

msg = ...

CATCH cx_sy_conversion_no_date.

msg = 'Illegal date'.

ENDTRY.

MESSAGE msg TYPE 'I'.

ENDMETHOD.

Page 54: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Selected TopicsOpen SQL Locators and Streaming

Page 55: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 55

Traditional ABAP AS ABAP

Lobs in Database Tables

... ...

key1 ← ... lob ... →

key2 ← ... lob ... →

DATA xstr TYPE xstring.

SELECT SINGLE lob

FROM dbtab

INTO xstr

WHERE key = key1.

UPDATE dbtab

SET lob = xstr

WHERE key = key2.

xstr

← ... lob ... →

Page 56: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 56

Modern ABAP – Locators as lob pointers AS ABAP

Lobs in Database Tables

... ...

key1 ← ... lob ... →

key2 ← ... lob ... →

DATA locator TYPE REF TO cl_abap_db_c_locator.

SELECT SINGLE lob

FROM dbtab

INTO locator

WHERE key = key1.

UPDATE dbtab

SET lob = locator

WHERE key = key2.

locator->close( ).

locator

Page 57: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 57

Modern ABAP – sequential lob processing with streams AS ABAP

Lobs in Database Tables

... ...

key1 ← ... lob ... →

... ...

DATA locator TYPE REF TO cl_abap_db_x_reader.

SELECT SINGLE lob

FROM dbtab

INTO reader

WHERE key = key1.

WHILE reader->data_available( ) = abap_true.

TRANSFER reader->read( len ) TO file.

ENDWHILE.

reader->close( ).

reader/writer

segment

Page 58: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 58

Lobs in Database Tables

Locators vs. Streams

Scenario Locators Streams

Copying X

Searching X

Sequential processing X

Access to substring X

Access to whole LOB X

Memory reduction on AS X X

Page 59: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

ExerciseCool down – lob locating and streaming

Page 60: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

Outlook

Page 61: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 61

The Future of ABAP

TYPES t_itab TYPE TABLE OF i

WITH EMPTY KEY.

LOOP AT

VALUE t_itab( ( 1 ) ( 2 ) ( 3 ) )

INTO DATA(wa).

...

ENDLOOP.

DATA: time_stamp TYPE utcsecond,

date TYPE dtday.

time_stamp = utcsecond_current( ).

date = dtday_current( sy-zonlo ).

me->meth( xstring`FFFF` ).

More functions and expressions

Deprecation of obsolete elements

Support of future technologies

Page 62: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 62

Further Information

SAP Public Web:

SAP Developer Network (SDN): www.sdn.sap.com

ABAP Documentation

http://help.sap.com/abapdocu_702/en/index.htm

Related Workshops/Lectures at SAP TechEd 2011

CD268, Secure ABAP Programming, Workshop

Book

http://www.sap-press.com/products/Official-ABAP-Programming-Guidelines.html

Page 63: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

FeedbackPlease complete your session evaluation.

Be courteous — deposit your trash,

and do not take the handouts for the following session.

Page 65: CD266-Modern ABAP Programming- Speaking the ABAP Language Today

© 2011 SAP AG. All rights reserved. 65

No part of this publication may be reproduced or transmitted in any form or for any purpose

without the express permission of SAP AG. The information contained herein may be

changed without prior notice.

Some software products marketed by SAP AG and its distributors contain proprietary

software components of other software vendors.

Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft

Corporation.

IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x,

System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer,

z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server,

PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER,

OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP,

RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX,

Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered

trademarks of IBM Corporation.

Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.

Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or

registered trademarks of Adobe Systems Incorporated in the United States and/or other

countries.

Oracle and Java are registered trademarks of Oracle and/or its affiliates.

UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.

Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are

trademarks or registered trademarks of Citrix Systems, Inc.

© 2011 SAP AG. All rights reserved.

HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World

Wide Web Consortium, Massachusetts Institute of Technology.

SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer,

StreamWork, and other SAP products and services mentioned herein as well as their

respective logos are trademarks or registered trademarks of SAP AG in Germany and other

countries.

Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports,

Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and

services mentioned herein as well as their respective logos are trademarks or registered

trademarks of Business Objects Software Ltd. Business Objects is an

SAP company.

Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, and other Sybase

products and services mentioned herein as well as their respective logos are trademarks or

registered trademarks of Sybase, Inc. Sybase is an SAP company.

All other product and service names mentioned are the trademarks of their respective

companies. Data contained in this document serves informational purposes only. National

product specifications may vary.

The information in this document is proprietary to SAP. No part of this document may be

reproduced, copied, or transmitted in any form or for any purpose without the express prior

written permission of SAP AG.