漠丶涯

动态取数据库表的方法

1.动态取数据库表展示ALV

REPORT ZBC_VARTAB0010.
*&---------------------------------------------------------------------*
*& Report  ZBC_VARTAB0010.
*&
*&---------------------------------------------------------------------*
*&
*&  ABAP 动态的实现任意表的ALV显示(学习动态构建表格和结构)
*&
*&---------------------------------------------------------------------*

type-pools:abap.
data lt_table type table of dfies.
data ls_table type dfies.

parameters p_name type objname default 'EKKO'.
parameters p_max  type i default 500.

initialization.

start-of-selection.

  call function 'DDIF_NAMETAB_GET'
  exporting
  tabname = p_name
  tables
* X031L_TAB =
  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.

  data lr_struc type ref to cl_abap_structdescr.
  data lr_table type ref to cl_abap_tabledescr.
  data lr_type type ref to cl_abap_typedescr.
  data lr_data type ref to cl_abap_datadescr.
  data l_string type string.
  data lt_comp type abap_component_tab.
  data ls_comp like line of lt_comp.
  data e_wa type ref to data.
  data e_table type ref to data.


  field-symbols <fs_table> type standard table.
  field-symbols <fs_struc> type any.

  loop at lt_table into ls_table.
    concatenate ls_table-tabname '-' ls_table-fieldname into l_string.
* L_STRING = LS_COMP-NAME.
    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 e_wa type handle lr_struc.
  create data e_table type handle lr_table.


  assign e_wa->* to <fs_struc>.
  assign e_table->* to <fs_table>.



  select *
    up to p_max rows
    into corresponding fields of table <fs_table>
    from (p_name).

  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_structure_name = p_name
    tables
      t_outtab         = <fs_table>
    exceptions
      program_error    = 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.

2.有个FUNCTION 可以CALL的



REPORT ZBC_VARTAB0020.
type-pools:abap.
data lt_table type table of dfies.
data ls_table type dfies.
DATA SELTAB LIKE RSPARAMS.
parameters p_name type objname default 'MAKT'.
INITIALIZATION.
START-OF-SELECTION.
CALL FUNCTION 'RS_TABLE_LIST_CREATE'
  EXPORTING
    TABLE_NAME               = p_name
*   ACTION                   = 'ANZE'
*   WITHOUT_SUBMIT           = ' '
*   GENERATION_FORCED        =
*   NEW_SEL                  =
*   NO_STRUCTURE_CHECK       = ' '
*   DATA_EXIT                = 'X'
* IMPORTING
*   PROGNAME                 =
* TABLES
*   SELTAB                   =
* EXCEPTIONS
*   TABLE_IS_STRUCTURE       = 1
*   TABLE_NOT_EXISTS         = 2
*   DB_NOT_EXISTS            = 3
*   NO_PERMISSION            = 4
*   NO_CHANGE_ALLOWED        = 5
*   OTHERS                   = 6
          .
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.


评论