Fundamentos de programación en ABAP

Anuncio
Fundamentos de programación en ABAP
ACTIVIDAD:
1. Reutilizando el programa ejemplo de la lección 4, cópielo con otro nombre y
adicione las sentencias que permitan mostrar los siguientes datos desde las
posiciones de un documento de compras:

Posición: EKPO-EBELN

Centro: EKPO-WEKRS

Material: EKPO-MATNR

Descripción del material EKPO-TXZ01
Pero antes de empezar tenga en cuenta la siguiente información:
a. La tabla de posiciones de pedidos es EKPO.
b. Para declarar una tabla debe utilizar el siguiente código:
* Tabla y estructura de pos.pedido
DATA: it_ekpo TYPE STANDAR TABLE OF ekpo,
wa_ekpo TYPE ekpo.
Se declara una tabla interna de tipo SORTED, es decir, que siempre permanece
ordenada y una estructura (work aérea) del mismo tipo de la tabla estándar.
c. Para leer varias posiciones de un pedido la sentencia SQL sería:
* lee las posiciones de pedidos
SELECT *
INTO TABLE it_ekpo
FROM ekpo
FOR ALL ENTRIES IN it_ekko
WHERE ebeln = it_ekko-ebeln.
Veamos en detalle el operando FOR ALL ENTRIES IN permite obtener todas las
líneas de la tabla anterior it_ekko, es muy recomendada para mejorar la
eficiencia de lectura de tablas.
d. Para recorrer todas las posiciones de un pedido, utilizar la siguiente
sentencia:
*
recorre la tabla de posiciones de pedido
LOOP AT it_ekpo INTO wa_ekpo WHERE ebeln = wa_ekko-ebeln.
…
ENDLOOP.
1
Fundamentos de programación en ABAP
Solución:
*&---------------------------------------------------------------------*
*& Report ZPRUEBA_ANG1
*
*&
*
*&---------------------------------------------------------------------*
*& Programa de prueba para leer posiciones EKPO
*
*&---------------------------------------------------------------------*
REPORT zprueba1.
* Declaración de variables globales
DATA wa_ekko TYPE ekko. "tabla de pedidos de compras MM
* Tabla y estructura de pos.pedido
DATA: it_ekpo TYPE STANDAR TABLE OF ekpo,
wa_ekpo TYPE ekpo.
* Declara parametro de entrada
PARAMETERS: p_ebeln TYPE ebeln. "Numero pedido
*---------- Inicio de procesos -----------*
START-OF-SELECTION.
"Inicio del programa
* Lee datos de cabecera del pedido
SELECT SINGLE *
FROM ekko
INTO wa_ekko
WHERE ebeln = p_ebeln.
IF sy-subrc = 0.
WRITE:'Documento ', wa_ekko-ebeln,
'Sociedad ', wa_ekko-bukrs,
'Fecha ',
wa_ekko-aedat.
* lee las posiciones de pedidos
SELECT *
INTO TABLE it_ekpo
FROM ekpo
FOR ALL ENTRIES IN it_ekko
WHERE ebeln = it_ekko-ebeln.
WRITE:'Documento ', wa_ekko-ebeln,
'Sociedad ', wa_ekko-bukrs,
'Fecha ',
wa_ekko-aedat.
IF sy-subrc = 0.
*
recorre la tabla de posiciones de pedido
LOOP AT it_ekpo INTO wa_ekpo WHERE ebeln = wa_ekko-ebeln.
WRITE:/ ‘Posición: ‘, wa_ekpo-ebelp,
‘Centro:’, wa_ekpo-werks,
‘Material:’, wa_ekpo-matnr,
‘Descripción: ‘, wa_ekpo-txz01.
ENDLOOP.
ENDIF.
ELSE.
WRITE 'NO EXISTE PEDIDO'.
ENDIF.
2
Fundamentos de programación en ABAP
2. Tendrá muchas dudas sobre información formal del lenguaje ABAP, por favor
búsquela en internet, le recomiendo Wikimedia.
ABAP provides a set of built-in data types. In addition, every
structure, table, view or data element defined in the ABAP Dictionary
can be used to type a variable. Also, object classes and interfaces
can be used as types.
The built-in data types are:
Type
Description
I
Integer (4-bytes)
P
Packed decimal
F
Floating point
N
Character numeric
C
Character
D
Date
T
Time
X
Hexadecimal (raw byte)
STRING Variable-length string
XSTRING
Variable-length raw byte array
ABAP statements – an overview
In contrast with languages like C/C++ or Java, which define a limited
set of language-specific statements and provide most functionality
via libraries, ABAP contains an extensive body of built-in
statements. These statements often support many options, which
explains why ABAP programs look "verbose", especially when compared
with programs written in C, C++ or Java.
This section lists some of the most important statements in the
language, subdivided by function. Both the statements listed here and
the subdivision used are fairly arbitrary and by no means exhaustive.
[edit]Declarative statements
These statements define data types or declare data objects which are
used by the other statements in a program or routine. The collected
declarative statements in a program or routine make up its
declaration part.
Examples of declarative statements:
TYPES, DATA, CONSTANTS, PARAMETERS, SELECT-OPTIONS, TABLES
[edit]Modularization statements
These statements define the processing blocks in an ABAP program.
The modularization statements can be further divided into event
statements and defining statements:
Event statements
These are used to define the beginning of event processing blocks.
There are no special statements to mark the end of such blocks - they
end when the next processing block is introduced.
Examples of event keywords are:
LOAD OF PAGE,INITIALIZATION,AT SELECTION SCREEN OUTPUT,AT SELECTION
SCREEN ON FIELD, AT SELECTION SCREEN ON BLOCK,
AT SELECTION SCREEN, START-OF-SELECTION,END-OF-SELECTION, AT USERCOMMAND, AT LINE-SELECTION,GET,GET LATE,AT USER COMMAND,
AT LINE SELECTION
Defining statements
These statements delineate callable code units such as subroutines,
function modules and methods. The statement marking the end of the
unit has the name of the opening statement prefixed with "END".
Examples of defining keywords:
FORM ..... ENDFORM, FUNCTION ... ENDFUNCTION,
MODULE ... ENDMODULE, METHOD ... ENDMETHOD
[edit]Control statements
3
Fundamentos de programación en ABAP
These statements control the flow of the program within a processing
block.
Statements controlling conditional execution are:
IF ... ELSEIF ... ELSE ... ENDIF
CASE ... WHEN ... ENDCASE
CHECK
The CHECK statement verifies a condition and exits the current
processing block (e.g. loop or subroutine) if the condition is not
satisfied.
Several statements exist to define a loop:
DO ... ENDDO
WHILE ... ENDWHILE
LOOP ... ENDLOOP
DO/ENDDO defines an unconditional loop. An exit condition (typically
in the form "IF <condition>. EXIT. ENDIF.") must be provided inside
the body of the loop. A variant (DO <n> TIMES) sets as exit condition
the number of times the loop body is executed. WHILE/ENDWHILE defines
a conditional loop. The condition is tested at the beginning of the
loop. LOOP/ENDLOOP loops over the lines of an internal table. The
loop ends after processing the last line of the internal table.
[edit]Call statements
These statements call processing blocks defined using the
corresponding modularization statements. The blocks can either be in
the same ABAP program or in a different program.
Examples of call keywords:
PERFORM, CALL METHOD, CALL TRANSACTION, CALL SCREEN, SUBMIT, LEAVE TO
transaction
[edit]Operational statements
These statements retrieve or modify the contents of variables.
A first group of operational statements assign or change a variable:
MOVE, ADD, SUBTRACT, DIVIDE
These statements, whose syntax originates in COBOL, can be written in
a shorter form that uses operators rather than keywords:
MOVE LASTNAME TO RECIPIENT.
* is equivalent to
RECIPIENT = LASTNAME.
ADD TAX TO PRICE.
* is equivalent to
PRICE = PRICE + TAX.
Examples of operational statements on character strings:
SEARCH, REPLACE, CONCATENATE, CONDENSE
Database access statements (Open SQL):
SELECT, INSERT, UPDATE, DELETE, MODIFY
Statements working on internal tables (notice that some "SQL"
statements can also be used here):
READ TABLE, INSERT, DELETE, MODIFY, SORT, DELETE ADJACENT DUPLICATES,
APPEND, CLEAR, REFRESH, FREE
[edit]Formatting statements
These statements produce or format output. They appear mainly in
reports, less so in module pools. Examples are:
WRITE, FORMAT, SKIP, ULINE, MESSAGE, NEW-PAGE, FREE
4
Descargar