Difference between revisions of "SAP ABAP 740"

From SapWiki
 
(80 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Ejemplos==
+
==Manejo de strings==
 
+
[https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-us/abapcompute_string_format_options.htm Documentación SAP]
===Manejo de strings===
 
 
  character_string = |{ a_numeric_variable }|.
 
  character_string = |{ a_numeric_variable }|.
 
  character_string = |This resulted in return code { sy-subrc }|.
 
  character_string = |This resulted in return code { sy-subrc }|.
Line 51: Line 50:
 
  ENDTRY.
 
  ENDTRY.
  
===Manejo de Tablas===
+
ld_message = |{ ld_delivery ALPHA = OUT }|.
 +
 
 +
===shift_right && clase cl_abap_string_utilities===
 +
<nowiki>DATA(str) = `abc  `.
 +
 
 +
DATA(rslt) = str.
 +
cl_abap_string_utilities=>del_trailing_blanks( CHANGING str = rslt ).
 +
 
 +
ASSERT rslt =  shift_right( str ).</nowiki>
 +
 
 +
==Manejo de Tablas==
 
  DATA l_pernr TYPE p0001-pernr VALUE '00003019'.
 
  DATA l_pernr TYPE p0001-pernr VALUE '00003019'.
 
   
 
   
 
  SELECT * INTO TABLE @DATA(lt_p2010) FROM pa2010 WHERE pernr = @l_pernr.
 
  SELECT * INTO TABLE @DATA(lt_p2010) FROM pa2010 WHERE pernr = @l_pernr.
+
===line_exists===
*1 line_exists
 
 
  IF line_exists( lt_p2010[ pernr = l_pernr begda = '20181101' ] ).
 
  IF line_exists( lt_p2010[ pernr = l_pernr begda = '20181101' ] ).
  * do somithing
+
  * do something
 
   WRITE:/ 'Existe'.
 
   WRITE:/ 'Existe'.
 
  ENDIF.
 
  ENDIF.
+
 
*2 l_index
+
===line_index===
 
  DATA(l_index) = line_index( lt_p2010[ pernr = l_pernr begda = '20181101' ] ).
 
  DATA(l_index) = line_index( lt_p2010[ pernr = l_pernr begda = '20181101' ] ).
 
  WRITE:/ l_index.
 
  WRITE:/ l_index.
 
   
 
   
+
===Modify the contents based on TABLE-KEY===
*3 Modify the contents based on TABLE-KEY
 
 
  TRY.
 
  TRY.
 
     lt_p2010[ pernr = l_pernr ]-begda = '20200101'.
 
     lt_p2010[ pernr = l_pernr ]-begda = '20200101'.
Line 73: Line 80:
 
  ENDTRY.
 
  ENDTRY.
 
   
 
   
*4
+
===search line===
DATA(ls_p2010) = lt_p2010[ pernr = l_pernr ].
+
  TRY.
 +
    DATA(ls_p2010) = lt_p2010[ pernr = l_pernr ].
 +
    CATCH cx_sy_itab_line_not_found.
 +
      RETURN.
 +
  ENDTRY.
 
  WRITE:/ ls_p2010-begda.
 
  WRITE:/ ls_p2010-begda.
 
   
 
   
*5
+
===CORRESPONDING===
 
  data lt_p0002 type TABLE OF p0002.
 
  data lt_p0002 type TABLE OF p0002.
 
   
 
   
Line 85: Line 96:
 
  write:/ ls_p0002-pernr.
 
  write:/ ls_p0002-pernr.
  
  *6 Inicializar tabla
+
    FUNCTION z_rfc_read_id.
 +
  *"----------------------------------------------------------------------
 +
*"*"Interfase local
 +
*"  IMPORTING
 +
*"    VALUE(I_ID) TYPE  ZRFCID
 +
*"  TABLES
 +
*"      T_ID    STRUCTURE  ZTABLE
 +
*"      T_RETURN STRUCTURE  ZRETURN
 +
*"      T_RETURN_2 STRUCTURE  ZRETURN_2 OPTIONAL
 +
...
 +
...
 +
...
 +
t_return_2[] = CORRESPONDING #( t_return[] ).
 +
APPEND LINES OF lt_return TO t_return_2.
 +
t_return[] = CORRESPONDING #( t_return_2[] ).
 +
 
 +
===MAPPING===
 +
itab = CORRESPONDING #( itab3 MAPPING a = c b = d ).
 +
===EXCEPT===
 +
itab = CORRESPONDING #( itab3 EXCEPT e ).
 +
===DISCARDING DUPLICATES===
 +
itab = CORRESPONDING #( itab2 DISCARDING DUPLICATES ).
 +
 
 +
===Inicializar tabla===
 
  lt_p0002 = VALUE #( ( pernr = l_pernr  begda = sy-datum ) ).
 
  lt_p0002 = VALUE #( ( pernr = l_pernr  begda = sy-datum ) ).
  
*7 agregar registros a tabla (append)
+
===agregar registros a tabla (append)===
 
  lt_p0002 = VALUE #( BASE lt_p0002 ( pernr = l_pernr  begda = '20200201' ) ).
 
  lt_p0002 = VALUE #( BASE lt_p0002 ( pernr = l_pernr  begda = '20200201' ) ).
  
*8 FILTER
+
===FILTER===
 
  DATA lt_p0002_s TYPE STANDARD TABLE OF p0002 WITH NON-UNIQUE SORTED KEY pernr COMPONENTS pernr.
 
  DATA lt_p0002_s TYPE STANDARD TABLE OF p0002 WITH NON-UNIQUE SORTED KEY pernr COMPONENTS pernr.
 
   
 
   
Line 130: Line 164:
 
  lt_p0002_h = FILTER #( lt_p0002_3 IN lt_filter_tab
 
  lt_p0002_h = FILTER #( lt_p0002_3 IN lt_filter_tab
 
                     WHERE pernr = pernr  AND begda = begda ).
 
                     WHERE pernr = pernr  AND begda = begda ).
 +
====ejemplo 2====
 +
<nowiki>
 +
DATA ti_teven TYPE TABLE OF teven.
 +
DATA l_pernr TYPE teven-pernr.
  
===VALUE===
+
TYPES: BEGIN OF filter,
  types ty_ra type RANGE OF vbak-auart.
+
        pernr TYPE teven-pernr,
 +
      END OF filter,
 +
      filter_tab TYPE HASHED TABLE OF filter
 +
        WITH UNIQUE KEY pernr.
 +
 
 +
DO 2 TIMES.
 +
  IF sy-index = 1.
 +
    l_pernr = '10000307'.
 +
  ELSE.
 +
    l_pernr = '00000000'.
 +
  ENDIF.
 +
 
 +
  DATA(filter_tab) = VALUE filter_tab(
 +
      ( pernr = l_pernr ) ).
 +
 
 +
  SELECT * FROM teven
 +
          INTO TABLE ti_teven
 +
          ORDER BY pernr ldate ltime erdat ertim pdsnr.
 +
 
 +
  DATA(lt_teven) = FILTER #( ti_teven IN filter_tab
 +
                WHERE pernr = pernr ) .
 +
 
 +
ENDDO.</nowiki>
 +
===LOOP GROUP BY===
 +
*[https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abenloop_group_by_abexas.htm Examples of Grouping with LOOP]
 +
