F1 相似查找字段存在的table
阅读原文时间:2023年07月15日阅读:1

REPORT ZME12_BDC1.
*REPORT demo_select_dynamic_conditions .
TABLES: makt,dd02t,marc.
TYPE-POOLS : ABAP.
DATA where_tab() OCCURS WITH HEADER LINE.
DATA where_tab2() OCCURS WITH HEADER LINE.
DATA : wa_itab LIKE makt OCCURS WITH HEADER LINE.
DATA : idetails TYPE abap_compdescr_tab,
xdetails TYPE abap_compdescr.
DATA:wa LIKE idetails WITH HEADER LINE.

DATA:BEGIN OF get_table occurs ,
tabname TYPE string,"表名
fieldname LIKE dd03l-fieldname,"字段名称
rollname LIKE dd03l-rollname,"资料元素
inttype LIKE dd03l-inttype, "类型
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.
*--->去掉前导
* CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
* EXPORTING
* input = s_trx
* IMPORTING
* output = s_trx.
*IF cl_abap_matcher=>matches(
* pattern = '^(-?[1-9]\d*(\.\d*[1-9])?)|(-?0\.\d*[1-9])$'
* text = s_trx ) = abap_true.
*
** WRITE '数字'.
* CONCATENATE p_field ' EQ ''' s_trx '''' INTO where_tab.
*ELSE.
*
** WRITE '非数字'.
* CONCATENATE p_field ' LIKE ''%' s_trx '%''' INTO where_tab.
*ENDIF.
* CONCATENATE p_field ' LIKE ''%' s_trx '%''' INTO where_tab.
* APPEND where_tab.

* CONCATENATE p_field ' NE ''''' INTO where_tab2.
* APPEND where_tab2.
* CLEAR where_tab2.

TYPE-POOLS : ABAP.
FIELD-SYMBOLS: TYPE STANDARD TABLE,
,
.
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 i,
p_len TYPE n.

   gs\_layout-colwidth\_optimize = 'X'.  
   p\_len = strlen( s\_trx ).  
    "查询ABAP 所有table表格  
    SELECT dd02vv~TABNAME  
                dd03l~fieldname  
                dd03l~rollname  
                dd03l~inttype  
      COUNT(\*) FROM dd02vv  
      INNER JOIN dd02l ON  dd02l~tabname EQ dd02vv~tabname  
      INNER JOIN dd03l ON dd03l~tabname EQ dd02vv~tabname  

* UP TO ROWS
INTO CORRESPONDING FIELDS OF TABLE get_table
WHERE dd02vv~tabclass IN ('TRANSP','CLUSTER','POOL') AND " 透明表
dd02vv~contflag NE 'L' AND " 存储时资料的表格,空传送
dd02vv~as4local NOT IN ( 'N','T' ) AND " 未启用,临时版本
( dd03l~fieldname EQ p_field OR
dd03l~rollname EQ p_field ) AND
* dd02vv~tabname NOT IN ( 'DF50D' ,'DF40D' ) AND
dd03l~LENG >= p_len AND
is_gtt NE 'X' " X全局临时表 N 没有全局临时表
GROUP BY dd02vv~tabname dd03l~fieldname
dd03l~rollname dd03l~inttype.

       "字符小写转换大写  
       TRANSLATE p\_field to UPPER CASE.  
        "创建内表结构  
       PERFORM new\_teble.  
       " 创建内表  
       PERFORM create\_dynamic\_itab.

       LOOP AT get\_table.  
         l\_index = sy-tabix.  
         CLEAR: l\_num.  
        "查询表格内容是否有值  
         SELECT COUNT(\*) INTO l\_num  
             UP TO  ROWS  
             FROM (get\_table-tabname).  
            IF sy-subrc <> .  
              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 = .
"--->1.abap 字典类型为 p x i f 8 字段调用动态 sql会down
"--->2.为知 b s g y u v h V r l a e j k z 类型 调用动态 sql是否会down
CLEAR:l_value.
IF get_table-inttype EQ 'P' OR
get_table-inttype EQ 'X' OR
get_table-inttype EQ 'I' OR
get_table-inttype EQ 'F' OR
get_table-inttype EQ ''.

* SELECT (p_field)
* FROM (get_table-tabname)
* INTO @l_value.

                 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> = get\_table-fieldname.

                 ASSIGN COMPONENT 'VALUE' OF  
                             STRUCTURE <dyn\_wa> TO <dyn\_field>.

                 <dyn\_field> = l\_value .

                 ASSIGN COMPONENT 'ACCURACY' OF  
                             STRUCTURE <dyn\_wa> TO <dyn\_field>.  
                 IF l\_value EQ s\_trx.  
                   <dyn\_field> = .  
                 ELSE.  
                   <dyn\_field> = .  
                 ENDIF.

                 APPEND <dyn\_wa> TO <dyn\_table>. CLEAR <dyn\_wa>.  
         ELSE.  
              CLEAR:where\_tab\[\].  
             CONCATENATE get\_table-fieldname ' LIKE ''%' s\_trx '%'''  
                                     INTO where\_tab.  
             APPEND where\_tab.

            SELECT SINGLE (get\_table-fieldname)  
               FROM (get\_table-tabname)  
               INTO @l\_value  
               WHERE (where\_tab).  
             IF sy-subrc = .  
                 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> = get\_table-fieldname.

                 ASSIGN COMPONENT 'VALUE' OF  

STRUCTURE TO .

= l_value .

ASSIGN COMPONENT 'ACCURACY' OF
STRUCTURE TO .
IF l_value EQ s_trx.
= .
ELSE.
= .
ENDIF.

APPEND TO . CLEAR .
ENDIF.
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 .
* create dynamic work area and assign to fs
CREATE DATA dy_line LIKE LINE OF .
ASSIGN dy_line->* TO .
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 = .
"小数点
* 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 = .
"小数点
* 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 = .
"小数点
* xfc-decimals = xdetails-decimals.
APPEND xfc TO ifc.

CLEAR xfc.
* xfc-tabname = 'TABLE_FIELD'. xfc
-fieldname = 'ACCURACY'. xfc
-datatype = 'P'. xfc
-inttype = 'P'. xfc
-intlen = .
"小数点 xfc
-decimals = .
APPEND xfc TO ifc.
ENDFORM.

FORM get_col.
DEFINE FILEDCAT. gt_fieldcat
-fieldname = &. "对应内表的字段名 gt_fieldcat
-seltext_l = &. "输出列文本 gt_fieldcat
-outputlen = &. "長度
APPEND gt_fieldcat.
END-OF-DEFINITION. FILEDCAT

'TABLE_NAME' '表格名称' . FILEDCAT
'FIELD' '表格字段' . FILEDCAT
'VALUE' '值' . FILEDCAT
'ACCURACY' '值的精确度' .
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
=
EXCEPTIONS PROGRAM_ERROR
=
OTHERS = .
ENDFORM.

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章