2018年9月26日 星期三

ABAP/4 讀取 Database table, store procedure
  1. data:
  2. PAR1(30) type C,
  3. PAR2(30) type C,
  4. OUT1 type I,
  5. OUT2(30) type C,
  6. OUT3(30) type C.
  7.  
  8. exec sql performig sp_return.
  9. select * from $PROC$sp_dummy
  10. where ( :par1, :par2 )
  11. into :out1, :out2, : out3
  12. endexec.
  13.  
  14. form sp_return.
  15. * some coding
  16. endform

General Types
Predefined data types without special semantic attributes.
Numeric Types
TypeValid Places mInitial ValueMeaningABAP Type
INT1301-byte integer, 0 to 255b
INT2502-byte integer, -32,768 to 32,767s
INT41004-byte integer, -2,147,483,648 to +2,147,483,647i
INT81908-byte integer, -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807int8
DEC1-310Packed number BCD formatp, length DIV 2 + 1
DF16_DEC1-150Decimal floating point number stored in BCD formatdecfloat16
DF16_RAW160Decimal floating point number stored in binary formatdecfloat16
DF34_DEC1-310Decimal floating point number stored in BCD formatdecfloat34
DF34_RAW340Decimal floating point number stored in binary formatdecfloat34
FLTP160Floating point numberf

Character-Like Types
TypeValid Places mInitial ValueMeaningABAP Type
CHAR1-30000, maximum of 1333 for table fieldsm blanksCharacter stringc, length m
LCHR256-32000Nonelong character stringc, length m
SSTRING1-1333Empty stringCharacter stringstring
STRING256-...Empty stringCharacter string (CLOB)string

Byte-like types
TypeValid Places mInitial ValueMeaningABAP Type
RAW1-32000 maximum of 255 for table fieldsNoneByte stringx, length m
LRAW256-32000NoneLong byte stringx, length m
RAWSTRING256-...Empty stringByte string (BLOB)xstring

Special Types
Predefined data types with special semantic attributes.
Date Types/Time Types
TypeValid Places mInitial ValueMeaningABAP Type
DATS800000000Date in the format YYYYMMDDd
TIMS6000000Time in the format HHMMSSt
ACCP66 blanksPosting period in the format YYYYMMn, length 6

Character-Like Types with Special Semantics
TypeValid Places mInitial ValueMeaningABAP Type
NUMC1-255m zeroesNumeric textn, length m
CLNT3000Clientc, length 3
LANG1BlankLanguage keyc, length 1

Currency Fields and Quantity Fields
TypeValid Places mInitial ValueMeaningABAP Type
CURR1-310Currency field in BCD formatp, length DIV 2 + 1
CUKY55 blanksCurrency key for currency fieldsc, length 5
QUAN1-310Quantity field in BCD formatp, length DIV 2 + 1
UNIT2-32 or 3 blanksUnit key of a quantity fieldc, length m

Obsolete Types
TypeValid Places mInitial ValueMeaningABAP Type
DF16_SCL160Decimal floating point number stored in binary format with scaling specified (obsolete)decfloat16
DF34_SCL340Decimal floating point number stored in binary format with scaling specified (obsolete)decfloat34
PREC20Obsolete data types
VARC1-...NoneObsolete data typec, length m



Character-Like Types and Byte-Like Types
Character Strings
The following predefined data types in ABAP Dictionary are available for general character strings:
  • CHAR for text fields
The predefined type CHAR, mapped to the ABAP type c, is used to describe general text fields with a maximum length of 30000 characters (only 1333 characters for table fields).
  • LCHR for long text fields