*Programa DEMO_LOOP_AT_ITAB_GROUP_BY
 +
 
 +
==VALUE==
 +
  <nowiki>types ty_ra type RANGE OF vbak-auart.
 
  data(ra_auart) = VALUE ty_ra(
 
  data(ra_auart) = VALUE ty_ra(
 
                               ( low = 'Z1' sign = 'I' option = 'EQ' )
 
                               ( low = 'Z1' sign = 'I' option = 'EQ' )
Line 154: Line 220:
 
     WHEN '0901'.
 
     WHEN '0901'.
 
       ra_ktart = VALUE #( ( low = '10' sign = 'I' option = 'EQ' ) ).
 
       ra_ktart = VALUE #( ( low = '10' sign = 'I' option = 'EQ' ) ).
  ENDCASE.
+
  ENDCASE.</nowiki>
 +
 
 +
===APPEND VALUE===
 +
    APPEND VALUE #( sign = 'I' option = 'EQ' low = 'A000' ) TO lr_einri.
 +
    APPEND VALUE #( sign = 'I' option = 'EQ' low = 'C200' ) TO lr_einri.
 +
 
 +
    INSERT VALUE #( comp1 = a comp2 = b ... ) INTO TABLE itab.
 +
===VALUE...BASE===
 +
<nowiki> TYPES: BEGIN OF ty_data2,
 +
          col1 TYPE char1,
 +
          col2 TYPE i,
 +
        END OF ty_data2.
 +
 
 +
  DATA lt_tabla TYPE TABLE OF ty_data2.
 +
  DATA lt_tabla2 TYPE TABLE OF ty_data2.
 +
 
 +
  lt_tabla = VALUE #( ( col1 = 'A' col2 = 2 ) ( col1 = 'B' col2 = 11 ) ( col1 = 'A' col2 = 23 ) ).
 +
  LOOP AT lt_tabla INTO DATA(ls_tabla) WHERE col1 = 'A'.
 +
    lt_tabla2 = VALUE #( BASE lt_tabla2 ( ls_tabla ) ).  "APPEND
 +
  ENDLOOP.</nowiki>
 +
 
 +
Uses BASE to append rows to existing rows in an internal table.
 +
<nowiki>
 +
TYPES itab TYPE TABLE OF string WITH EMPTY KEY.
 +
 
 +
DATA(itab) =
 +
  VALUE itab(
 +
    ( `a` ) ( `b` ) ( `c` ) ).
 +
 
 +
...
 +
 
 +
itab =
 +
  VALUE #(
 +
    BASE itab
 +
    ( `d` ) ( `e` ) ( `f` ) ).
 +
</nowiki>
 +
 
 +
===VALUE...FOR===
 +
<nowiki>
 +
    DATA itab TYPE TABLE OF i WITH EMPTY KEY.
 +
    itab = VALUE #( FOR j = 1 WHILE j <= 10 ( j ) ).</nowiki>
 +
 
 +
==REDUCE==
 +
===Creating Values with FOR and REDUCE===
 +
<nowiki>
 +
*Summation
 +
  DATA(l_v1) = REDUCE i( INIT sum = 0
 +
                      FOR n = 1 UNTIL n > 10
 +
                      NEXT sum = sum + n ) .
 +
*Concatenation without THEN
 +
  data(l_v2) = REDUCE string( INIT text = `Count up:`
 +
                            FOR n = 1 UNTIL n > 10
 +
                            NEXT text = text && | { n }| ).
 +
*Concatenation with THEN
 +
  DATA(l_v3) = REDUCE string( INIT text = `Count down:`
 +
                            FOR n = 10 THEN n - 1 WHILE n > 0
 +
                            NEXT text = text && | { n }| ).
 +
*Non arithmetic expression
 +
  DATA(l_v4) = REDUCE string( INIT text = ``
 +
                            FOR t = `x` THEN t && `y`
 +
                                        UNTIL strlen( t ) > 10
 +
                            NEXT text = text && |{ t } | ).
 +
*L_V1 = 55
 +
*L_V2 = Count up: 1 2 3 4 5 6 7 8 9 10
 +
*L_V3 = Count down: 10 9 8 7 6 5 4 3 2 1
 +
*L_V4 = x xy xyy xyyy xyyyy xyyyyy xyyyyyy xyyyyyyy xyyyyyyyy xyyyyyyyyy
 +
</nowiki>
 +
 
 +
===table reduction===
 +
====Summation of an Array====
 +
<nowiki>
 +
    DATA itab TYPE TABLE OF i WITH EMPTY KEY.
 +
    itab = VALUE #( FOR j = 1 WHILE j <= 10 ( j ) ).
 +
    cl_demo_output=>write( itab ).
 +
 
 +
    DATA(sum) = REDUCE i( INIT x = 0 FOR wa IN itab NEXT x = x + wa ).</nowiki>
  
===CONV===
+
==CONV==
 
  data: l_dir type char200.
 
  data: l_dir type char200.
 
  l_dir = 'c:\temp'.
 
  l_dir = 'c:\temp'.
Line 169: Line 310:
 
  ENDIF.   
 
  ENDIF.   
  
===COND===
+
==COND==
 
  DATA(time) = COND string( WHEN sy-timlo < '120000' THEN |{ sy-timlo TIME = ISO } AM|
 
  DATA(time) = COND string( WHEN sy-timlo < '120000' THEN |{ sy-timlo TIME = ISO } AM|
 
                           WHEN sy-timlo > '120000' THEN |{ CONV t( sy-timlo - 12 * 3600 ) TIME = ISO } PM|
 
                           WHEN sy-timlo > '120000' THEN |{ CONV t( sy-timlo - 12 * 3600 ) TIME = ISO } PM|
 
                           WHEN sy-timlo = '120000' THEN |High Noon|
 
                           WHEN sy-timlo = '120000' THEN |High Noon|
 
                           ELSE 'nada' ).
 
                           ELSE 'nada' ).
=== SWITCH ===
+
== SWITCH ==
 
  DATA l_flag TYPE flag.
 
  DATA l_flag TYPE flag.
 
  DATA(l_status) = SWITCH #( l_flag
 
  DATA(l_status) = SWITCH #( l_flag
Line 185: Line 326:
 
                                       WHEN 3 THEN 'tres'
 
                                       WHEN 3 THEN 'tres'
 
                                       else 'nada' ).
 
                                       else 'nada' ).
===BOOLC===
+
==BOOLC==
 
   p_f = boolc( p2001[] is not initial ).
 
   p_f = boolc( p2001[] is not initial ).
 +
==EQUIV==
 +
[https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenlogexp_equiv.htm Equiv]
 +
 +
<nowiki>
 +
SELECTION-SCREEN BEGIN OF BLOCK bl1.
 +
PARAMETERS p_alv RADIOBUTTON GROUP radi.
 +
PARAMETERS p_csv RADIOBUTTON GROUP radi.
 +
 +
PARAMETERS p_pc AS CHECKBOX.
 +
PARAMETERS p_serv AS CHECKBOX.
 +
 +
SELECTION-SCREEN END OF BLOCK bl1.
 +
 +
AT SELECTION-SCREEN ON BLOCK bl1.
  
