Sunteți pe pagina 1din 3

Making Powerful ABAP Applications Using Dynamic Open SQL

Below is a snippet from one of hundreds of articles available to ERPtips subscribers.


If you would like a complimentary copy of the full article, please email
Mark.Downs@ERPtips.com
(include the title of the article in your email)

To subscribe to ERPtips, go to
www.ERPtips.com/Subscribe.asp.

ERPtips Journal is published by Klee Associates, Inc.


ERPtips University provides both public and onsite training for SAP clients.
For more about ERPtips University, including the current schedule, click here:
www.ERPtips.com/WorkshopSchedule.asp

Making Powerful ABAP Applications Using Dynamic Open SQL

How often do you find that your department is calling in a consultant in order to make business
process changes to a custom-built SAP application? We know it happens frequently, and it adds
up quickly. But before you shrug and say, “it`s unavoidable,” you need to read Rehan Zaidi`s latest articl

Click here to read this Snippet


SAPtips4On ABAP/J2EE
Making Powerful ABAP Applications
Page 

April / May 2007 Volume V Issue 2

Using Dynamic Open SQL –


A Guide for SAP® Developers and Consultants
By Rehan Zaidi, Siemens Pakistan
Editor’s Note: How often do you • What are some of the dynamic pro- find it useful. I will assume that the
find that your department is call- gramming options available within reader is familiar with ABAP and
ing in a consultant in order to make the SQL terrain? SQL statements .For more informa-
business process changes to a cus- tion, refer to the SAP documentation
tom-built SAP application? We know • How can we incorporate these on http://help.sap.com. The Dynamic
it happens frequently, and it adds up options in our applications? Open SQL techniques described
quickly. But before you shrug and in this article can be used in SAP
say, “it’s unavoidable,” you need to Release 4.6 and onwards.
read Rehan Zaidi’s latest article on
Dynamic Open SQL. Rehan presents Dynamic Open SQL –
a compelling case for taking some An Overview
extra steps in the initial develop- There are many Dynamic Open SQL is an exten-
ment of your applications that will sion of Open SQL that allows you
save you time and money down the situations in which to write generic database access/
road when business needs dictate IT modification code. This is done by
changes. certain parts of a specifying certain parts (clauses) of
the SQL statements in the form of
SQL statement may dynamic elements or tokens. The
Introduction dynamic portion of the statement is
SAP customers hire consultants in not be determined denoted by ABAP variables specified
order to accommodate business pro- in parenthesis.
cess changes in their custom-built at the time of
applications. However, if certain There are many situations in which
measures are taken at the time of program writing. certain parts (or parameters) of a
initial development, this extra cost SQL statement may not be deter-
incurred and (any associated time mined at the time of program writ-
waste) may be avoided. One of the ing. These may include the names of
ways that SAP lets you achieve this the database tables, their fields, or
is with the application of Dynamic the logical condition upon which the
SAPtipsJournal

Open SQL. Due to the numerous We’ll also discuss the clauses or data is to be fetched. In such cases,
advantages that Dynamic Open SQL parts of SQL statements that may the known parameters are written
provides to customers, learning the be specified dynamically, as well as statically, whereas the unknown por-
basics of this technique is essential highlight the importance of addition- tion is specified using suitable ABAP
for developers and consultants. al code that should be used in con- variables. At the time of program
junction with Dynamic Open SQL. execution, the SAP system carries out
The goal of this article is to provide Finally, I will use an easy-for-all the parsing of the dynamic portion
a detailed description of Dynamic company scenario to illustrate how of the SQL statement and combines
Open SQL and the benefits it pro- this works, with, of course, any tips it with the static portion in order to
vides. These are some of the ques- learned from my experience. execute the entire SQL statement.
tions that this article will address.
This article is primarily intended As compared to statements speci-
• What is Dynamic Open SQL and for ABAP developers, but SAP users fied statically, we need to write extra
why is it necessary? who regularly work with developers coding prior to the SQL statements
to accommodate relevant flexibility whose components are to be deter-
features in their programs may also mined at runtime. This coding

