ABAP/4 讀取 Database table, store procedure
- data:
- PAR1(30) type C,
- PAR2(30) type C,
- OUT1 type I,
- OUT2(30) type C,
- OUT3(30) type C.
- exec sql performig sp_return.
- select * from $PROC$sp_dummy
- where ( :par1, :par2 )
- into :out1, :out2, : out3
- endexec.
- form sp_return.
- * some coding
- endform
General Types
Predefined data types without special semantic attributes.
Numeric Types
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
INT1 | 3 | 0 | 1-byte integer, 0 to 255 | b |
INT2 | 5 | 0 | 2-byte integer, -32,768 to 32,767 | s |
INT4 | 10 | 0 | 4-byte integer, -2,147,483,648 to +2,147,483,647 | i |
INT8 | 19 | 0 | 8-byte integer, -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 | int8 |
DEC | 1-31 | 0 | Packed number BCD format | p, length m DIV 2 + 1 |
DF16_DEC | 1-15 | 0 | Decimal floating point number stored in BCD format | decfloat16 |
DF16_RAW | 16 | 0 | Decimal floating point number stored in binary format | decfloat16 |
DF34_DEC | 1-31 | 0 | Decimal floating point number stored in BCD format | decfloat34 |
DF34_RAW | 34 | 0 | Decimal floating point number stored in binary format | decfloat34 |
FLTP | 16 | 0 | Floating point number | f |
Character-Like Types
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
CHAR | 1-30000, maximum of 1333 for table fields | m blanks | Character string | c, length m |
LCHR | 256-32000 | None | long character string | c, length m |
SSTRING | 1-1333 | Empty string | Character string | string |
STRING | 256-... | Empty string | Character string (CLOB) | string |
Byte-like types
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
RAW | 1-32000 maximum of 255 for table fields | None | Byte string | x, length m |
LRAW | 256-32000 | None | Long byte string | x, length m |
RAWSTRING | 256-... | Empty string | Byte string (BLOB) | xstring |
Special Types
Predefined data types with special semantic attributes.
Date Types/Time Types
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
DATS | 8 | 00000000 | Date in the format YYYYMMDD | d |
TIMS | 6 | 000000 | Time in the format HHMMSS | t |
ACCP | 6 | 6 blanks | Posting period in the format YYYYMM | n, length 6 |
Character-Like Types with Special Semantics
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
NUMC | 1-255 | m zeroes | Numeric text | n, length m |
CLNT | 3 | 000 | Client | c, length 3 |
LANG | 1 | Blank | Language key | c, length 1 |
Currency Fields and Quantity Fields
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
CURR | 1-31 | 0 | Currency field in BCD format | p, length m DIV 2 + 1 |
CUKY | 5 | 5 blanks | Currency key for currency fields | c, length 5 |
QUAN | 1-31 | 0 | Quantity field in BCD format | p, length m DIV 2 + 1 |
UNIT | 2-3 | 2 or 3 blanks | Unit key of a quantity field | c, length m |
See Currency Fields and Quantity Fields.
Obsolete Types
Type | Valid Places m | Initial Value | Meaning | ABAP Type |
DF16_SCL | 16 | 0 | Decimal floating point number stored in binary format with scaling specified (obsolete) | decfloat16 |
DF34_SCL | 34 | 0 | Decimal floating point number stored in binary format with scaling specified (obsolete) | decfloat34 |
PREC | 2 | 0 | Obsolete data type | s |
VARC | 1-... | None | Obsolete data type | c, 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.
- SSTRING for short text strings
- 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.
- STRING for text strings
- 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.
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.
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.
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( ).
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( ).
沒有留言:
張貼留言