The predefined type LCHR, also mapped to the ABAP type c, is used to describe general text fields with lengths between 256 and 32000 characters. In database tables, the maximum length must be specified as the value of a preceding INT2 or INT4 field. Table fields of this type cannot be used as key fields of database tables or in any positions in Open SQL statements.
The predefined type SSTRING is mapped to the ABAP type string, but handled like the type CHAR or VARCHAR in ABAP Dictionary and by database tables. The length is restricted to a maximum of 1333, but table fields of this type can also be used as key fields by database tables and used by Open SQL statements in almost all positions where text fields are possible.
The predefined type STRING is mapped to the ABAP type string and also handled like a text string (CLOB) in ABAP Dictionary and by database tables. Table fields of this type cannot be used as key fields or index fields in database tables or in any positions in Open SQL statements.
Notes
  • In general structures, the length of a component of type CHAR is not restricted to 1333.
  • In database tables, there can be only one table field of the type LCHR and it must be the last table field. This means that the type LRAW cannot be used simultaneously in the same database table. If the statement SELECT is used to read an LCHR field, the preceding length field must also be read. In writes using Open SQL, the length field must be given the correct value or data may be lost.
  • LCHR is no longer recommended for new developments. The type STRING is recommended instead, however it is also not suitable for key fields of database tables or for unrestricted use in Open SQL. Where possible, SSTRING can be used here instead.
  • Switching existing dictionary objects from LCHR to STRING or SSTRING, on the other hand, can be critical, since all ABAP types and ABAP objects that reference dictionary objects like this are then given a deep data type. A switch from a flat data type to a deep data type usually constitutes an incompatible change and can produce syntax errors in ABAP programs.

Example
Like the example for literals; the row to be read is here also specified using host variables. The addition STRUCTURE is specified after INTO. However, this is not necessary since wa can be identified statically as a structure. The structure wa is handled in the INTO clause as if all substrings were specified separately: INTO :wa-cityfrom, :wa-cityto.
PARAMETERS: p_carrid TYPE spfli-carrid,
            p_connid TYPE spfli-connid.

DATA: BEGIN OF wa,
        cityfrom TYPE spfli-cityfrom,
        cityto   TYPE spfli-cityto,
      END OF wa.

EXEC SQL.
  SELECT cityfrom, cityto
         INTO STRUCTURE :wa
         FROM spfli
         WHERE mandt  = :sy-mandt AND
               carrid = :p_carrid AND connid = :p_connid
ENDEXEC. 



Example
Reads multiple rows from the database table SPFLI using cursor handling and host variables in static Native SQL. If rows are found, sy-subrc is set to 0 and sy-dbcnt is increased by one for each row read.
PARAMETERS p_carrid TYPE spfli-carrid.

DATA:  connid   TYPE spfli-connid,
       cityfrom TYPE spfli-cityfrom,
       cityto   TYPE spfli-cityto.

EXEC SQL.
  OPEN dbcur FOR
    SELECT connid, cityfrom, cityto
           FROM spfli
           WHERE mandt  = :sy-mandt AND
                 carrid = :p_carrid
ENDEXEC.

DO.
  EXEC SQL.
    FETCH NEXT dbcur INTO :connid, :cityfrom, :cityto
  ENDEXEC.
  IF sy-subrc <> 0.
    EXIT.
  ELSE.
    ...
  ENDIF.
ENDDO.

EXEC SQL.
  CLOSE dbcur
ENDEXEC. 

Bad Example
Reads multiple rows from the database table SCARR and calls the subroutine evaluate for each row read.
DATA wa TYPE spfli-carrid.

EXEC SQL PERFORMING evaluate.
  SELECT carrid FROM spfli INTO :wa WHERE mandt = :sy-mandt
ENDEXEC.
cl_demo_output=>display( ).

FORM evaluate.
  cl_demo_output=>write( |{ wa } | ).
ENDFORM.
Good Example
Explicit cursor processing.
DATA wa TYPE spfli-carrid.

EXEC SQL.
  OPEN CUR FOR
  SELECT carrid FROM spfli WHERE mandt = :sy-mandt
ENDEXEC.

DO.
  EXEC SQL.
    FETCH NEXT CUR INTO :wa
  ENDEXEC.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  cl_demo_output=>write( |{ wa } | ).
ENDDO.
cl_demo_output=>display( ).

沒有留言:

張貼留言