Skip to content
Jun 13 15

ABAP Dynamic Programming – Part 5

by admin
Recommend This Post! Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest

Tony CecchiniAnthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 17 years of experience in SAP R/3 business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at ajcecchini@itpsap.com

 

ABAP Dynamic Programming Techniques

This will be the final post in this series on ABAP Dynamic Programming, but it’s a doozy!

Let’s set the stage, suppose most, if not all, of the information you need to write a particular subroutine or program is available only at runtime. For instance, you want to develop a utility for migrating legacy data. In situations like this, where the data structures are unknown and must be created at runtime, program generation and execution may be your only option.

Program Generation

Program code generation is considered to be the highest level of dynamic programming because the source code is created at runtime and all ABAP features can be used independently of input parameters. But, please be aware that this is an expensive and difficult option. The code to generate programs dynamically is very complex and hard to maintain. If you adopt this approach, you still have choices to make. ABAP supports two types of runtime program generation — transient and persistent.

Transient Program Code Generation

In ABAP, you can generate programs at runtime and execute them. To generate a program, the source code must be created at runtime. The source code is then passed to special ABAP commands that generate and execute the program.

You have two options for generating a program during runtime. The difference between them is the lifetime of the generated program. Transient code generation means that the generated program exists only as long as the internal mode exists. When the internal mode is finished, all transient generated programs are deleted. Persistent code generation means that the generated program is stored permanently in a database (until you delete it).

Generating code at runtime is typically used for handling dynamic data. It is possible to generate very efficient programs for special data at runtime. However, be aware of the following disadvantages:

(1) Generating a program at runtime is very time consuming and memory-intensive.

(2) Generated programs are hard to debug.

(3) There are no static checks or scans (such as scanning for the use of critical ABAP commands in a system) for programs that are generated at runtime.

(4) Generated programs are not fully supported by the ABAP Workbench (e.g., there is no Where-Used list available).

(5) Special services, such as the connection to the Transport Organizer, have to be implemented manually.

The above caveats aside, the general rule should be obvious: do not generate programs unless you have no other option. One exception is working with Open SQL, as not all of its constructs support dynamic features. Please check SAP help for a complete list. OK, a transient-generated program is called a subroutine pool. Subroutine pools cannot be called directly, and you can only call forms in a subroutine pool. In addition, there is a maximum limit of subroutine pools per internal mode.

Lets see how to generate a subroutine pool and execute a form in it! Take a look at the code below… read more…

Recommend This Post! Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest
May 20 15

ABAP Dynamic Programming – Part 4

by admin
Recommend This Post! Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest

 

Tony CecchiniAnthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 20 years of experience in SAP R/3 business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at ajcecchini@itpsap.com.

 

ABAP Dynamic Programming Techniques

OK, lets pick up from last month where I promised we would look at how to use the Runtime Type Identification (RTTI) technique to get information at runtime about data that is passed with generic types. So lets get started…

Runtime Type Identification (RTTI)

Runtime Type Identification (RTTI) is a powerful technique for obtaining all information about a data type at runtime. When you use dynamic programming techniques, sometimes you need to dynamically determine the data type or properties in order to decide how to handle the data. This situation typically occurs when you use generic types, where you need to obtain at runtime the missing data characteristics not described by the generic type. For example, if you use the generic type ANY in a subroutine, you will need to get information at runtime about the type of the data being passed.

RTTI is implemented in ABAP Objects using description objects. Description objects for types are created from description classes, and every type in the ABAP hierarchy has a corresponding description object. Next, every description class has special attributes and appropriate navigation methods. For instance, the class CL_ABAP_TABLEDESCR has an attribute TABLE_KIND and a navigation method GET_TABLE_LINE_TYPE.

In the RTTI class hierarchy. As you can see below, the class CL_ABAP_TYPEDESCR is the root class, which contains all methods to derive a description object from a data type or the name of the type.

RTTI Class Hierarchy read more…

Recommend This Post! Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest
Apr 19 15

ABAP Dynamic Programming – Part 3

by admin
Recommend This Post! Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest

Tony CecchiniAnthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 20 years of experience in SAP R/3 business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at ajcecchini@itpsap.com.

 

ABAP Dynamic Programming Techniques

OK, lets pick up from last month where I promised we would look at how to create a Dynamic Where clause for our OPEN SQL. Before we start diving into this, I want to give you a little context about “Tokens”. No No, not the kind you use for the subway! What are Tokens in ABAP? Lets go right to the help for an ABAP Tutorial!

ABAP Statements

ABAP statements consist of the following tokens and end with a period (.).

- ABAP words
- Operands
- Operators

Certain ABAP words, operands and operators form

- expressions,

which can be specified at certain operand positions.

The tokens of a statement must be separated by at least one blank or a line break. Otherwise, blanks and line breaks between tokens are not significant. An ABAP statement is not restricted to a line in the source text.

No distinction is made between upper and lowercase letters. Apart from ABAP words, operands and operators, you can also use the following special characters:

- If a number of expressions of the same type with operators are linked to an expression, the priority of the individual operations can be defined usig round brackets (()).
– For the purpose of calling functions and methods, round brackets (()) can sometimes be used.
– Lists of operands are expressed by round brackets (()) and commas (,) in certain positions.
– When forming a chained statement, a colon (:) and commas (,) can be used.

A number of free-standing special characters, such as round brackets for setting the priority, need to be separated from other tokens by an empty character. Other special characters – as well as the period at the end – do not have to be separated by an empty character.

Example

ABAP statement with the keyword DELETE, the addition WHERE, the operators =, <, >, AND, OR, the operands itab, col1, op1, col2, op2, col3, op3and round brackets.