===NEW===
+
  IF p_csv = abap_true.
 +
    IF p_pc = abap_false AND p_serv = abap_false.
 +
      MESSAGE 'Debe seleccionar destino de archivo de salida (PC o Servidor)' TYPE 'E'.
 +
    elseIF p_pc = abap_true EQUIV p_serv = abap_true.
 +
      MESSAGE e000(0k) WITH 'Debe elegir solo una opcion'.
 +
    ENDIF.
 +
  ENDIF.</nowiki>
 +
==NEW==
 
   DATA g_sol TYPE REF TO cl_sol.
 
   DATA g_sol TYPE REF TO cl_sol.
 
   CREATE OBJECT g_sol
 
   CREATE OBJECT g_sol
Line 199: Line 361:
 
Non-class-based exceptions of the instance constructor cannot be handled, which means that sy-subrc is never set to a value other than 0. The return code sy-subrc is not set when anonymous data objects are created.
 
Non-class-based exceptions of the instance constructor cannot be handled, which means that sy-subrc is never set to a value other than 0. The return code sy-subrc is not set when anonymous data objects are created.
  
===Códigos Varios===
+
==OPEN SQL==
 +
=== [https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapsql_expr.htm SQL Expressions sql_exp]===
 +
=== [https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abensql_expr_abexas.htm Examples of SQL Expressions]===
 +
=== [https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abensql_functions_string.htm String Functions]===
 +
=== Demos: DEMO_SQL_*===
 +
