ABAP 7.4/7.5: SELECT Queries and LOOP
-------------------------------------------------------------------------------------
CASE 1:
SELECT SINGLE VORNA as First_Name,
NACHN as Last_Name
FROM PA0002
INTO TABLE @DATA(LS_EE_NAME).
This will create a structure with columns with field description as FIRST_NAME and LAST_NAME
-------------------------------------------------------------------------------------
CASE 2:
SELECT VORNA as First_Name,
NACHN as Last_Name
FROM PA0002
INTO TABLE @DATA(LT_EE_NAME).
This will create an internal table with single column with field description as FIRST_NAME and LAST_NAME
-------------------------------------------------------------------------------------
CASE 3: WHERE
PARAMETERS: P_PERNR TYPE PERSNO.
SELECT SINGLE PERNR as Employee,
VORNA as First_Name,
NACHN as Last_Name
FROM PA0002
INTO TABLE @DATA(LS_EE_NAME)
WHERE Employee EQ @P_PERNR.
// One can use Internal table as well without using SINGLE
-------------------------------------------------------------------------------------
CASE 4: JOIN // WHERE // FOR ALL ENTRIES IN
PARAMETERS: P_PERNR TYPE PERSNO.
CONSTANTS: LC_SUBTY TYPE SUBTY VALUE '0001'.
IF LT_EE_MAIN[] IS NOT INITIAL.
SELECT A~VORNA, A~NACHAN
B~SUBTY, B~REGIO, B~STATE, B~PSTLZ
FROM PA0002 AS A
INNER JOIN PA0006
ON PA0006-PERNR EQ PA0002-PERNR
INTO TABLE @DATA(LT_EE_NAME)
FOR ALL ENTRIES IN @LT_EE_MAIN[]
WHERE A~PERNR EQ @LT_EE_MAIN[]-PERNR
AND A~PERNR EQ @P_PERNR
AND B~SUBTY EQ @LC_SUBTY.
-------------------------------------------------------------------------------------
CASE 5: General Declarations
DATA: lv_rows TYPE i. lv_rows = 0.
lv_rows = NEW i( 0 ).
DATA(lv_vehicle) = 'Mercedes'.
DATA(lv_rows) = LINES( itab ).
lo_human = NEW class_human( name = ‘TONY’ ).
INSERT Values in an Internal Table:
DATA(LT_TAB) = VALUE((10)( 20 )(30).
-------------------------------------------------------------------------------------
CASE 6: LOOP & APPEND
TYPES: BEGIN OF LTY_CITY_1,
CITY TYPE CHAR10,
STATE TYPE CHAR10,
PST_CD TYPE CHAR10,
END OF LTY_CITY_1,
BEGIN OF LTY_CITY_2,
CITY TYPE CHAR10,
END OF LTY_CITY_2.
DATA: LIT_CITIES TYPE TABLE OF LTY_CITY_1,
GT_CITY TYPE TABLE OF LTY_CITY_2
DATA(GT_CITY) = VALUE LTY_CITY_1( FOR LS_CITY IN LIT_CITIES
( LS_CITY-CITY )
WHERE ( LS_CITY-CITY )(STATE = 'NY') ).
Store value from one table to another by filtering conditions in loop (avoiding append a different table in loop)
-------------------------------------------------------------------------------------
CASE 7: FILTER
EXAMPLE 1:
DATA(extract) = FILTER #( spfli_tab USING KEY carr_city
WHERE carrid = CONV #( to_upper( carrid ) ) AND
cityfrom = CONV #( to_upper( cityfrom ) )
).
EXAMPLE 2:
TYPES: BEGIN OF filter,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto,
END OF filter,
filter_tab TYPE HASHED TABLE OF filter
WITH UNIQUE KEY cityfrom cityto.
DATA(filter_tab) = …
DATA(extract) = FILTER #( spfli_tab IN filter_tab WHERE cityfrom = cityfrom AND cityto = cityto ).
EXAMPLE 3:
TYPES: BEGIN OF line,
id TYPE i,
value TYPE string,
END OF line,
itab TYPE SORTED TABLE OF line WITH UNIQUE KEY id.
DATA(def) = VALUE line( id = 0 value = `not found` ).
…
DATA(result) = VALUE #( itab[ id = … ] DEFAULT def ).
0 comments:
Post a Comment