[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 thecarridandconnidfields, 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 yourSELECTstatement.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 TABLEin 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 yourSELECTstatement 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
WHEREconditions - The logical operators in
WHERE - Casting in
WHERE - The
INoperator - Range table
- The dynamic
WHEREcondition SELECT SINGLE FOR UPDATEGROUP BYORDER BYUP TO N ROWSSELECTandENDSELECTFOR 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 theWHEREcondition, we can make it a requirement that fields need to have some values. The important thing here is that when we haveSTRINGorRAWSTRINGplusLCHRandLRAW, we cannot use this field inWHERE. CreatingWHEREwith themandtfields is not possible, as the client automatically handles this, but this automatization can be deactivated usingCLIENT SPECIFIEDafter theFROMclause. However, this is only the most basic possibility of theWHEREcondition.InSELECT, we can use dynamic parameters. When creating the code with the exact values of theWHEREcondition, 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 incarridare the same as values in thegv_carridvariable.We can also use other operands as follows:EQor=: 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 theSELECTstatement'scarridandconnidparameters, wherecarridis equal toAA.NEor<>: This operand selectsGet all datawhen 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, allcarridandconnidfields are selected, except those in whichcarridis equal toAA.LTor<: This operand selectsGet all datawhen 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, thecarridandconnidfields are selected only ifcarridis smaller thanAA.GTor>: This operand selectsGet all datawhen 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, thecarridandconnidfields are selected only ifcarridis greater thanAA.LEor<=:SELECTgets 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, thecarridandconnidfields are selected only ifcarridis less than, or equal to,AA.GEor>=:SELECTgets 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, thecarridandconnidfields are selected only ifcarridis greater than, or equal to,AA.In theWHEREcondition, we can also use theBETWEENcondition:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid BETWEEN 'AA' AND 'DL'.The result of thatSELECTstatement is to get all thecarridandconnidfields wherecarridis inclusive between'AA'and'DL'.We can get the same result if we useSELECTlike 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 theLIKEoperator, which may be helpful for getting data from a table, using only parts of searched words. The operator is built as follows:LIKE 'XXX_', whereXXXstands for the part of the word we are looking for,and _stands for any other set of symbols.XXXand (_) can be used in any order. In the given example in the preceding code snippet,SELECTgets all thecarridvalues when the values start withAND. In this case, the second character may be any letter.The following is an example ofLIKEinSELECT:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE carrid LIKE 'A_'.In the results, we get two differentcarridparameters. 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 theWHEREconditions can also be usedwith theNOTaddition.For instance, we can useNOT BETWEENto select where we get all data from the table, excluding data with selected fields that are not in range.The next operator inWHEREisIN. TheINoperator is used to create ranges in aSELECTquery. 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 wherecarridis'AA','DL', or'LH'.In theINoperator, 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 inWHEREbased 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 - HIGHIn theSIGNfield we can inputIorE. If we inputI, values of this row will be included in the selection. If we inputE, the value will not be included.In theOPTIONfield, we have the same option as inWHERE(which isEQorGE) and we can also putCPandBThere.CPis similar to theLIKEcommand, andBTis similar toBETWEEN.When we put anEQorELSEoption using only one operand, values should be inLOW.HIGHis used only when usingBETWEENin theOPTIONfield.The range table is really useful for maintaining code, which will be easier to maintain as theSELECTstatement 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_carridtable, we have the following values:SIGNOPTIONLOWHIGHIEQAAIBTDLNWIGESAEEQNGECPF_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_'.ThisWHEREcondition inSELECTis 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 theWHEREcondition, we can use the dynamicWHEREcondition. We can decide during runtime which field will be added to theWHEREcondition.The dynamicWHEREcondition is a variable typed as a string, where we have stored text that is similar to code. Let's take a look at theSELECTstatement: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 thisSELECTstatement as the following:SELECT carrid connid FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight WHERE (lv_dyn_where).lv_dyn_wherehas the following values:carrid EQ 'AA',connid EQ '0017', andfldate GT '01.01.2015'. The result of both of theseSELECTstatements is identical.TheFOR UPDATEstatement 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 UPDATEstatement causes deadlock, an exception will be raised. When we use theFOR UPDATEstatement, it is also important for theSELECTstatement to bypass SAP buffering.An example ofSELECT SINGLE FOR UPDATEis 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 BYclause combines identical content in columns specified by aGROUP BYclause, or content that has the same result in a SQL expression for a single row.TheGROUP BYclause combines identical content in columns specified by aGROUP BYclause, 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 NOTkey, andSELECT DISTINCTonly selects unique values in a selected key.ORDER BYclauses are used to sort data directly through theSELECTstatement. 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 adynamicorder, which is created on the same principle asdynamic, with different values compared with adynamicvariable:SELECT carrid connid planetype FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight ORDER BY planetype ASCENDING.UP TO (natural number) ROWSis 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 thisSELECTstatement gets one row following a single execution and the loop ends whenSELECTcannot 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 fromconnidbefore appending them to the maingt_sflighttable: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 SIZEaddition.PACKAGE SIZEis 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 ENTRIEScan 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 firstSELECTstatement, we get data aboutcarridandconnid, but only ifplanetypeis'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 ENTRIESis that we need to check before executingSELECTwithFOR ALL ENTRIES, if the table using this is not empty. If the table is empty, all records will be obtained from thespflitable in this example.Subqueries can be used in theWHEREcondition to get maximum values directly from another table. For example,SUBQUERYcan be used with the following additions:- ALL|ANY|SOMEIf usingALL, the expression is true if the comparison is true for all rows in the results set of the scalar subquery. Consequently, if theANYorSOMEaddition is used, the expression is true if it is true for at least one of the rows in the results set of the subquery:- EXISTIf using theEXISTsubquery, the expression is true if the table with a subquery contains at least one row:- INTheINoperator is working in the same way as in theWHEREcondition, 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