===[https://blogs.sap.com/2018/12/26/for-all-entries-alternatives/ Alternativas para For All Entries]===
 +
=== [https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapselect_itab.htm SELECT - FROM @itab]===
 +
=== [https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abenddic_database_tables_gtt.htm Global Temporary Tables]===
 +
=== [https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abapunion.htm UNION]===
 +
  <nowiki>
 +
SELECT FROM pa0002 FIELDS pernr,vorna WHERE gbdat >= '19900101' INTO TABLE @DATA(lt_pernr2).
 +
 
 +
  SELECT FROM vbrk
 +
          INNER JOIN vbrp
 +
            ON vbrk~vbeln = vbrp~vbeln
 +
        FIELDS vbrk~*,vbrp~posnr,vbrp~matnr
 +
        WHERE fkdat >= '20190101'
 +
        INTO TABLE @DATA(itab).
 +
 
 +
*ITAB[1]-VBRK-VBELN = '0090156792'
 +
 
 +
DATA city        TYPE spfli-cityfrom VALUE 'NEW YORK'.
 +
SELECT *
 +
      FROM sflight AS s
 +
      WHERE seatsocc < s~seatsmax AND
 +
            EXISTS ( SELECT  *
 +
                            FROM spfli
 +
                            WHERE carrid  =  s~carrid AND
 +
                                  connid  =  s~connid AND
 +
                                  cityfrom = @city )
 +
      INTO TABLE @DATA(free_flights).
 +
 
 +
DATA: carr_id TYPE spfli-carrid VALUE 'LH',
 +
      conn_id TYPE spfli-connid VALUE '400'.
 +
SELECT SINGLE city, latitude, longitude
 +
      FROM sgeocity
 +
      WHERE city IN ( SELECT cityfrom
 +
                              FROM spfli
 +
                              WHERE carrid = @carr_id AND
 +
                                    connid = @conn_id )
 +
      INTO (@DATA(city), @DATA(lati), @DATA(longi)).
 +
 
 +
      SELECT planetype, seatsmax
 +
            FROM  saplane AS plane
 +
            WHERE seatsmax < @wa-seatsmax AND
 +
                  seatsmax >= ALL
 +
                    ( SELECT seatsocc
 +
                              FROM  sflight
 +
                              WHERE carrid = @wa-carrid AND
 +
                              connid = @wa-connid )
 +
            ORDER BY seatsmax
 +
            INTO (@DATA(plane), @DATA(seats)).
 +
 
 +
    SELECT SINGLE
 +
          FROM demo_expressions
 +
          FIELDS CAST( num1    AS CHAR( 20 ) ) AS col1,
 +
                  CAST( numlong1 AS CHAR( 20 ) ) AS col2,
 +
                  CAST( dec3    AS CHAR( 20 ) ) AS col3,
 +
                  CAST( dats2    AS CHAR( 20 ) ) AS col4
 +
          WHERE id = 'X'
 +
          INTO @DATA(result).
 +
</nowiki>
 +
 
 +
===constantes en select list===
 +
<nowiki>
 +
SELECT carrid, connid, '007' as tipo
 +
      FROM spfli
 +
      INTO  table @DATA(result).
 +
 
 +
SELECT carrid, connid, @abap_true as tipo
 +
      FROM spfli
 +
      INTO  table @DATA(result).
 +
 
 +
SELECT carrid, connid, @sy-datum as fecha_ejecucion
 +
      FROM spfli
 +
      INTO  table @DATA(result).
 +
 
 +
SELECT SINGLE vorna && ' ' && nachn && ' ' && name2 AS nombre_emp, gbdat 
 +
                                                  INTO (@wa-nombre_emp, @wa-gbdat) FROM pa0002
 +
                                                WHERE pernr = @pernr AND
 +
                                                      begda <= @sy-datum AND
 +
                                                      endda >= @sy-datum.
 +
</nowiki>
 +
 
 +
===CASE===
 +
<nowiki>
 +
    SELECT SINGLE j~partner, i~rut_pagador, i~total_pagar, i~nom_archivo,
 +
                CASE i~banco_abon
 +
                  WHEN ' ' THEN i~banco_abon2
 +
                  ELSE i~banco_abon
 +
                END AS banco
 +
                  INTO CORRESPONDING FIELDS OF @wa_data
 +
                                            FROM zfi_histra_paso AS i INNER JOIN but000 AS j
 +
                                                  ON i~rut_pagador = j~bu_sort2
 +
                                                  INNER JOIN zfi_bp_datos_com AS z
 +
                                                  ON j~partner = z~bp
 +
                                            WHERE i~folio = @g_folio_histra
 +
                                              AND i~rut_pagador IN @s_rut
 +
                                              AND i~nom_archivo LIKE 'A%'
 +
                                              AND j~partner IN @s_bp
 +
                                              AND z~tipo_sucursal = '00' . "#EC CI_SEL_NESTED
 +
 
 +
SELECT pernr, vorna,
 +
    CASE
 +
      WHEN vorna <> 'Homero' THEN 'MAGIOS'
 +
      ELSE 'NO MAGIOS'
 +
  END AS secta
 +
FROM pa0002
 +
  INTO TABLE @DATA(lt_pernr).
 +
</nowiki>
 +
 
 +
===CONCAT===
 +
<nowiki>
 +
SELECT FROM  spfli
 +
      FIELDS CONCAT( CONCAT( carrid , '_' ),
 +
                              CAST( connid AS CHAR ) )  AS key,
 +
              cityto, 1  AS cities
 +
      INTO TABLE @DATA(result).
 +
 
 +
KEY    CITYTO         CITIES
 +
LH_0400 NEW YORK 1
 +
AA_0017 SAN FRANCISCO 1
 +
AZ_0555 FRANKFURT 1
 +
 
 +
REPORT demo_sql_function_concat.
 +
 
 +
CLASS demo DEFINITION.
 +
  PUBLIC SECTION.
 +
    CLASS-METHODS main.
 +
ENDCLASS.
 +
 
 +
CLASS demo IMPLEMENTATION.
 +
  METHOD main.
 +
    SELECT CONCAT( CONCAT( carrid,
 +
                          LPAD( carrname,21,' ' ) ),
 +
                  LPAD( url,40,' ' ) ) AS line
 +
          FROM scarr
 +
          INTO TABLE @DATA(result).
 +
    cl_demo_output=>display( result ).
 +
  ENDMETHOD.
 +
ENDCLASS.
 +
 
 +
START-OF-SELECTION.
 +
  demo=>main( ).
 +
</nowiki>
 +
 
 +
===SUBSTRING===
 +
<nowiki>
 +
*PRmmaaRppnnnnnn
 +
*0208020 + aa + mm + nnnnnn
 +
*Ejemplo: PR0720R007188233 se convierte en 02080202007188233
 +
  SELECT concat( '0208020', ( concat( substring( xblnr,5,2 ), ( concat( substring( xblnr,3,2 ), ( substring( xblnr,10,6 ) ) ) ) ) ) ) AS folio
 +
                        FROM bsis
 +
                      WHERE bukrs EQ @g_bukrs
 +
                        AND xblnr IN @s_xblnr
 +
                AND augdt = '00000000'
 +
 
 +
  INTO CORRESPONDING FIELDS OF TABLE @gt_data . </nowiki>
 +
 
 +
===Literal in SELECT List===
 +
<nowiki>
 +
SELECT SINGLE @abap_true
 +
          FROM scarr
 +
          WHERE carrid = @carrier
 +
          INTO @DATA(exists).
 +
      IF exists = abap_true.
 +
        cl_demo_output=>display(
 +
          |Carrier { carrier } exists in SCARR| ).
 +
      ELSE.
 +
        cl_demo_output=>display(
 +
          |Carrier { carrier } does not exist in SCARR| ).
 +
      ENDIF. </nowiki>
 +
 
 +
===aggregations===
 +
<nowiki>
 +
* nombre completo empleado
 +
select VORNA && ' ' && nachn as nombre  from pa0002 into table @data(lt_data) where pernr = '10000000'.
 +
 
 +
*total factura
 +
select netwr + MWSBK as total  from vbrk into table @data(lt_vbrk) UP TO 1 ROWS.
 +
 
 +
* facturas con total > 1000000
 +
select netwr + MWSBK as total  from vbrk  where netwr + MWSBK > 10000 and waerk = 'CLP'
 +
                                      into table @data(lt_vbrk).
 +
</nowiki>
 +
[https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapselect_aggregate.htm Open SQL - Aggregate Expressions agg_exp]
 +
 
 +
===Host Expressions, Use in the WHERE Condition ===
 +
<nowiki>
 +
REPORT demo_select_where_tab_exp.
 +
 
 +
CLASS demo DEFINITION.
 +
  PUBLIC SECTION.
 +
    CLASS-DATA carriers TYPE HASHED TABLE OF scarr
 +
                  WITH UNIQUE KEY carrid
 +
                  WITH NON-UNIQUE SORTED KEY name COMPONENTS carrname.
 +
    CLASS-METHODS:
 +
      class_constructor,
 +
      main.
 +
ENDCLASS.
 +
 
 +
CLASS demo IMPLEMENTATION.
 +
  METHOD main.
 +
    DATA name TYPE scarr-carrname VALUE 'United Airlines'.
 +
    cl_demo_input=>request( CHANGING field = name ).
 +
 
 +
    SELECT carrid, connid, cityfrom, cityto
 +
          FROM spfli
 +
          WHERE carrid =
 +
            @( VALUE spfli-carrid( carriers[ KEY name
 +
                                              carrname = name ]-carrid
 +
                                              OPTIONAL ) )
 +
          INTO TABLE @DATA(result).
 +
 
 +
    cl_demo_output=>display( result ).
 +
  ENDMETHOD.
 +
  METHOD class_constructor.
 +
    SELECT *
 +
          FROM scarr
 +
          INTO TABLE carriers.
 +
  ENDMETHOD.
 +
 
 +
ENDCLASS.
 +
START-OF-SELECTION.
 +
  demo=>main( ).
 +
</nowiki>
 +
 
 +
<nowiki>report zdemo99.
 +
CLASS cl_mara DEFINITION.
 +
  PUBLIC SECTION.
 +
    CLASS-METHODS: get_maktx IMPORTING i_matnr TYPE matnr
 +
                            RETURNING VALUE(r_maktx) TYPE maktx.
 +
 
 +
ENDCLASS.
 +
 
 +
CLASS cl_mara IMPLEMENTATION.
 +
  METHOD get_maktx.
 +
    SELECT SINGLE maktx INTO r_maktx FROM  makt
 +
          WHERE  matnr  = i_matnr
 +
          AND    spras  = sy-langu.
 +
    IF sy-subrc <> 0.
 +
      r_maktx = space.
 +
    ENDIF.
 +
  ENDMETHOD.
 +
 
 +
ENDCLASS.
 +
 
 +
START-OF-SELECTION.
 +
DATA l_matnr TYPE mara-matnr.
 +
 
 +
  cl_demo_input=>request( CHANGING field = l_matnr ).
 +
 
 +
  SELECT matnr,maktx FROM  makt
 +
        WHERE  matnr  = @l_matnr
 +
        AND    spras  = @sy-langu
 +
    INTO TABLE @DATA(lt_makt).
 +
 
 +
  SELECT matnr,
 +
        mtart,
 +
        meins,
 +
        CASE WHEN matkl = 'Z1MC00054'  THEN 'K'
 +
          ELSE @( lt_makt[ matnr = l_matnr ]-maktx )
 +
      END AS maktx
 +
      FROM mara WHERE matnr = @l_matnr
 +
      INTO TABLE @DATA(lt_mara).
 +
 
 +
  SELECT matnr,
 +
        mtart,
 +
        meins,
 +
        CASE WHEN matkl = 'Z1MC00054'  THEN 'K'
 +
          ELSE @( cl_mara=>get_maktx( i_matnr = l_matnr ) )
 +
      END AS maktx
 +
      FROM mara WHERE matnr = @l_matnr
 +
      INTO TABLE @DATA(lt_mara2).
 +
 
 +
  cl_demo_output=>write( lt_mara ).
 +
  cl_demo_output=>write( lt_mara2 ).
 +
 
 +
  cl_demo_output=>display( ).
 +
</nowiki>
 +
 
 +
===ABAP SQL Indicator structure (ABAP 7.55 (or) S/4HANA 2020)===
 +
[https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/abapupdate_set_indicator.htm UPDATE dbtab, set indicators]
 +
<nowiki>
 +
TYPES wa TYPE sflight WITH INDICATORS ind.
 +
 
 +
DATA itab TYPE TABLE OF wa WITH EMPTY KEY.
 +
 
 +
SELECT carrid, connid, fldate, price
 +
      FROM sflight
 +
      WHERE carrid = char`LH` AND
 +
            connid = numc`0400` AND
 +
            fldate > '20180601'
 +
      INTO CORRESPONDING FIELDS OF TABLE @itab.
 +
 
 +
IF sy-subrc  = 0.
 +
  LOOP AT itab ASSIGNING FIELD-SYMBOL(<wa>).
 +
    <wa>-price *= '0.8'.
 +
    <wa>-ind-price = 'X'.
 +
  ENDLOOP.
 +
 
 +
  UPDATE sflight FROM TABLE @itab INDICATORS SET STRUCTURE ind.
 +
ENDIF.
 +
</nowiki>
 +
 
 +
===JOIN===
 +
<nowiki>
 +
"objeto arc_doc_id tiene 1 registro en tabla toa01 y no existe en tabla toatt
 +
 
 +
select i~ar_object,j~descr
 +
    FROM toa01 as i LEFT OUTER JOIN toaat as j on i~arc_doc_id = j~arc_doc_id
 +
  where i~arc_doc_id = 'f14195fb-d35f-4b41-9406-4e0aeef25d31'
 +
  INTO TABLE @DATA(result3). "tabla result3: 1 registro
 +
 
 +
 
 +
select i~ar_object,j~descr
 +
    FROM toa01 as i inner JOIN toaat as j on i~arc_doc_id = j~arc_doc_id
 +
  where i~arc_doc_id = 'f14195fb-d35f-4b41-9406-4e0aeef25d31'
 +
  INTO TABLE @DATA(result4). "tabla result4: 0 registro
 +
</nowiki>
 +
 
 +
==Códigos Varios==
 
  DATA(l_day) = cl_rs_time_service=>get_weekday( i_date = sy-datum ).
 
  DATA(l_day) = cl_rs_time_service=>get_weekday( i_date = sy-datum ).
 
  IF l_day > 4.
 
  IF l_day > 4.
Line 213: Line 696:
 
  ********************
 
  ********************
 
  DATA(components) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_name( 'T100' ) )->components.
 
  DATA(components) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_name( 'T100' ) )->components.
 +
 +