SAPtips.com SAPtips © 2007 Klee Associates, Inc.


SAPtips4On ABAP/J2EE
Page 

April / May 2007 Volume V Issue 2

a) first creates the additional ABAP determining the number of rows of a • In the first step a variable, MYREF,
variables to be used in the SQL table, the following static statement is defined as:
statement, may be used:
DATA: MYREF TYPE REF TO DATA.
b) then, creates the Dynamic Open SELECT COUNT(*) FROM SPFLI INTO MYNUM.
SQL code construct, This is used for storing the refer-
The above statement specifically ence to the data object.
c) and then stores this code in the counts the total records in the data-
defined ABAP variables. base table SPFLI. However, if you • Next, a working area corresponding
need to write a program for any data- to the dynamic table name stored
The art of Dynamic Open SQL base table that the user specifies, the in MYTABLE is created:
programming lies in combining these following Dynamic Open SQL state-
SQL statements with some elegant ments will be required: CREATE DATA MYREF TYPE (MYTABLE).
ABAP programming logic in order to
write powerful generic code. Dynam- DATA: MYTABLE(20). This statement lets you make
ic Open SQL provides a number of SELECT COUNT(*) FROM (MYTABLE) INTO MYREF refer to the work area.
advantages to SAP developers and MYNUM.
consultants: • You’ll then define a field symbol
Note: The variable that refers to the that should point to your work
• The code is more meaningful and dynamic part of the SQL statement is area. This assignment is made via
compact. specified in parenthesis, for example an ASSIGN statement:
• No additional resources are used (MYTABLE).
for generating the source code. FIELD-SYMBOLS : <ONERECORD> TYPE
At runtime, the variable MYTABLE ANY.
• All appropriate syntax checks are is populated with the name of the ASSIGN MYREF->* TO <ONERECORD> .
performed, and the program may table that you are interested in. The
be debugged very easily. SELECT statement is then used to • The SELECT statement is finally
read the number of rows in the table used to read data row by row from
Since SAP Release 4.6, the ABAP and store the value in the MYNUM the table in question, with the help
language provides a stable set of variable. of the work area pointed by field
features pertinent to Dynamic Open symbol ONERECORD:
SQL. In the SAP Web Application Dynamically Specifying
Server 6.10, the Dynamic Open SQL Parts of the SELECT state- SELECT * FROM (MYTABLE) UP TO 100 ROWS
capabilities of ABAP were further ment INTO <ONERECORD>.
improved. Some of the statements As already mentioned, a number ……
(and their clauses) that may be of clauses of the SELECT statement .......
specified dynamically are shown in may be specified dynamically. Let’s ENDSELECT.
SAPtipsJournal

Figure 1. look at them individually.


From Release 6.10, you may also
Statements Components (clauses ) 1) Dynamic FROM and INTO declare entire tables based on run-
Clause time data (i.e., an internal table hav-
SELECT S ELECT, FROM, WHERE, The FROM and INTO clauses let ing a line type specified at runtime).
GROUP BY and HAVING
you access data records from data- The steps required in this case are as
UPDATE WHERE, FROM base tables and store them into suit- follows:
able data variables. The main dif-
INSERT WHERE, FROM
ficulty with Dynamic Open SQL, in • A field symbol and a data refer-
DELETE WHERE, FROM this case, lies in declaring an area in ence, along with the table name,
which the data is to be stored. A pow- are declared as shown below:
Figure 1: Statements and Components That May Be erful method for creating a dynamic
Dynamically Specified
working area is to use the CREATE DATA : MYTAB TYPE TABNAME.
To better understand the working DATA statement. DATA : MYREF TYPE REF TO DATA.
of Dynamic Open SQL constructs, FIELD-SYMBOLS : <MYTABLE> TYPE ANY
let’s consider a simple example of a This requires a few steps: TABLE.
Dynamic Open SQL statement. For

SAPtips.com SAPtips © 2007 Klee Associates, Inc.

S-ar putea să vă placă și