DELETE itab 
  WHERE ( col1 = op1 AND ( col2 > op2 OR col3 < op3 ) ).

Now that we understand what a Token is, we can see the WHERE Clause has tokens in it, but so do other ABAP statements. So instead of focusing on just a Dynamic Where Clause, let broaden the discussion to DYNAMIC TOKENS in ABAP.

Dynamic Token Specification

Most ABAP statements allow you to specify some part of the statement dynamically. Essentially, this means you can supply various components of ABAP statements at runtime with a common syntax (see help above). The best way to understand this concept is via an example: Suppose you have an internal table, which must have a specified sort order. The name of the component to be used for sorting the internal table could be specified at runtime and stored in either a character field or string. Dynamic token specification is the technique you would use to meet this requirement. Take a look at the code below.

* dynamic sort
name = 'SSN'.
SORT itab BY (name).

* static sort
SORT itab BY SSN.

read more…

Recommend This Post! Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest
Mar 15 15

ABAP Dynamic Programming – Part 2

by admin
Recommend This Post! Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest

Tony CecchiniAnthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 20 years of experience in SAP R/3 business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at ajcecchini@itpsap.com.

 

ABAP Dynamic Programming Techniques

OK, lets pick up from last month where we attempted to accurately define what Dynamic Programming. We said … Dynamic programming is the use of special programming constructs that enable you to use selected features of data and/or operations at runtime that cannot, for various reasons, be determined at compile time.

Lets keep this definition in mind as we begin this discussion on DYNAMIC Programming Techniques.

Using a Dynamic Table Names

Let begin by creating a very simple program that will display table names to the user and when clicked on, the user is presented with the number of rows in the table. Look at the code below

REPORT ztony.
DATA: tabname  TYPE tabname,
      count    TYPE i.

START-OF-SELECTION.
WRITE: / 'EBAN', / 'MARA', / 'VBAK'.

AT LINE-SELECTION.
READ CURRENT LINE LINE VALUE INTO tabname.
SELECT COUNT(*) FROM (tabname) INTO count.
WRITE: 'The table', tabname(7), 'contains', count, 'entries.'.

We have manufactured the “Requirement” that the user will NOT KNOW THE TABLE NAME at compile time, but will at runtime. This is exactly what our definition above states will require us to make use of Dynamic Program Techniques.

The program, at the event line-selection, will take the content of the selected line and read it into the field tabname. In the SELECT statement, the name of the database table is not specified statically. Instead, the clause from (tabname), denotes that the name of the database table is to be read from the variable tabname and substituted into the SELECT statement. So when the SELECT statement is executed, the field tabname is parsed for a valid name of a database table.  If tabname doesn’t contain a valid name, an exception occurs.

OK, lets keep evolving this little program… let’s say instead of the total count of records, we actually want the rows returned. OK, now we are talking about SELECTING rows into a work-area. But wait! We don’t know which table the user will choose, so how can we TYPE the appropriate work-area? We can, trust me. First we need to learn a little about Generic Types and Field-Symbols. But, I’ll even go up a level of abstraction and ask, what the heck is a TYPE?

ABAP Types

ABAP is a hybrid programming language with a type system that reflects both the runtime-event-oriented and the object oriented programming models. Object types (e.g.,classes and interfaces) and value types (e.g., structures and tables) are integrated into one type system and share the same namespace.

Think of types as blueprints for a house. Blueprints provide descriptions of houses. Types are just descriptions of data. This analogy provides an easy way to distinguish between types and data.

Generic Types

A type is defined as “a set of properties that apply to all data of this type,” while data is a sequence of bytes in the memory of the program. A type that contains the complete description required to create data is called a concrete type. In contrast, a generic type is only a partial description of data — for example, the length is not defined within the type. Since additional information is needed to fully describe the data, you cannot create data from a generic type.

Well by using Generic Data Types like ANY, or DATA we can create DATA REFERENCES. We can construct an appropriate work area via the command CREATE DATA. It allows you to create data objects with a dynamically specified type at runtime.

But before we start coding, we need to understand another ABAP construct, the Field-Symbol (cue the music..Da..Da…Da..Daaaaa) read more…

Recommend This Post! Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest
Feb 16 15

ABAP Dynamic Programming – Part 1

by admin
Recommend This Post! Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest

Tony CecchiniAnthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 20 years of experience in SAP R/3 business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at ajcecchini@itpsap.com.

 

ABAP Dynamic Programming Techniques

We have all seen it…..

Dynamic ABAP


Some of us have been around 20+ years and still don’t REALLY understand it. Imagine how a fresher feels when they encounter this form of code. Now I can hear the old timers here screaming it just NOT necessary to code this way and what does it buy you anyway? Well, this is exactly what I intend to look at in this series. All manner of DYNAMIC ABAP and DYNAMIC OPEN SQL. If nothing else it will be a good place for freshers to look when they encounter this madness… lets go!

Dynamic Programming Concepts and Benefits

What exactly is ABAP dynamic programming, and why is it so beneficial? Fundamentally, dynamic programming is used in almost every program, but the extent varies. Most programs consist of both dynamic and static parts. In this sense, programs are rarely black and white — most are shades of gray. Perhaps the best way to answer that question is to contrast dynamic with static programming.

In this context, dynamic and static apply to the data and/or operations performed on the data within a program. Static properties of data or operations are those characteristics that are known and fixed at compile time. They are explicitly defined in the program source code and will not change during runtime. In contrast, dynamic properties of data or operations are the characteristics that are not known or fixed at compile time. Instead, they are defined by the values of parameters that are passed to the program at runtime. read more…

Recommend This Post! Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on Pinterest