==Numeric Calculations==
 +
===ipow - Integer Power Function===
 +
ipow( base = arg exp = n ), equivale a: arg ** n , pero ipow tiene mejor performance, ejemplo programa DEMO_IPOW
 +
 +
===nmax, nmin - Numeric Extremum Functions===
 +
 +
Syntax Forms
 +
 +
... nmax|nmin( val1 = arg1 val2 = arg2 [val3 = arg3] ... [val9 = arg9] ) ...
 +
 +
Effect
 +
 +
These functions return the value of the greatest or the least of the arguments passed. A minimum of two arguments, arg1 and arg2, and a maximum of nine arguments must be passed. Here, the optional input parameters val3 to val9 must be filled in ascending order without gaps. The arguments arg1 to arg9 are numeric expression positions.
 +
 +
===General numerical functions===
 +
Ver programa demo_numerical_function
  
 
==Demos==
 
==Demos==

Latest revision as of 16:23, 20 November 2023

Manejo de strings

Documentación SAP

character_string = |{ a_numeric_variable }|.
character_string = |This resulted in return code { sy-subrc }|.
LO_OBJECT->STRING2XML( |{ converted_xml }{ xml_row-row_close_tag }| ).
v_var1 = 'Esto es'.
v_var2 = 'un'.
v_var3 = 'String'.
lv_result = v_var1 && v_var2 && v_var3.
[WIDTH     = len]
[ALIGN     = LEFT|RIGHT|CENTER|(val)]
[PAD       = c]
[CASE      = RAW|UPPER|LOWER|(val)]
[SIGN      = LEFT|LEFTPLUS|LEFTSPACE|RIGHT|RIGHTPLUS|RIGHTSPACE|(val)]
[EXPONENT  = exp]
[DECIMALS  = dec]
[ZERO      = YES|NO|(val)]
[XSD       = YES|NO|(val)]
[STYLE     =  SIMPLE|SIGN_AS_POSTFIX|SCALE_PRESERVING
             |SCIENTIFIC|SCIENTIFIC_WITH_LEADING_ZERO
             |SCALE_PRESERVING_SCIENTIFIC|ENGINEERING
             |(val)]
[CURRENCY  = cur]
[NUMBER    = RAW|USER|ENVIRONMENT|(val)]
[DATE      = RAW|ISO|USER|ENVIRONMENT|(val)]
[TIME      = RAW|ISO|USER|ENVIRONMENT|(val)]
[TIMESTAMP = SPACE|ISO|USER|ENVIRONMENT|(val)]
[TIMEZONE  = tz]
[COUNTRY   = cty] ...

l_dir = |including a \{ bracket|.
l_dir = |c:\\temp|.  "c:\temp'
TRY.
  WRITE: /   |string|.
  WRITE: /   |{ my_variable }|.
  WRITE: /   |i'm a { my_variable }!|.
  WRITE: /   |i'm number { my_number * 10 }!| .
  WRITE: /   |client 000 is called: | &&
             |{ clients[ mandt = '000' ]-mtext }|.
  WRITE: /   |i'm going to be in upper case: | &&
             |{ my_variable CASE = UPPER }|.
  WRITE: /   |function and formatting:| &&
             | date raw - { my_string->date( ) DATE = RAW }| &&
             | date iso - { my_string->date( ) DATE = ISO }|.
  CATCH cx_root.
ENDTRY.
ld_message = |{ ld_delivery ALPHA = OUT }|.

shift_right && clase cl_abap_string_utilities

DATA(str) = `abc   `.

 DATA(rslt) = str.
 cl_abap_string_utilities=>del_trailing_blanks( CHANGING str = rslt ).

 ASSERT rslt =  shift_right( str ).

Manejo de Tablas

DATA l_pernr TYPE p0001-pernr VALUE '00003019'.

SELECT * INTO TABLE @DATA(lt_p2010) FROM pa2010 WHERE pernr = @l_pernr.

line_exists

IF line_exists( lt_p2010[ pernr = l_pernr begda = '20181101' ] ).
* do something
  WRITE:/ 'Existe'.
ENDIF.

line_index

DATA(l_index) = line_index( lt_p2010[ pernr = l_pernr begda = '20181101' ] ).
WRITE:/ l_index.

Modify the contents based on TABLE-KEY

TRY.
    lt_p2010[ pernr = l_pernr ]-begda = '20200101'.
  CATCH cx_sy_itab_line_not_found.
ENDTRY.

search line

 TRY.
   DATA(ls_p2010) = lt_p2010[ pernr = l_pernr ].
   CATCH cx_sy_itab_line_not_found.
     RETURN.
 ENDTRY.
WRITE:/ ls_p2010-begda.

CORRESPONDING

data lt_p0002 type TABLE OF p0002.

lt_p0002 = CORRESPONDING #( lt_p2010 ).

data(ls_p0002) = lt_p0002[ 1 ].
write:/ ls_p0002-pernr.
    FUNCTION z_rfc_read_id.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     VALUE(I_ID) TYPE  ZRFCID
*"  TABLES
*"      T_ID     STRUCTURE  ZTABLE
*"      T_RETURN STRUCTURE  ZRETURN
*"      T_RETURN_2 STRUCTURE  ZRETURN_2 OPTIONAL
...
...
...
t_return_2[] = CORRESPONDING #( t_return[] ).
APPEND LINES OF lt_return TO t_return_2.
t_return[] = CORRESPONDING #( t_return_2[] ).

MAPPING

itab = CORRESPONDING #( itab3 MAPPING a = c b = d ).

EXCEPT

itab = CORRESPONDING #( itab3 EXCEPT e ).

DISCARDING DUPLICATES

itab = CORRESPONDING #( itab2 DISCARDING DUPLICATES ).

Inicializar tabla

lt_p0002 = VALUE #( ( pernr = l_pernr  begda = sy-datum ) ).

agregar registros a tabla (append)

lt_p0002 = VALUE #( BASE lt_p0002 ( pernr = l_pernr  begda = '20200201' ) ).

FILTER

DATA lt_p0002_s TYPE STANDARD TABLE OF p0002 WITH NON-UNIQUE SORTED KEY pernr COMPONENTS pernr.

lt_p0002_s = VALUE #( ( pernr = l_pernr     begda = '20200201' )
                      ( pernr = '00003020'  begda = '20200301' )
                   ).

