根据名字获取结构 DATA: STRUCTTYPE TYPE REF TO CL_ABAP_STRUCTDESCR. STRUCTTYPE ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( 'SPFLI' ). 根据变量获取结构 DATA: DATATYPE TYPE REF TO CL_ABAP_ELEMDESCR,W_CHAR TYPE CHAR5. DATATYPE ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( W_CHAR ). 自定义数值结构 DATA: ELEMTYPE TYPE REF TO CL_ABAP_ELEMDESCR. ELEMTYPE ?= CL_ABAP_ELEMDESCR=>GET_I( ). ELEMTYPE ?= CL_ABAP_ELEMDESCR=>GET_C( 20 ). 定义动态长度的字符串 DATA: W_C10 TYPE CHAR10 VALUE 'HB',W_C20 TYPE CHAR20,W_CNT TYPE I. W_CNT = STRLEN( W_C10 ).DATA: DREF TYPE REF TO DATA,CTYPE TYPE REF TO CL_ABAP_ELEMDESCR. FIELD-SYMBOLS:<FS>.CTYPE = CL_ABAP_ELEMDESCR=>GET_C( W_CNT ).CREATE DATA DREF TYPE HANDLE CTYPE. ASSIGN DREF->* TO <FS>. *ASSIGN DREF->* TO <FS> CASTING TYPE HANDLE CTYPE.<FS> = W_C10(W_CNT). CONCATENATE '***' <FS> '***' INTO W_C20 RESPECTING BLANKS. WRITE W_C20.动态读取系统表数据 PARAMETERS P_NAME TYPE TABNAME.DATA: LR_STRUC TYPE REF TO CL_ABAP_STRUCTDESCR,LR_DATA TYPE REF TO CL_ABAP_DATADESCR,LR_TABLE TYPE REF TO CL_ABAP_TABLEDESCR,DYN_TABLE TYPE REF TO DATA,DYN_WA TYPE REF TO DATA.FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,<DYN_WA> TYPE ANY.LR_STRUC ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( P_NAME ).LR_TABLE ?= CL_ABAP_TABLEDESCR=>CREATE( P_LINE_TYPE = LR_STRUC ).*ASSIGN DYN_WA->* TO <DYN_WA> CASTING TYPE HANDLE LR_STRUC. *ASSIGN DYN_TABLE->* TO <DYN_TABLE> CASTING TYPE HANDLE LR_TABLE.CREATE DATA DYN_WA TYPE HANDLE LR_STRUC. CREATE DATA DYN_TABLE TYPE HANDLE LR_TABLE. ASSIGN DYN_WA->* TO <DYN_WA>. ASSIGN DYN_TABLE->* TO <DYN_TABLE>.SELECT * INTO CORRESPONDING FIELDS OF TABLE <DYN_TABLE>UP TO 100 ROWSFROM (P_NAME).*CALL METHOD LR_TABLE->GET_TABLE_LINE_TYPE * RECEIVING * P_DESCR_REF = LR_DATA * .CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'EXPORTINGI_STRUCTURE_NAME = P_NAMETABLEST_OUTTAB = <DYN_TABLE>EXCEPTIONSPROGRAM_ERROR = 1OTHERS = 2. IF SY-SUBRC <> 0.MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgnoWITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.动态定义表结构 TYPES:BEGIN OF TYP_PERSON,NAME TYPE CHAR10,SEX TYPE CHAR1,AGE TYPE I,END OF TYP_PERSON.DATA: ST_PERSON TYPE TYP_PERSON,TR_TABLE TYPE REF TO CL_ABAP_TABLEDESCR,TR_STRUCT TYPE REF TO CL_ABAP_STRUCTDESCR,TR_TYPE TYPE REF TO CL_ABAP_TYPEDESCR,FS_TABLE TYPE REF TO DATA,FS_STRUCT TYPE REF TO DATA,TD_COMP TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,TH_COMP LIKE LINE OF TD_COMP.FIELD-SYMBOLS:<FS_TABLE> TYPE STANDARD TABLE,<FS_STRUCT> TYPE ANY.TR_STRUCT ?= CL_ABAP_STRUCTDESCR=>DESCRIBE_BY_DATA( ST_PERSON ). TD_COMP = TR_STRUCT->GET_COMPONENTS( ).CALL METHOD CL_ABAP_STRUCTDESCR=>CREATEEXPORTINGP_COMPONENTS = TD_COMP * P_STRICT = TRUE RECEIVINGP_RESULT = TR_STRUCT.CALL METHOD CL_ABAP_TABLEDESCR=>CREATEEXPORTINGP_LINE_TYPE = TR_STRUCTRECEIVINGP_RESULT = TR_TABLE.CREATE DATA FS_STRUCT TYPE HANDLE TR_STRUCT. CREATE DATA FS_TABLE TYPE HANDLE TR_TABLE. ASSIGN FS_STRUCT->* TO <FS_STRUCT>. ASSIGN FS_TABLE->* TO <FS_TABLE>.动态读取表结构 TYPES:BEGIN OF TYP_PERSON,NAME TYPE CHAR10,SEX TYPE CHAR1,AGE TYPE I,END OF TYP_PERSON.DATA: TH_PERSON TYPE TYP_PERSON,TD_PERSON TYPE TABLE OF TYP_PERSON.PERFORM FRM_SWAP_TABLE TABLES TD_PERSON. *&---------------------------------------------------------------------* *& Form FRM_SWAP_TABLE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->PTD_PERSON text *----------------------------------------------------------------------* FORM FRM_SWAP_TABLE TABLES PTD_PERSON. DATA: FS_TABLE TYPE REF TO DATA,FS_STRUCT TYPE REF TO DATA,TR_TABLE TYPE REF TO CL_ABAP_TABLEDESCR,TR_STRUCT TYPE REF TO CL_ABAP_STRUCTDESCR,TR_TYPE TYPE REF TO CL_ABAP_TYPEDESCR.FIELD-SYMBOLS:<FS_TABLE> TYPE STANDARD TABLE,<FS_STRUCT> TYPE ANY.TR_STRUCT ?=CL_ABAP_TABLEDESCR=>DESCRIBE_BY_DATA( P_DATA = PTD_PERSON ).CREATE DATA FS_STRUCT TYPE HANDLE TR_STRUCT. ASSIGN FS_STRUCT->* TO <FS_STRUCT>. CREATE DATA FS_TABLE LIKE TABLE OF <FS_STRUCT>. ASSIGN FS_TABLE->* TO <FS_TABLE>.ENDFORM. " FRM_SWAP_TABLE
转载于:https://www.cnblogs.com/loveplxf/archive/2012/08/15/2640767.html