Posted: Fri Sep 28, 2007 10:12 am Post subject: Example: Dynamic SELECT
SELECT Specifying Columns Dynamically
You can also specify <cols> dynamically as follows:
SELECT <lines> (<itab>) ...
The parentheses must include the name of an internal table <itab> that is either empty or contains <s 1 > <s 2 > ... to specify the columns or aggregate expressions to be read. For this purpose, the line type of <itab> must be a type C field with a maximum length of 72. If the internal table is empty, the system reads all columns.
Code:
DATA: ITAB TYPE STANDARD TABLE OF SPFLI,
WA LIKE LINE OF ITAB.
DATA: LINE(72) TYPE C,
LIST LIKE TABLE OF LINE(72).
LINE = ' CITYFROM CITYTO '.
APPEND LINE TO LIST.
SELECT DISTINCT (LIST)
INTO CORRESPONDING FIELDS OF TABLE ITAB
FROM SPFLI.
IF SY-SUBRC EQ 0.
LOOP AT ITAB INTO WA.
WRITE: / WA-CITYFROM, WA-CITYTO.
ENDLOOP.
ENDIF.
The internal table ITAB contains the columns of the database table SPFLI to be read. The DISTINCT addition in the SELECT clause means that the statement only reads those lines that have different contents in both of these columns. The result is a list of possible routes.
FROM Specifying Database Tables Dynamically
To specify the name of a database table dynamically, use the following:
SELECT ... FROM (<name>) <options> ...
The field <name> must contain the name of a database table in the ABAP Dictionary. The table name must be written in uppercase. When you specify the name of a database table dynamically, you cannot use an empty INTO clause to read all of the columns into the work area <dbtab>. It is also not possible to use alternative table names.
Code:
DATA WA TYPE SCARR.
DATA NAME(10) VALUE 'SCARR'.
SELECT *
INTO WA
FROM (NAME) CLIENT SPECIFIED
WHERE MANDT = '000'.
WRITE: / WA-CARRID, WA-CARRNAME.
ENDSELECT.
A condition for the MANDT field is allowed, since the example uses the CLIENT SPECIFIED option. If NAME had contained the value ‘scarr’ instead of ‘SCARR’, a runtime error would have occurred.
WHERE
Dynamic Conditions
To specify a condition dynamically, use:
SELECT ... WHERE (<itab>) ...
where <itab> is an internal table with line type C and maximum length 72 characters. All of the conditions listed above except for selection tables, can be written into the lines of <itab>. However, you may only use literals, and not the names of data objects. The internal table can also be left empty.
If you only want to specify a part of the condition dynamically, use:
SELECT ... WHERE <cond> AND (<itab>) ...
You cannot link a static and a dynamic condition using OR.
You may only use dynamic conditions in the WHERE clause of the SELECT statement.
Code:
DATA: COND(72) TYPE C,
ITAB LIKE TABLE OF COND.
PARAMETERS: CITY1(10) TYPE C, CITY2(10) TYPE C.
DATA WA TYPE SPFLI-CITYFROM.
CONCATENATE 'CITYFROM = ''' CITY1 '''' INTO COND.
APPEND COND TO ITAB.
CONCATENATE 'OR CITYFROM = ''' CITY2 '''' INTO COND.
APPEND COND TO ITAB.
CONCATENATE 'OR CITYFROM = ''' 'BERLIN' '''' INTO COND.
APPEND COND TO ITAB.
LOOP AT ITAB INTO COND.
WRITE COND.
ENDLOOP.
SKIP.
SELECT CITYFROM
INTO WA
FROM SPFLI
WHERE (ITAB).
WRITE / WA.
ENDSELECT.
Tabular Conditions
The WHERE clause of the SELECT statement has a special variant that allows you to derive conditions from the lines and columns of an internal table:
SELECT ... FOR ALL ENTRIES IN <itab> WHERE <cond> ...
<cond> may be formulated as described above. If you specify a field of the internal table <itab> as an operand in a condition, you address all lines of the internal table. The comparison is then performed for each line of the internal table. For each line, the system selects the lines from the database table that satisfy the condition. The result set of the SELECT statement is the union of the individual selections for each line of the internal table. Duplicate lines are automatically eliminated from the result set. If <itab> is empty, the addition FOR ALL ENTRIES is disregarded, and all entries are read.
The internal table <itab> must have a structured line type, and each field that occurs in the condition <cond> must be compatible with the column of the database with which it is compared. Do not use the operators LIKE, BETWEEN, and IN in comparisons using internal table fields. You may not use the ORDER BY clause in the same SELECT statement.
You can use the option FOR ALL ENTRIES to replace nested select loops by operations on internal tables. This can significantly improve the performance for large sets of selected data.
Code:
DATA: BEGIN OF LINE,
CARRID TYPE SPFLI-CARRID,
CONNID TYPE SPFLI-CONNID,
CITYFROM TYPE SPFLI-CITYFROM,
CITYTO TYPE SPFLI-CITYTO,
END OF LINE,
ITAB LIKE TABLE OF LINE.
LINE-CITYFROM = 'FRANKFURT'.
LINE-CITYTO = 'BERLIN'.
APPEND LINE TO ITAB.
LINE-CITYFROM = 'NEW YORK'.
LINE-CITYTO = 'SAN FRANCISCO'.
APPEND LINE TO ITAB.
SELECT CARRID CONNID CITYFROM CITYTO
INTO CORRESPONDING FIELDS OF LINE
FROM SPFLI
FOR ALL ENTRIES IN ITAB
WHERE CITYFROM = ITAB-CITYFROM AND CITYTO = ITAB-CITYTO.
This is the complex program, show a Dynamic Select.
Code:
REPORT zdany_dynamic_select.
* We use some parameters to dynamically control the select, this is not very
* clever but this is just a test program !!
PARAMETER : p_tabnam TYPE tabname DEFAULT 'SFLIGHT',
p_selfl1 TYPE edpline DEFAULT 'CARRID',
p_selfl2 TYPE edpline DEFAULT 'CONNID',
p_selfl3 TYPE edpline DEFAULT 'FLDATE',
p_selfl4 TYPE edpline DEFAULT 'PRICE',
p_selfl5 TYPE edpline DEFAULT 'CURRENCY',
p_where1 TYPE edpline DEFAULT 'PRICE > 300',
p_where2 TYPE edpline DEFAULT 'AND CURRENCY = ''EUR'''.
FIELD-SYMBOLS : <lt_outtab> TYPE ANY TABLE,
<ls_outtab> TYPE ANY,
<l_fld> TYPE ANY.
DATA: lt_where TYPE TABLE OF edpline,
lt_sel_list TYPE TABLE OF edpline,
lt_group TYPE TABLE OF edpline,
l_having TYPE string,
l_wa_name TYPE string,
l_sel_list TYPE edpline,
dref TYPE REF TO data,
itab_type TYPE REF TO cl_abap_tabledescr,
struct_type TYPE REF TO cl_abap_structdescr,
elem_type TYPE REF TO cl_abap_elemdescr,
comp_tab TYPE cl_abap_structdescr=>component_table,
comp_fld TYPE cl_abap_structdescr=>component.
TYPES: f_count TYPE i.
* Creation of the output table including a non standard field, f_count
* see ABAP FAQ #14 for more information on this topic
struct_type ?= cl_abap_typedescr=>describe_by_name( p_tabnam ).
elem_type ?= cl_abap_elemdescr=>describe_by_name( 'F_COUNT' ).
comp_tab = struct_type->get_components( ).
* We remove the unnecessary fields
LOOP AT comp_tab INTO comp_fld.
IF comp_fld-name <> p_selfl1 AND
comp_fld-name <> p_selfl2 AND
comp_fld-name <> p_selfl3 AND
comp_fld-name <> p_selfl4 AND
comp_fld-name <> p_selfl5.
DELETE TABLE comp_tab WITH TABLE KEY name = comp_fld-name.
ENDIF.
ENDLOOP.
comp_fld-name = 'F_COUNT'.
comp_fld-type = elem_type.
APPEND comp_fld TO comp_tab.
l_wa_name = 'l_WA'.
CREATE DATA dref TYPE HANDLE itab_type.
ASSIGN dref->* TO <lt_outtab>.
CREATE DATA dref TYPE HANDLE struct_type.
ASSIGN dref->* TO <ls_outtab>.
* Creation of the selection fields and the "group by" clause
APPEND p_selfl1 TO lt_sel_list.
APPEND p_selfl1 TO lt_group.
APPEND p_selfl2 TO lt_sel_list.
APPEND p_selfl2 TO lt_group.
APPEND p_selfl3 TO lt_sel_list.
APPEND p_selfl3 TO lt_group.
APPEND p_selfl4 TO lt_sel_list.
APPEND p_selfl4 TO lt_group.
APPEND p_selfl5 TO lt_sel_list.
APPEND p_selfl5 TO lt_group.
APPEND 'COUNT(*) AS F_COUNT' TO lt_sel_list.
* creation of the "where" clause
APPEND p_where1 TO lt_where.
APPEND p_where2 TO lt_where.
* Creation of the "having" clause
l_having = 'count(*) >= 1'.
* THE dynamic select
SELECT (lt_sel_list)
FROM (p_tabnam)
INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>
WHERE (lt_where)
GROUP BY (lt_group)
HAVING (l_having)
ORDER BY (lt_group).
* display of the results
LOOP AT <lt_outtab> ASSIGNING <ls_outtab>.
LOOP AT comp_tab INTO comp_fld.
ASSIGN COMPONENT comp_fld-name OF STRUCTURE <ls_outtab> TO <l_fld>.
WRITE: <l_fld>.
ENDLOOP.
SKIP.
ENDLOOP.
Использование RANGES
Выбрать все записи, в которых Company Code не '2000' и не '3000'.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.