https://www.cnblogs.com/jiangzhengjun/p/4264644.html 看了阁下这篇博文,亲自试了一下,创建动态内表的时候,反射出来的动态内表字段长度翻倍了。代码36 37行经打断点发现出了问题。麻烦看到时给个回复,谢谢。
1 *&---------------------------------------------------------------------* 2 *& Report ZCHENH022 3 *& 4 *&---------------------------------------------------------------------* 5 *& 6 *& 7 *&---------------------------------------------------------------------* 8 9 REPORT zchenh022. 10 TYPE-POOLS: abap. 11 FIELD-SYMBOLS:<dyn_table> TYPE STANDARD TABLE, 12 <dyn_wa>, 13 <dyn_field>. 14 DATA: dy_table TYPE REF TO data, 15 dy_line TYPE REF TO data, 16 xfc TYPE lvc_s_fcat, 17 ifc TYPE lvc_t_fcat. 18 SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME. 19 PARAMETERS: p_table(30) TYPE c DEFAULT 'T001'. 20 SELECTION-SCREEN END OF BLOCK b1. 21 22 START-OF-SELECTION. 23 PERFORM get_structure. 24 PERFORM create_dynamic_itab. 25 PERFORM get_data. 26 PERFORM write_out. 27 *&--------------------------------------------------------------------* 28 *& Form get_structure 29 *&--------------------------------------------------------------------* 30 * text 31 *---------------------------------------------------------------------* 32 FORM get_structure. 33 DATA: idetails TYPE abap_compdescr_tab, 34 xdetails TYPE abap_compdescr. 35 DATA: ref_table_des TYPE REF TO cl_abap_structdescr. 36 ref_table_des ?= cl_abap_typedescr=>describe_by_name( p_table ). 37 38 idetails[] = ref_table_des->components[]. 39 LOOP AT idetails INTO xdetails. 40 CLEAR xfc. 41 xfc-fieldname = xdetails-name. 42 xfc-datatype = xdetails-type_kind. 43 xfc-inttype = xdetails-type_kind. 44 xfc-intlen = xdetails-length. 45 xfc-decimals = xdetails-decimals. 46 APPEND xfc TO ifc. 47 ENDLOOP. 48 ENDFORM. "get_structure 49 *&--------------------------------------------------------------------* 50 *& Form create_dynamic_itab 51 *&--------------------------------------------------------------------* 52 * text 53 *---------------------------------------------------------------------* 54 FORM create_dynamic_itab. 55 CALL METHOD cl_alv_table_create=>create_dynamic_table 56 EXPORTING 57 it_fieldcatalog = ifc 58 IMPORTING 59 ep_table = dy_table. 60 ASSIGN dy_table->* TO <dyn_table>. 61 CREATE DATA dy_line LIKE LINE OF <dyn_table>. 62 ASSIGN dy_line->* TO <dyn_wa>. 63 ENDFORM. "create_dynamic_itab 64 *&--------------------------------------------------------------------* 65 *& Form get_data 66 *&--------------------------------------------------------------------* 67 * text 68 *---------------------------------------------------------------------* 69 FORM get_data. 70 SELECT * INTO TABLE <dyn_table> FROM (p_table). 71 ENDFORM. "get_data 72 *&--------------------------------------------------------------------* 73 *& Form write_out 74 *&--------------------------------------------------------------------* 75 * text 76 *---------------------------------------------------------------------* 77 FORM write_out. 78 LOOP AT <dyn_table> INTO <dyn_wa>. 79 DO. 80 ASSIGN COMPONENT sy-index OF STRUCTURE <dyn_wa> TO <dyn_field>. 81 IF sy-subrc <> 0. 82 EXIT. 83 ENDIF. 84 IF sy-index = 1. 85 WRITE:/ <dyn_field>. 86 ELSE. 87 WRITE: <dyn_field>. 88 ENDIF. 89 ENDDO. 90 ENDLOOP. 91 ENDFORM. "write_out
字段长度翻倍是啥意思?
ref_table_des->components[].这个修改成这个试试,我没有这样直接取它的成员变量,我一般是这样用的:
ref_table_des->get_components( ).
字段长度翻倍的意思是:代码的第44行: xfc-intlen = xdetails-length. 这个反射出来的长度与数据库底表中的长度不同,阁下可以试试。