[SAP ABAP] Databases Part 1
INTO
: When we choose this, we can select fields to structure. An example can be found in the explanation ofSELECT SINGLE
.INTO CORRESPONDING FIELDS OF
: We can also use this addition, but, duringSELECT
, the system tries to select and match fields corresponding to data elements in a result. The differences in the results will be shown in the example.INTO [ obj1, obj2 ... ]
: We can select fields in relation to a given variable. For example, if we want to select thecarrid
andconnid
fields, we can select these fields directly in relation to a variable instead of a structure or table.INTO TABLE
: This is the same asINTO
, but we can use a table rather than structure and we can also select more than one row.INTO CORRESPONDING FIELDS OF TABLE
: This is the same asINTO CORRESPONDING FIELDS OF
, but we can select more than one row here, and we need to use the table as an internal variable where we can store the result.APPENDING
: Appending can be used when we want to append new rows instead of reHow to see data selected from a database
In order to exerciseSELECT
, we need to know which values were selected from the database. How to show data is covered in Chapter 7, Building User Interfaces. For now, I will get you a fragment of code to display data, which is selected from yourSELECT
statement.The following is an example of code to show data from an internal table:DATA: gr_alv TYPE REF TO cl_salv_table, gr_columns TYPE REF TO cl_salv_columns_table. CALL METHOD cl_salv_table=>factory IMPORTING r_salv_table = gr_alv CHANGING t_table = YOUR TABLE. gr_columns = gr_alv->get_columns( ). gr_columns->set_optimize( value = 'X' ). gr_alv->display( ).
Do you seeYOUR TABLE
in this piece of code? This is where you can input the name of your table.For instance, if you want to see data stored ingt_sflight
, you use the following code:DATA: gr_alv TYPE REF TO cl_salv_table, gr_columns TYPE REF TO cl_salv_columns_table. CALL METHOD cl_salv_table=>factory IMPORTING r_salv_table = gr_alv CHANGING t_table = gt_sflight. gr_columns = gr_alv->get_columns( ). gr_columns->set_optimize( value = 'X' ). gr_alv->display( ).
It is important to note that yourSELECT
statement must be executed first, otherwise you will not be able to see any data.- In OpenSQL in ABAP, we have more advanced possibilities. In this section, we will cover the following topics:
- The
WHERE
conditions - The logical operators in
WHERE
- Casting in
WHERE
- The
IN
operator - Range table
- The dynamic
WHERE
condition SELECT SINGLE FOR UPDATE
GROUP BY
ORDER BY
UP TO N ROWS
SELECT
andENDSELECT
FOR ALL ENTRIES
- Subqueries
There are many possibilities here. When we are familiar with them, we can write selects in effective ways while retaining the speed of the executing statement.In theWHERE
condition, we can make it a requirement that fields need to have some values. The important thing here is that when we haveSTRING
orRAWSTRING
plusLCHR
andLRAW
, we cannot use this field inWHERE
. CreatingWHERE
with themandt
fields is not possible, as the client automatically handles this, but this automatization can be deactivated usingCLIENT SPECIFIED
after theFROM
clause. However, this is only the most basic possibility of theWHERE
condition.InSELECT
, we can use dynamic parameters. When creating the code with the exact values of theWHERE
condition, we rarely know exactly what values we need. In such a case, dynamic values would be helpful. They might be found, for example, on some screens:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid EQ gv_carrid.
In this example, values incarrid
are the same as values in thegv_carrid
variable.We can also use other operands as follows:EQ
or=
: Select all data where the field is equal to the second operand:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid EQ 'AA'.
This code snippet provides an example of code that shows data from the internal table.The result is theSELECT
statement'scarrid
andconnid
parameters, wherecarrid
is equal toAA
.NE
or<>
: This operand selectsGet all data
when the field is not equal to the second operand:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid NE 'AA'.
As a result, allcarrid
andconnid
fields are selected, except those in whichcarrid
is equal toAA
.LT
or<
: This operand selectsGet all data
when the value of the field is less than the second operand:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid LT 'AA'.
As a result, thecarrid
andconnid
fields are selected only ifcarrid
is smaller thanAA
.GT
or>
: This operand selectsGet all data
when the field is greater than the second operand:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid GT 'AA'.
As a result, thecarrid
andconnid
fields are selected only ifcarrid
is greater thanAA
.LE
or<=
:SELECT
gets all of the data when the field is less than, or equal to, the second operand:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid LE 'AA'.
As a result, thecarrid
andconnid
fields are selected only ifcarrid
is less than, or equal to,AA
.GE
or>=
:SELECT
gets all of the data when the field is greater than, or equal to, the second operand:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid GE 'AA'.
As a result, thecarrid
andconnid
fields are selected only ifcarrid
is greater than, or equal to,AA
.In theWHERE
condition, we can also use theBETWEEN
condition:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid BETWEEN 'AA' AND 'DL'.
The result of thatSELECT
statement is to get all thecarrid
andconnid
fields wherecarrid
is inclusive between'AA'
and'DL'
.We can get the same result if we useSELECT
like this:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid GE 'AA' AND carrid LE 'DL'.
We can also use theLIKE
operator, which may be helpful for getting data from a table, using only parts of searched words. The operator is built as follows:LIKE 'XXX_'
, whereXXX
stands for the part of the word we are looking for,and _
stands for any other set of symbols.XXX
and (_
) can be used in any order. In the given example in the preceding code snippet,SELECT
gets all thecarrid
values when the values start withAND
. In this case, the second character may be any letter.The following is an example ofLIKE
inSELECT
:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid LIKE 'A_'.
In the results, we get two differentcarrid
parameters. The first of these is'AA'
and the second is'AZ'
.When usingLIKE
, we must have characters as a value. However, if we want to use a non-character, we can try to useCAST
.SELECT carrid, connid FROM sflight WHERE CAST( connid AS CHAR ) LIKE '00__' INTO CORRESPONDING FIELDS OF TABLE @gt_sflight.
All of theWHERE
conditions can also be usedwith theNOT
addition.For instance, we can useNOT BETWEEN
to select where we get all data from the table, excluding data with selected fields that are not in range.The next operator inWHERE
isIN
. TheIN
operator is used to create ranges in aSELECT
query. In this example, we add three values to a range. We add'AA'
,'DL'
, and'LH'
as values ofcarrid
:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid in ('AA', 'DL', 'LH' ).
In the result, we get values wherecarrid
is'AA'
,'DL'
, or'LH'
.In theIN
operator, we can also use a special internal table to describe the conditions. One of the most common uses of these possibilities is when we want to dynamically establish conditions inWHERE
based on one value, or by using ranges from the selection screen (we will go into more detail about the selection screen in Chapter 7, Building User Interfaces).The range table has four fields, and three of them need to be filled. These fields are as follows:- SIGN - OPTION - LOW - HIGH
In theSIGN
field we can inputI
orE
. If we inputI
, values of this row will be included in the selection. If we inputE
, the value will not be included.In theOPTION
field, we have the same option as inWHERE
(which isEQ
orGE
) and we can also putCP
andBT
here.CP
is similar to theLIKE
command, andBT
is similar toBETWEEN
.When we put anEQ
orELSE
option using only one operand, values should be inLOW
.HIGH
is used only when usingBETWEEN
in theOPTION
field.The range table is really useful for maintaining code, which will be easier to maintain as theSELECT
statement is shorter, as demonstrated in the following example:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid IN gt_carrid.
In thegt_carrid
table, we have the following values:SIGNOPTIONLOWHIGHI
EQ
AA
I
BT
DL
NW
I
GE
SA
E
EQ
NG
E
CP
F_
If we want to make this directly inSELECT
, it should look like this:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid EQ 'AA' AND carrid BETWEEN 'DL' AND 'NW' AND carrid GE 'SA' AND carrid NE 'NG' AND carrid NOT LIKE 'F_'.
ThisWHERE
condition inSELECT
is more complicated, and even if we have dynamic values, we cannot use more than five values with operands that have been set up in advance in this example.If we do not know what field we need in theWHERE
condition, we can use the dynamicWHERE
condition. We can decide during runtime which field will be added to theWHERE
condition.The dynamicWHERE
condition is a variable typed as a string, where we have stored text that is similar to code. Let's take a look at theSELECT
statement:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid EQ 'AA' AND connid EQ '0017' AND fldate GT '01.01.2015'.
We can also develop thisSELECT
statement as the following:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE (lv_dyn_where).
lv_dyn_where
has the following values:carrid EQ 'AA'
,connid EQ '0017'
, andfldate GT '01.01.2015'
. The result of both of theseSELECT
statements is identical.TheFOR UPDATE
statement can be used to set an exclusive lock for a selected row. However, if we have more than one entry with the same primary key, the result set will be empty. Consequently, it is really important to specify a full key. Furthermore, when ourFOR UPDATE
statement causes deadlock, an exception will be raised. When we use theFOR UPDATE
statement, it is also important for theSELECT
statement to bypass SAP buffering.An example ofSELECT SINGLE FOR UPDATE
is as follows:SELECT SINGLE FOR UPDATE carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gs_sflight WHERE carrid EQ 'AA' AND connid EQ '0017' AND fldate GT '01.01.2015'.
TheGROUP BY
clause combines identical content in columns specified by aGROUP BY
clause, or content that has the same result in a SQL expression for a single row.TheGROUP BY
clause combines identical content in columns specified by aGROUP BY
clause, or content that has the same result in a SQL expression for a single row:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight GROUP BY carrid connid.
The result of that query is as follows:As you can see, the result set is pretty much the same in that example as when we useSELECT DISTINCT
. However, inGROUP BY
, we can also specify a field with anIS NOT
key, andSELECT DISTINCT
only selects unique values in a selected key.ORDER BY
clauses are used to sort data directly through theSELECT
statement. Sorting can be carried out using a primary key:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight ORDER BY PRIMARY KEY.
It can also sort other columns in ascending or descending order, or even in adynamic
order, which is created on the same principle asdynamic
, with different values compared with adynamic
variable:SELECT carrid connid planetype FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight ORDER BY planetype ASCENDING.
UP TO (natural number) ROWS
is used for defining a limit in terms of the number of rows that can be selected inSELECT
:SELECT carrid connid planetype UP TO 10 ROWS FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight.
In this example, we will get the first 10 rows:SELECT carrid connid planetype FROM sflight INTO CORRESPONDING FIELDS OF gs_sflight. ENDSELECT.
This means that thisSELECT
statement gets one row following a single execution and the loop ends whenSELECT
cannot get the next rows with the defined key.This is helpful when we want to make changes directly to data afterSELECT
. In this example, we delete leading zeros fromconnid
before appending them to the maingt_sflight
table:SELECT carrid connid planetype FROM sflight INTO CORRESPONDING FIELDS OF gs_sflight. SHIFT gs_sflight-connid LEFT DELETING LEADING '0'. APPEND gs_sflight TO gt_sflight. ENDSELECT.
We can also get data in a loop on a database, sent directly to a table with aPACKAGE SIZE
addition.PACKAGE SIZE
is responsible for establishing how many entries need to be selected in one execution of a loop:SELECT carrid connid planetype PACKAGE SIZE 100 FROM sflight INTO CORRESPONDING FIELDS OF gt_sflight. ENDSELECT.
In this case, we get 100 rows in 1 loop.FOR ALL ENTRIES
can be used when we have two tables and we want get data from the second table based on a field in the first table:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF gt_sflight WHERE planetype = '747-400'. IF gt_sflight IS NOT INITIAL. SELECT carrid connid counryfr cityfr airpfrom countryto FROM spfli INTO CORRESPONDING FIELDS OF TABLE gt_spfli FOR ALL ENTRIES IN gt_sflight WHERE carrid = gt_sflight-carrid AND carrid = gt_sflight-connid. ENDIF.
In the firstSELECT
statement, we get data aboutcarrid
andconnid
, but only ifplanetype
is'747-400'
. In the second table, as we do not haveplanetype
, we can only getrows where we want them.One thing that we need to remember when usingFOR ALL ENTRIES
is that we need to check before executingSELECT
withFOR ALL ENTRIES
, if the table using this is not empty. If the table is empty, all records will be obtained from thespfli
table in this example.Subqueries can be used in theWHERE
condition to get maximum values directly from another table. For example,SUBQUERY
can be used with the following additions:- ALL|ANY|SOME
If usingALL
, the expression is true if the comparison is true for all rows in the results set of the scalar subquery. Consequently, if theANY
orSOME
addition is used, the expression is true if it is true for at least one of the rows in the results set of the subquery:- EXIST
If using theEXIST
subquery, the expression is true if the table with a subquery contains at least one row:- IN
TheIN
operator is working in the same way as in theWHERE
condition, but the result will be taken dynamically from the database.For instance, if we want to book a flight with the highest price, we can make the following subquery:SELECT * FROM sflight INTO CORRESPONDING FIELDS OF gt_sflight WHERE price = ( SELECT MAX( price ) FROM sflight ).
- The
Comments
Post a Comment