DATA(lt_p0002_h) = FILTER #( lt_p0002_s USING KEY pernr WHERE pernr = l_pernr ).

* Create a filter internal table with multiple values
DATA filter_tab TYPE SORTED TABLE OF p0002-pernr
                WITH UNIQUE KEY table_line.

filter_tab = VALUE #( ( l_pernr ) ).

* Apply filters
lt_p0002_h = FILTER #( lt_p0002_s IN filter_tab
                               WHERE pernr = table_line ).

*
TYPES: BEGIN OF ty_filter,
         pernr TYPE p0002-pernr,
         begda TYPE p0002-begda,
       END OF ty_filter,
       ty_filter_tab TYPE HASHED TABLE OF ty_filter
                     WITH UNIQUE KEY pernr begda.

DATA(lt_filter_tab) = VALUE ty_filter_tab( ( pernr = l_pernr begda = '20200201' ) ).

DATA lt_p0002_3 TYPE STANDARD TABLE OF p0002.

lt_p0002_3 = VALUE #( ( pernr = l_pernr     begda = '20200201' )
                      ( pernr = '00003020'  begda = '20200301' )
                   ).

clear lt_p0002_h.

lt_p0002_h = FILTER #( lt_p0002_3 IN lt_filter_tab
                   WHERE pernr = pernr  AND begda = begda ).

ejemplo 2

DATA ti_teven TYPE TABLE OF teven.
DATA l_pernr TYPE teven-pernr.

TYPES: BEGIN OF filter,
         pernr TYPE teven-pernr,
       END OF filter,
       filter_tab TYPE HASHED TABLE OF filter
        WITH UNIQUE KEY pernr.

DO 2 TIMES.
  IF sy-index = 1.
    l_pernr = '10000307'.
  ELSE.
    l_pernr = '00000000'.
  ENDIF.

  DATA(filter_tab) = VALUE filter_tab(
      ( pernr = l_pernr ) ).

  SELECT * FROM teven
           INTO TABLE ti_teven
           ORDER BY pernr ldate ltime erdat ertim pdsnr.

  DATA(lt_teven) = FILTER #( ti_teven IN filter_tab
                 WHERE pernr = pernr ) .

ENDDO.

LOOP GROUP BY

VALUE

types ty_ra type RANGE OF vbak-auart.
 data(ra_auart) = VALUE ty_ra(
                               ( low = 'Z1' sign = 'I' option = 'EQ' )
                               ( low = 'Z2' sign = 'I' option = 'EQ' )
                             ).
  select vbeln into table @data(LT_vbeln) from vbak where auart in @ra_auart.

 DATA itab TYPE RANGE OF i. 
 itab = VALUE #( sign = 'I'  option = 'BT' ( low = 1  high = 10 ) 
                                          ( low = 21 high = 30 ) 
                                          ( low = 41 high = 50 ) 
                            option = 'GE' ( low = 61 )  ).

 data ra_ktart TYPE RANGE OF p2006-ktart.
 CASE lw_solicitudes-subty.
   WHEN '0100'.
     ra_ktart = VALUE #( ( low = '01' sign = 'I' option = 'EQ' )
                            ( low = '02' sign = 'I' option = 'EQ' )                     ).
   WHEN '0300' OR '0301' OR '0309'.
     ra_ktart = VALUE #( ( low = '17' sign = 'I' option = 'EQ' ) ).
    WHEN '0901'.
      ra_ktart = VALUE #( ( low = '10' sign = 'I' option = 'EQ' ) ).
 ENDCASE.

APPEND VALUE

   APPEND VALUE #( sign = 'I' option = 'EQ' low = 'A000' ) TO lr_einri.
   APPEND VALUE #( sign = 'I' option = 'EQ' low = 'C200' ) TO lr_einri.
   INSERT VALUE #( comp1 = a comp2 = b ... ) INTO TABLE itab.

VALUE...BASE

 TYPES: BEGIN OF ty_data2,
           col1 TYPE char1,
           col2 TYPE i,
         END OF ty_data2.

  DATA lt_tabla TYPE TABLE OF ty_data2.
  DATA lt_tabla2 TYPE TABLE OF ty_data2.

  lt_tabla = VALUE #( ( col1 = 'A' col2 = 2 ) ( col1 = 'B' col2 = 11 ) ( col1 = 'A' col2 = 23 ) ).
  LOOP AT lt_tabla INTO DATA(ls_tabla) WHERE col1 = 'A'.
    lt_tabla2 = VALUE #( BASE lt_tabla2 ( ls_tabla ) ).  "APPEND
  ENDLOOP.

Uses BASE to append rows to existing rows in an internal table.

 TYPES itab TYPE TABLE OF string WITH EMPTY KEY. 

 DATA(itab) = 
  VALUE itab( 
    ( `a` ) ( `b` ) ( `c` ) ). 

 ... 

 itab = 
  VALUE #( 
    BASE itab 
    ( `d` ) ( `e` ) ( `f` ) ).

VALUE...FOR

    DATA itab TYPE TABLE OF i WITH EMPTY KEY.
    itab = VALUE #( FOR j = 1 WHILE j <= 10 ( j ) ).

REDUCE

Creating Values with FOR and REDUCE

*Summation
  DATA(l_v1) = REDUCE i( INIT sum = 0
                      FOR n = 1 UNTIL n > 10
                      NEXT sum = sum + n ) .
*Concatenation without THEN
  data(l_v2) = REDUCE string( INIT text = `Count up:`
                            FOR n = 1 UNTIL n > 10
                            NEXT text = text && | { n }| ).
*Concatenation with THEN
  DATA(l_v3) = REDUCE string( INIT text = `Count down:`
                            FOR n = 10 THEN n - 1 WHILE n > 0
                            NEXT text = text && | { n }| ).
*Non arithmetic expression
  DATA(l_v4) = REDUCE string( INIT text = ``
                            FOR t = `x` THEN t && `y`
                                        UNTIL strlen( t ) > 10
                            NEXT text = text && |{ t } | ).
*L_V1 = 55
*L_V2 = Count up: 1 2 3 4 5 6 7 8 9 10
*L_V3 = Count down: 10 9 8 7 6 5 4 3 2 1
*L_V4 = x xy xyy xyyy xyyyy xyyyyy xyyyyyy xyyyyyyy xyyyyyyyy xyyyyyyyyy

table reduction

Summation of an Array

    DATA itab TYPE TABLE OF i WITH EMPTY KEY.
    itab = VALUE #( FOR j = 1 WHILE j <= 10 ( j ) ).
    cl_demo_output=>write( itab ).

    DATA(sum) = REDUCE i( INIT x = 0 FOR wa IN itab NEXT x = x + wa ).

