F1 Find a table similar to existing fields

*&---------------------------------------------------------------------*
*& Report ZDQUERY
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZME12_BDC1.

TYPE-POOLS : ABAP.
DATA where_tab(80OCCURS 10 WITH HEADER LINE.
DATA where_tab2(80OCCURS 10 WITH HEADER LINE.
DATA : wa_itab LIKE makt OCCURS 10 WITH HEADER LINE.
*DATA : idetails TYPE abap_compdescr_tab,
*           xdetails TYPE abap_compdescr.
DATA:BEGIN OF get_table occurs ,
  TABNAME TYPE string,
END OF get_table.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
PARAMETERS:p_field TYPE string OBLIGATORY.
PARAMETERS: s_trx  TYPE string LOWER CASE." LOWER CASE 大小写不转换
SELECTION-SCREEN END OF BLOCK B1.

INITIALIZATION.

START-OF-SELECTION.
*--->去掉前导 0
*  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
*    EXPORTING
*      input  = s_trx
*    IMPORTING
*      output = s_trx.

  CONCATENATE   p_field ' LIKE ''%' s_trx '%''' INTO where_tab.
  APPEND where_tab.

TYPE-POOLS : ABAP.
FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
               <DYN_WA>,
               <DYN_FIELD>.
DATA: dy_table TYPE REF TO data,
      dy_line  TYPE REF TO data,
      xfc TYPE lvc_s_fcat,
      ifc TYPE lvc_t_fcat.

  DATA:wa_structure TYPE lvc_s_fcat,
           wa_structure1 TYPE lvc_s_fcat,
           gt_structure TYPE lvc_t_fcat.

DATA: gs_layout TYPE slis_layout_alv,
          gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
          wa_fieldcat TYPE slis_fieldcat_alv.

START-OF-SELECTION.
     PERFORM data_table.
     PERFORM get_col.
     PERFORM alv_show.
END-OF-SELECTION.

FORM data_table.
DATA: l_count TYPE i.

DATA : ref_table_des TYPE REF TO cl_abap_structdescr.
DATA : ef_is_unicode LIKE  /bdl/tasks-status.
DATA:l_value TYPE string.
DATA: l_index TYPE sy-tabix,
          lv_comt TYPE i,
          l_num TYPE ,
          p_len TYPE n.

     gs_layout-colwidth_optimize 'X'.
     p_len strlen( s_trx ).
      "查询ABAP 所有table表格
      SELECT dd02vv~TABNAME COUNT(*FROM dd02vv
        INNER JOIN dd02l ON  dd02l~tabname EQ dd02vv~tabname
        INNER JOIN dd03l ON dd03l~tabname EQ dd02vv~tabname
* The UP the TO 10 ROWS
        the INTO CORRESPONDING  the FIELDS  . OF  TABLE get_table
        the WHERE dd02vv ~ tabclass  the IN  ( 'the TRANSP' , 'the CLUSTER' , 'the POOL' the AND  "Transparent Table
                    dd02vv ~ contflag  NE  'L'  the AND  " when the stored data table, an empty transfer
                    dd02vv ~ as4local  the nOT  the IN  (   'N' , 'T'  the AND  "is not enabled, a temporary version
                    dd03l ~ fieldName  the EQ p_field  the AND
                    dd03l~ intlen> = p_len  the AND
                    is_gtt  NE  'X-'  "X-global temporary table N is not a global temporary table
            the GROUP  BY dd02vv ~ TABNAME  .

          " character lowercase conversion uppercase
          TRANSLATE p_field  to  the UPPER  the CASE .
           "Create inner structure of
          the PERFORM new_teble .
          " Creating the table
          create_dynamic_itab PERFORM .

          LOOP  AT get_table .
            l_index  = SY -tabix .
            CLEAR : l_num .
           "inquiry form content has a value
            the SELECT  COUNT (* INTO l_num
                UP  the TO  ROWS
                FROM (get_table-tabname).
               IF sy-subrc <> 0.
                 DELETE get_table INDEX l_index.
                  CONTINUE.
               ENDIF.
**               DESCRIBE TABLE get_table-tabname LINES index_z
**               n = lines(get_table-tabname).
*
*             "***获取表结构
*               CLEAR:ef_is_unicode,idetails[],xdetails,
*                         ref_table_des.
*               " check the system is unicode or nonunicode
*               CALL FUNCTION '/BDL/CHECK_UNICODE'
*                 IMPORTING
*                   ef_is_unicode = ef_is_unicode.
**              get the structure of the table.
*               ref_table_des ?=
*               cl_abap_typedescr=>describe_by_name( get_table-tabname ).
*               idetails[] = ref_table_des->components[].
*
*               "先获取每个table字段 与查找的table是否相同
*               READ TABLE idetails INTO wa WITH KEY name = p_field BINARY SEARCH.
*               IF sy-subrc = 0 .
                 CLEAR:l_value.
                    SELECT SINGLE (p_field)
                        FROM (get_table-tabname)
                        INTO @l_value
                        WHERE (where_tab).
                      IF sy-subrc 0.
                          ASSIGN COMPONENT 'TABLE_NAME' OF STRUCTURE <dyn_wa> TO <dyn_field>.
                          <dyn_field> = get_table-tabname.

                          ASSIGN COMPONENT 'FIELD' OF STRUCTURE <dyn_wa> TO <dyn_field>.
                          <dyn_field> = p_field.

                          ASSIGN COMPONENT 'VALUE' OF STRUCTURE <dyn_wa> TO <dyn_field>.
                          <dyn_field> = l_value .
                          APPEND <dyn_wa> TO <dyn_table>CLEAR <dyn_wa>.
                      ENDIF.
*              ENDIF.
          ENDLOOP.
ENDFORM.

*&---------------------------------------------------------------------*
*&      form  create_dynamic_itab
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form create_dynamic_itab.
* create dynamic internal table and assign to fs
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      i_length_in_byte 'x'
      it_fieldcatalog  = ifc
    IMPORTING
      ep_table         = dy_table.
  ASSIGN dy_table->* TO <dyn_table>.
* create dynamic work area and assign to fs
  CREATE DATA dy_line LIKE LINE OF <dyn_table>.
  ASSIGN dy_line->* TO <dyn_wa>.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form NEW_TEBLE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM NEW_TEBLE .
  CLEAR xfc.
*      xfc-tabname = 'TABLE_NAME'.
      xfc-fieldname 'TABLE_NAME' .
      xfc-datatype 'C'.
      xfc-inttype 'C'.
      xfc-intlen 30.
      "小数点
*      xfc-decimals = xdetails-decimals.
      APPEND  xfc TO ifc.
  CLEAR xfc.
*   xfc-tabname = 'TABLE_FIELD'.
   xfc-fieldname 'FIELD'."p_field .
   xfc-datatype 'C'.
   xfc-inttype 'C'.
   xfc-intlen 30.
   "小数点
*   xfc-decimals = xdetails-decimals.
   APPEND  xfc TO ifc.

  CLEAR xfc.
*   xfc-tabname = 'TABLE_FIELD'.
   xfc-fieldname 'VALUE'.
   xfc-datatype 'C'.
   xfc-inttype 'C'.
   xfc-intlen 50 .
   "Decimal
* XFC-decimals = xdetails-decimals.
   The APPEND XFC  the TO IFC .
EndForm .

The FORM get_col .  
The DEFINE FILEDCAT . Gt_fieldcat
-fieldname  = &. 1 .    " The correspondence table of field names gt_fieldcat
-seltext_l  = & 2 .     "Output column text gt_fieldcat
-outputlen  =. 3 & .    "length of         
the APPEND gt_fieldcat .    
the END. OF--the DEFINITION . FILEDCAT 

'TABLE_NAME'     'form name'   20 . FILEDCAT 
'the fIELD'                 'form fields'   20.    FILEDCAT 
'VALUE'              '查询内容'  20.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form ALV_SHOW
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM ALV_SHOW .        

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'         
EXPORTING             I_CALLBACK_PROGRAM 
= SY-REPID             IS_LAYOUT                   
= GS_LAYOUT            IT_FIELDCAT                  
= GT_FIELDCAT[]             I_SAVE                         
'A'           
TABLES           T_OUTTAB                     
= <dyn_table>           
EXCEPTIONS             PROGRAM_ERROR      
1           
OTHERS                       2.
ENDFORM.

 

Guess you like

Origin www.cnblogs.com/Brokenshelltao/p/11670435.html