SE38 - SAP ABAP DESCRIBE使用示例代码
REPORT YZLL_DESC_TEST.
CLASS DEMO DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: DESC_FIELD, DESC_DISTANCE.
METHODS DESC_TABLE CHANGING ITAB TYPE ANY TABLE..
ENDCLASS.
CLASS DEMO IMPLEMENTATION.
METHOD DESC_FIELD.
DATA: LV_DATE TYPE SY-DATUM.
DESCRIBE FIELD LV_DATE
TYPE DATA(TYP)
LENGTH DATA(ILEN_C) IN CHARACTER MODE
"LENGTH DATA(ILEN_B) in BYTE MODE
DECIMALS DATA(DES)
OUTPUT-LENGTH DATA(OLEN)
HELP-ID DATA(HLP)
EDIT MASK DATA(MASK).
LV_DATE = SY-DATUM.
PERFORM PRINTHEADER USING '1、DESC_FIELD'.
"WRITE:/ '=============DESCRIBE FIELD============='.
WRITE:/ '输出格式:', LV_DATE,
/ '类型为:', TYP,
/ '字符长度为:', ILEN_C,
"/ '字节长度为:', ILEN_B,
/ '小数位为:', DES,
/ '输出长度为:', OLEN,
/ '搜索帮助为:', HLP,
/ '修改模式为:', MASK.
SKIP.
DATA: BEGIN OF STRUC1,
COMP1 TYPE C LENGTH 1,
COMP2 TYPE STRING,
BEGIN OF STRUC2,
COMP1 TYPE C LENGTH 1,
COMP2 TYPE I,
END OF STRUC2,
END OF STRUC1.
DESCRIBE FIELD: STRUC1 TYPE DATA(TYP1) COMPONENTS DATA(COMP1),
STRUC1-STRUC2 TYPE DATA(TYP2) COMPONENTS DATA(COMP2).
WRITE:/ 'STRUC1-COMP1 类型为:', TYP1,
/ 'STRUC1-STRUC2-COMP2 类型为:', TYP2.
SKIP.
DATA: TEXT TYPE C LENGTH 1 VALUE 'A',
BYTES TYPE I.
DESCRIBE FIELD TEXT LENGTH DATA(BLEN) IN BYTE MODE.
DESCRIBE FIELD TEXT LENGTH DATA(CLEN) IN CHARACTER MODE.
BYTES = BLEN / CLEN.
WRITE:/ '输出格式:',TEXT,
/ '字节长度为:', BLEN,
/ '字符长度为:', CLEN,
/ '单字符所需字节数为:', BYTES.
SKIP.
DATA:
BEGIN OF STRUCT,
TEXT TYPE C LENGTH 3,
HEX TYPE X LENGTH 3,
END OF STRUCT.
STRUCT-TEXT = '123'.
"STRUCT-HEX = 'ABC'.
DESCRIBE FIELD STRUCT LENGTH DATA(SLEN) IN BYTE MODE.
"cl_demo_output=>display( STRUCT ).
WRITE:/ '输出格式:',STRUCT-TEXT, STRUCT-HEX,
/ '对比格式:','123', 'ABC',
/ '字节长度为:', SLEN.
SKIP.
DATA: CARRID TYPE SPFLI-CARRID,
STRUC TYPE DFIES-TABNAME,
COMP TYPE DFIES-FIELDNAME.
DESCRIBE FIELD CARRID HELP-ID DATA(CHLP).
WRITE:/ 'CARRID的搜索帮助为:', CHLP. "SPFLI-CARRID
"通过函数调用帮助
SPLIT CHLP AT '-' INTO STRUC COMP. "SPFLI CARRID
CALL FUNCTION 'F4IF_FIELD_VALUE_REQUEST'
EXPORTING
TABNAME = STRUC
FIELDNAME = COMP
EXCEPTIONS
FIELD_NOT_FOUND = 1
NO_HELP_FOR_FIELD = 2
INCONSISTENT_HELP = 3
NO_VALUES_FOUND = 4
OTHERS = 5.
DATA SPFLI TYPE SPFLI.
ASSIGN COMPONENT 'CARRID' OF STRUCTURE SPFLI TO FIELD-SYMBOL(<FS>).
DESCRIBE FIELD <FS> HELP-ID DATA(CHLP2).
WRITE:/ '<FS>的搜索帮助为:', CHLP2. "SPFLI-CARRID
SKIP.
DATA: TIME TYPE S_FLTIME,
SECONDS TYPE I,
OUTPUT TYPE C LENGTH 10.
DESCRIBE FIELD TIME EDIT MASK DATA(MSK).
SECONDS = 333.
WRITE SECONDS TO OUTPUT USING EDIT MASK MSK.
WRITE:/ '原值为:', SECONDS,
/ 'TIME修改模式为:', MSK,
/ '重写后为:', OUTPUT .
ENDMETHOD.
METHOD DESC_TABLE.
DESCRIBE TABLE ITAB KIND DATA(TABKIND) "SYDES_KIND-STANDARD SYDES_KIND-HASHED SYDES_KIND-SORTED
LINES DATA(TABLINE) " lines( ITAB )
OCCURS DATA(TABNO).
WRITE:/ '表类型为:', TABKIND, ',表行数为:', TABLINE, "lines( ITAB )
',表初始空间为:', TABNO.
ENDMETHOD.
METHOD DESC_DISTANCE.
DATA TEXT TYPE C LENGTH 10 VALUE '0123456789'.
DESCRIBE DISTANCE BETWEEN TEXT+2(1) AND TEXT+7(1)
INTO DATA(CDIST)
IN CHARACTER MODE.
DESCRIBE DISTANCE BETWEEN TEXT+2(1) AND TEXT+7(1)
INTO DATA(BDIST)
IN BYTE MODE.
DATA(CHARSTART) = TEXT+2(1) .
DATA(CHAREND) = TEXT+7(1) .
PERFORM PRINTHEADER USING '3、DESC_DISTANCE'.
WRITE:/ '源字符串为:',TEXT, ',字符 ', CHARSTART , '到字符 ', CHAREND, '的距离:',
/ '字符距离为:', CDIST, ',字节记录为:', BDIST.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
"类方法:
DEMO=>DESC_FIELD( ).
"实例方法:
DATA: OBJ TYPE REF TO DEMO.
DATA: ITAB1 TYPE TABLE OF SFLIGHT INITIAL SIZE 10 WITH EMPTY KEY,
"ITAB2 TYPE STANDARD TABLE OF SFLIGHT,
ITAB3 TYPE SORTED TABLE OF VBAP WITH NON-UNIQUE KEY VBELN,
ITAB4 TYPE HASHED TABLE OF SPFLI WITH UNIQUE KEY CARRID CONNID.
SELECT CARRID FROM SCARR INTO TABLE @DATA(ITAB2).
PERFORM PRINTHEADER USING '2、DESC_TABLE'.
WRITE:/ '表类型说明:',
/ SYDES_KIND-STANDARD && ': SYDES_KIND-STANDARD' ,
/ SYDES_KIND-SORTED && ': SYDES_KIND-SORTED' ,
/ SYDES_KIND-HASHED && ': SYDES_KIND-HASHED'.
SKIP.
CREATE OBJECT OBJ.
CALL METHOD OBJ->DESC_TABLE CHANGING ITAB = ITAB1.
CALL METHOD OBJ->DESC_TABLE CHANGING ITAB = ITAB2.
CALL METHOD OBJ->DESC_TABLE CHANGING ITAB = ITAB3.
CALL METHOD OBJ->DESC_TABLE CHANGING ITAB = ITAB4.
"类方法:
DEMO=>DESC_DISTANCE( ).
"FORM:格式化表头输出
FORM PRINTHEADER USING VALUE(DESC_TYPE) TYPE STRING.
SKIP 2.
WRITE:/ '=============',DESC_TYPE ,'============='.
ENDFORM.