CONV

data: l_dir type char200.
l_dir = 'c:\temp'.
data(l_resp) =  cl_gui_frontend_services=>directory_exist( DIRECTORY = CONV #( l_dir ) ).
DATA l_date_c(8) VALUE '20200205'.
DATA(l_day) = cl_rs_time_service=>get_weekday( i_date = CONV #( l_date_c ) ).
IF l_day > 4.
  WRITE:/ 'Solo lunes a jueves'.
  RETURN.
ENDIF.  

COND

DATA(time) = COND string( WHEN sy-timlo < '120000' THEN |{ sy-timlo TIME = ISO } AM|
                          WHEN sy-timlo > '120000' THEN |{ CONV t( sy-timlo - 12 * 3600 ) TIME = ISO } PM|
                          WHEN sy-timlo = '120000' THEN |High Noon|
                          ELSE 'nada' ).

SWITCH

DATA l_flag TYPE flag.
DATA(l_status) = SWITCH #( l_flag
                             WHEN 'X' THEN 'OK'
                             ELSE 'Error'
                         ).
DATA(l_number) = SWITCH string( sy-index WHEN 1 THEN 'uno'
                                      WHEN 2 THEN 'dos'
                                      WHEN 3 THEN 'tres'
                                      else 'nada' ).

BOOLC

 p_f = boolc( p2001[] is not initial ).

EQUIV

Equiv

SELECTION-SCREEN BEGIN OF BLOCK bl1.
PARAMETERS p_alv RADIOBUTTON GROUP radi.
PARAMETERS p_csv RADIOBUTTON GROUP radi.

PARAMETERS p_pc AS CHECKBOX.
PARAMETERS p_serv AS CHECKBOX.

SELECTION-SCREEN END OF BLOCK bl1.

AT SELECTION-SCREEN ON BLOCK bl1.

  IF p_csv = abap_true.
    IF p_pc = abap_false AND p_serv = abap_false.
      MESSAGE 'Debe seleccionar destino de archivo de salida (PC o Servidor)' TYPE 'E'.
    elseIF p_pc = abap_true EQUIV p_serv = abap_true.
      MESSAGE e000(0k) WITH 'Debe elegir solo una opcion'.
    ENDIF.
  ENDIF.

NEW

 DATA g_sol TYPE REF TO cl_sol.
 CREATE OBJECT g_sol
   EXPORTING
     i_iddocumento = p_id
     .
 data(g_sol2) = NEW cl_sol( i_iddocumento = p_id ).

Non-class-based exceptions of the instance constructor cannot be handled, which means that sy-subrc is never set to a value other than 0. The return code sy-subrc is not set when anonymous data objects are created.

OPEN SQL

SQL Expressions sql_exp

Examples of SQL Expressions

String Functions

Demos: DEMO_SQL_*

Alternativas para For All Entries

SELECT - FROM @itab

Global Temporary Tables

UNION

 
 SELECT FROM pa0002 FIELDS pernr,vorna WHERE gbdat >= '19900101' INTO TABLE @DATA(lt_pernr2).

  SELECT FROM vbrk
          INNER JOIN vbrp
            ON vbrk~vbeln = vbrp~vbeln
        FIELDS vbrk~*,vbrp~posnr,vbrp~matnr
         WHERE fkdat >= '20190101'
        INTO TABLE @DATA(itab).

*ITAB[1]-VBRK-VBELN = '0090156792'

DATA city         TYPE spfli-cityfrom VALUE 'NEW YORK'.
SELECT *
       FROM sflight AS s
       WHERE seatsocc < s~seatsmax AND
             EXISTS ( SELECT  *
                             FROM spfli
                             WHERE carrid   =  s~carrid AND
                                   connid   =  s~connid AND
                                   cityfrom = @city )
       INTO TABLE @DATA(free_flights).

DATA: carr_id TYPE spfli-carrid VALUE 'LH',
      conn_id TYPE spfli-connid VALUE '400'.
SELECT SINGLE city, latitude, longitude
       FROM sgeocity
       WHERE city IN ( SELECT cityfrom
                              FROM spfli
                              WHERE carrid = @carr_id AND
                                    connid = @conn_id )
       INTO (@DATA(city), @DATA(lati), @DATA(longi)).

      SELECT planetype, seatsmax
             FROM  saplane AS plane
             WHERE seatsmax < @wa-seatsmax AND
                   seatsmax >= ALL
                     ( SELECT seatsocc
                              FROM  sflight
                              WHERE carrid = @wa-carrid AND
                              connid = @wa-connid )
             ORDER BY seatsmax
             INTO (@DATA(plane), @DATA(seats)).

    SELECT SINGLE
           FROM demo_expressions
           FIELDS CAST( num1     AS CHAR( 20 ) ) AS col1,
                  CAST( numlong1 AS CHAR( 20 ) ) AS col2,
                  CAST( dec3     AS CHAR( 20 ) ) AS col3,
                  CAST( dats2    AS CHAR( 20 ) ) AS col4
           WHERE id = 'X'
           INTO @DATA(result).

constantes en select list

SELECT carrid, connid, '007' as tipo
       FROM spfli
       INTO  table @DATA(result).

SELECT carrid, connid, @abap_true as tipo
       FROM spfli
       INTO  table @DATA(result).

SELECT carrid, connid, @sy-datum as fecha_ejecucion
       FROM spfli
       INTO  table @DATA(result).

SELECT SINGLE vorna && ' ' && nachn && ' ' && name2 AS nombre_emp, gbdat  
                                                  INTO (@wa-nombre_emp, @wa-gbdat) FROM pa0002 
                                                 WHERE pernr = @pernr AND
                                                       begda <= @sy-datum AND
                                                       endda >= @sy-datum.

CASE

    SELECT SINGLE j~partner, i~rut_pagador, i~total_pagar, i~nom_archivo,
                 CASE i~banco_abon
                   WHEN ' ' THEN i~banco_abon2
                   ELSE i~banco_abon
                 END AS banco
                  INTO CORRESPONDING FIELDS OF @wa_data
                                             FROM zfi_histra_paso AS i INNER JOIN but000 AS j
                                                  ON i~rut_pagador = j~bu_sort2
                                                   INNER JOIN zfi_bp_datos_com AS z
                                                   ON j~partner = z~bp
                                             WHERE i~folio = @g_folio_histra
                                              AND i~rut_pagador IN @s_rut
                                              AND i~nom_archivo LIKE 'A%'
                                              AND j~partner IN @s_bp
                                              AND z~tipo_sucursal = '00' . "#EC CI_SEL_NESTED

SELECT pernr, vorna,
    CASE
      WHEN vorna <> 'Homero' THEN 'MAGIOS'
      ELSE 'NO MAGIOS'
   END AS secta
 FROM pa0002
  INTO TABLE @DATA(lt_pernr).

CONCAT

SELECT FROM   spfli
       FIELDS CONCAT( CONCAT( carrid , '_' ),
                              CAST( connid AS CHAR ) )  AS key,
              cityto, 1  AS cities
       INTO TABLE @DATA(result).

KEY     CITYTO	        CITIES
LH_0400	NEW YORK 	1
AA_0017	SAN FRANCISCO	1
AZ_0555	FRANKFURT	1

REPORT demo_sql_function_concat.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    SELECT CONCAT( CONCAT( carrid,
                           LPAD( carrname,21,' ' ) ),
                   LPAD( url,40,' ' ) ) AS line
           FROM scarr
           INTO TABLE @DATA(result).
    cl_demo_output=>display( result ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

SUBSTRING

*PRmmaaRppnnnnnn
*0208020 + aa + mm + nnnnnn
*Ejemplo: PR0720R007188233 se convierte en 02080202007188233
  SELECT concat( '0208020', ( concat( substring( xblnr,5,2 ), ( concat( substring( xblnr,3,2 ), ( substring( xblnr,10,6 ) ) ) ) ) ) ) AS folio
                        FROM bsis
                       WHERE bukrs EQ @g_bukrs
                         AND xblnr IN @s_xblnr
                AND augdt = '00000000'

   INTO CORRESPONDING FIELDS OF TABLE @gt_data . 

Literal in SELECT List

 SELECT SINGLE @abap_true
           FROM scarr
           WHERE carrid = @carrier
           INTO @DATA(exists).
      IF exists = abap_true.
        cl_demo_output=>display(
          |Carrier { carrier } exists in SCARR| ).
      ELSE.
        cl_demo_output=>display(
          |Carrier { carrier } does not exist in SCARR| ).
      ENDIF. 

aggregations

* nombre completo empleado
select VORNA && ' ' && nachn as nombre  from pa0002 into table @data(lt_data) where pernr = '10000000'.

*total factura
select netwr + MWSBK as total  from vbrk into table @data(lt_vbrk) UP TO 1 ROWS.

* facturas con total > 1000000
select netwr + MWSBK as total  from vbrk  where netwr + MWSBK > 10000 and waerk = 'CLP'
                                       into table @data(lt_vbrk).

Open SQL - Aggregate Expressions agg_exp

Host Expressions, Use in the WHERE Condition

REPORT demo_select_where_tab_exp.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA carriers TYPE HASHED TABLE OF scarr
                  WITH UNIQUE KEY carrid
                  WITH NON-UNIQUE SORTED KEY name COMPONENTS carrname.
    CLASS-METHODS:
      class_constructor,
      main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA name TYPE scarr-carrname VALUE 'United Airlines'.
    cl_demo_input=>request( CHANGING field = name ).

    SELECT carrid, connid, cityfrom, cityto
           FROM spfli
           WHERE carrid =
             @( VALUE spfli-carrid( carriers[ KEY name
                                              carrname = name ]-carrid
                                              OPTIONAL ) )
           INTO TABLE @DATA(result).

    cl_demo_output=>display( result ).
  ENDMETHOD.
  METHOD class_constructor.
    SELECT *
           FROM scarr
           INTO TABLE carriers.
  ENDMETHOD.

ENDCLASS.
START-OF-SELECTION.
  demo=>main( ).

report zdemo99.
CLASS cl_mara DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: get_maktx IMPORTING i_matnr TYPE matnr
                             RETURNING VALUE(r_maktx) TYPE maktx.

ENDCLASS.

CLASS cl_mara IMPLEMENTATION.
  METHOD get_maktx.
    SELECT SINGLE maktx INTO r_maktx FROM  makt
           WHERE  matnr  = i_matnr
           AND    spras  = sy-langu.
    IF sy-subrc <> 0.
      r_maktx = space.
    ENDIF.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
DATA l_matnr TYPE mara-matnr.

  cl_demo_input=>request( CHANGING field = l_matnr ).

  SELECT matnr,maktx FROM  makt
         WHERE  matnr  = @l_matnr
         AND    spras  = @sy-langu
     INTO TABLE @DATA(lt_makt).

  SELECT matnr,
         mtart,
         meins,
         CASE WHEN matkl = 'Z1MC00054'  THEN 'K'
          ELSE @( lt_makt[ matnr = l_matnr ]-maktx )
       END AS maktx
       FROM mara WHERE matnr = @l_matnr
       INTO TABLE @DATA(lt_mara).

  SELECT matnr,
         mtart,
         meins,
         CASE WHEN matkl = 'Z1MC00054'  THEN 'K'
          ELSE @( cl_mara=>get_maktx( i_matnr = l_matnr ) )
       END AS maktx
       FROM mara WHERE matnr = @l_matnr
       INTO TABLE @DATA(lt_mara2).

  cl_demo_output=>write( lt_mara ).
  cl_demo_output=>write( lt_mara2 ).

  cl_demo_output=>display( ).

ABAP SQL Indicator structure (ABAP 7.55 (or) S/4HANA 2020)

UPDATE dbtab, set indicators

TYPES wa TYPE sflight WITH INDICATORS ind.

DATA itab TYPE TABLE OF wa WITH EMPTY KEY.

SELECT carrid, connid, fldate, price
       FROM sflight
       WHERE carrid = char`LH` AND
             connid = numc`0400` AND
             fldate > '20180601'
       INTO CORRESPONDING FIELDS OF TABLE @itab.

IF sy-subrc  = 0.
  LOOP AT itab ASSIGNING FIELD-SYMBOL(<wa>).
    <wa>-price *= '0.8'.
    <wa>-ind-price = 'X'.
  ENDLOOP.

  UPDATE sflight FROM TABLE @itab INDICATORS SET STRUCTURE ind.
ENDIF.

JOIN

"objeto arc_doc_id tiene 1 registro en tabla toa01 y no existe en tabla toatt

select i~ar_object,j~descr
    FROM toa01 as i LEFT OUTER JOIN toaat as j on i~arc_doc_id = j~arc_doc_id
  where i~arc_doc_id = 'f14195fb-d35f-4b41-9406-4e0aeef25d31'
  INTO TABLE @DATA(result3). "tabla result3: 1 registro


select i~ar_object,j~descr
    FROM toa01 as i inner JOIN toaat as j on i~arc_doc_id = j~arc_doc_id
  where i~arc_doc_id = 'f14195fb-d35f-4b41-9406-4e0aeef25d31'
  INTO TABLE @DATA(result4). "tabla result4: 0 registro

Códigos Varios

DATA(l_day) = cl_rs_time_service=>get_weekday( i_date = sy-datum ).
IF l_day > 4.
  WRITE:/ 'Solo lunes a jueves'.
  RETURN.
ENDIF.   
IF cl_rs_time_service=>get_weekday( i_date = sy-datum ) > 4.
  WRITE:/ 'Solo lunes a jueves'.
  RETURN.
ENDIF.   
********************
DATA(components) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_name( 'T100' ) )->components.

Numeric Calculations

ipow - Integer Power Function

ipow( base = arg exp = n ), equivale a: arg ** n , pero ipow tiene mejor performance, ejemplo programa DEMO_IPOW

nmax, nmin - Numeric Extremum Functions

Syntax Forms

... nmax|nmin( val1 = arg1 val2 = arg2 [val3 = arg3] ... [val9 = arg9] ) ...

Effect

These functions return the value of the greatest or the least of the arguments passed. A minimum of two arguments, arg1 and arg2, and a maximum of nine arguments must be passed. Here, the optional input parameters val3 to val9 must be filled in ascending order without gaps. The arguments arg1 to arg9 are numeric expression positions.

General numerical functions

Ver programa demo_numerical_function

Demos

[1] Demos