有感于权限太小(没有se11,12,16),准备研究一下动态内表,准备结合动态内表和动态查询写一个function,达到类试se16的功能。
data tabname type tabname .
data where_clause type string .
data: d_ref type ref to data.
field-symbols: type table.
data: dr_line type ref to data.
field-symbols: type any.
data: i_alv_cat type table of lvc_s_fcat,
ls_alv_cat like line of i_alv_cat.
call function 'LVC_FIELDCATALOG_MERGE'
exporting
i_structure_name = tabname
changing
ct_fieldcat = i_alv_cat.
* internal table build
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = i_alv_cat
importing
ep_table = d_ref.
assign d_ref->* to .
* Create dynamic work area and assign to FS
create data dr_line like line of .
assign dr_line->* to .
* get data
select * into table
from (tabname)
where (where_clause) .
初步解决一下,还有很多问题,关键是怎么把转换成普通内表。
定义动态结构 方案2:
DATA: lt_ztp009 TYPE TABLE OF ztp009,
ls_ztp009 TYPE ztp009,
dyn_wa TYPE REF TO data.
FIELD-SYMBOLS: TYPE any,
TYPE any.
CREATE DATA dyn_wa TYPE (iv_attribute_structure_name). " 定义动态结构
ASSIGN dyn_wa->* TO .
ASSIGN COMPONENT ls_ztp009-fieldname OF STRUCTURE TO .
CLEAR .
详细CODE:
SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S31 {
font-style: italic;
color: #808080;
}
.L0S32 {
color: #3399FF;
}
.L0S33 {
color: #4DA619;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
} FUNCTION zfm_code_add.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(IV_TABNAME) TYPE TABNAME
*"----------------------------------------------------------------------
************定义变量
DATA: lt_fieldcat TYPE lvc_t_fcat,
ls_fieldcat TYPE lvc_s_fcat,
d_ref TYPE REF TO data,
dr_line TYPE REF TO data.
FIELD-SYMBOLS: TYPE ANY TABLE,
TYPE any,
TYPE any.
****************************************************
CHECK iv_tabname IS NOT INITIAL .
*********获得表结构
REFRESH lt_fieldcat .
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = iv_tabname
CHANGING
ct_fieldcat = lt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
CHECK lt_fieldcat[] IS NOT INITIAL .
*********创建动态内表
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_fieldcat
IMPORTING
ep_table = d_ref.
ASSIGN d_ref->* TO .
*********创建动态内表工作区
CREATE DATA dr_line LIKE LINE OF .
ASSIGN dr_line->* TO .
*********动态内表取数
SELECT * INTO TABLE
FROM (iv_tabname) .
* where (where_clause) .
******** 动态内表 值处理
LOOP AT ASSIGNING .
CLEAR ls_fieldcat .
LOOP AT lt_fieldcat INTO ls_fieldcat.
ASSIGN COMPONENT ls_fieldcat-fieldname OF STRUCTURE TO .
* = '' .
CLEAR ls_fieldcat .
ENDLOOP.
ENDLOOP.
ENDFUNCTION.
http://www.cnblogs.com/VerySky/articles/2514436.html
SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S31 {
font-style: italic;
color: #808080;
}
.L0S32 {
color: #3399FF;
}
.L0S33 {
color: #4DA619;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}
FUNCTION zfm_code_add1.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(P_NAME) TYPE TABNAME DEFAULT 'BUT000'
*"----------------------------------------------------------------------
DATA: lt_table TYPE TABLE OF dfies, "字段结构表
ls_table TYPE dfies,
lr_struc TYPE REF TO cl_abap_structdescr,
lr_table TYPE REF TO cl_abap_tabledescr,
lr_type TYPE REF TO cl_abap_typedescr,
lr_data TYPE REF TO cl_abap_datadescr,
lt_comp TYPE abap_component_tab,
ls_comp LIKE LINE OF lt_comp,
dyn_wa TYPE REF TO data,
dyn_table TYPE REF TO data,
l_string TYPE string.
FIELD-SYMBOLS: TYPE STANDARD TABLE,
TYPE any.
*1.获取表中的字段结构
CALL FUNCTION 'DDIF_NAMETAB_GET'
EXPORTING
tabname = p_name
TABLES
dfies_tab = lt_table
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
LOOP AT lt_table INTO ls_table.
CONCATENATE ls_table-tabname '-' ls_table-fieldname INTO l_string.
ls_comp-name = ls_table-fieldname.
*读取字段类型
CALL METHOD cl_abap_datadescr=>describe_by_name
EXPORTING
p_name = l_string
RECEIVING
p_descr_ref = lr_type
EXCEPTIONS
type_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ls_comp-type ?= lr_type.
APPEND ls_comp TO lt_comp.
CLEAR ls_comp.
ENDLOOP.
*根据字段目录创建动态结构类型
CALL METHOD cl_abap_structdescr=>create
EXPORTING
p_components = lt_comp
RECEIVING
p_result = lr_struc.
*根据动态结构创建动态内表类型
CALL METHOD cl_abap_tabledescr=>create
EXPORTING
p_line_type = lr_struc
RECEIVING
p_result = lr_table.
*参照动态结构类型和动态内表类型创建内表与工作区
CREATE DATA dyn_wa TYPE HANDLE lr_struc.
CREATE DATA dyn_table TYPE HANDLE lr_table.
*指定内表与工作区到字段符号
ASSIGN dyn_wa->* TO .
ASSIGN dyn_table->* TO .
ENDFUNCTION.
阅读(1813) | 评论(8) | 转发(0) |