Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

can i create dynamic table according database table ??

Former Member
0 Likes
2,340

hi,

could i use database table for cl_alv_table_create=>create_dynamic_table to create table ? i mean, import a database table name to the method and create a internal table. now i only know parameters use fieldcatalog like this.

EXPORTING

it_fieldcatalog = it_fieldcat

more, i doule click the method cl_alv_table_create=>create_dynamic_table but just got a code part, how can i find the parameters definition of this method ? and why it's different from other method which i can browse the parameters by double clicking the method name in code.

thanks!!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,368

u can create the dynamic internal table without using class too..and if u want to create field catalog also then create it using field catalog merge function.

Program to create dynamic internal table -


*&---------------------------------------------------------------------*
*& Report  ZGILL_FS                                                    *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  ZGILL_FS     line-size 250
                     line-count 65 .


*************Creating Dynamic internal table*************

parameter p_table type tabname.

field-symbols <tab> type table.
field-symbols <tab1> type any.


types: begin of itab,
      t_name type tabname,
      t_ref type ref to data,
      end of itab.

data itab1 type table of itab with non-unique key t_name.

perform fetch_data using p_table.
perform print_table using p_table.
*&---------------------------------------------------------------------*
*&      Form  fetch_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_TABLE  text
*----------------------------------------------------------------------*
FORM fetch_data  USING P_TABLE1 type tabname.

data itab2 type itab.
itab2-t_name = p_table1.

create data itab2-t_ref type table of (itab2-t_name) .

assign itab2-t_ref->* to <tab>.

append itab2 to itab1.

select * from (p_table1) up to 25 rows into corresponding fields of table <tab>.

ENDFORM.                    " fetch_data
*&---------------------------------------------------------------------*
*&      Form  print_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_TABLE  text
*----------------------------------------------------------------------*
FORM print_table  USING P_TABLE1 type tabname.

DATA t_ref1 TYPE REF TO data.
DATA itab2 TYPE itab.

FIELD-SYMBOLS <field> TYPE ANY.

READ TABLE itab1 INTO itab2 WITH KEY t_name = p_table1.

ASSIGN itab2-t_ref->* TO <tab>.

CREATE DATA t_ref1 LIKE LINE OF <tab>.

ASSIGN t_ref1->* TO <tab1>.

DO.
*READ TABLE <tab> ASSIGNING <tab1> INDEX 1.
READ TABLE <tab> ASSIGNING <tab1> INDEX SY-INDEX.
*WRITE:/ p_table1.
NEW-LINE.
IF sy-subrc <> 0.
EXIT.
ENDIF.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
WRITE: <field>,'    '.
ENDDO.
ENDDO.
ENDFORM.                    " print_table

9 REPLIES 9
Read only

Former Member
0 Likes
1,368

one long way to build field catalog using REUSE_ALV_FIELDCATALOG_MERGE by passing the structure and build the fieldcatalog, not sure if there is any easy way...

goto se24 and give the class name

and search for that method,

put the cursor on method name and click on parameters button

Read only

0 Likes
1,368

Dear Jagarlamudi ,

thank you very much !

i use your method, but now i has another problem.

the data type in cl_alv_table_create=>create_dynamic_table is lvc_t_fcat but the type in REUSE_ALV_FIELDCATALOG_MERGE is slis_t_fieldcat_alv, how to convert from slis_t_fieldcat_alv to lvc_t_fcat ?

.

Read only

0 Likes
1,368

use LVC_FIELDCATALOG_MERGE fn module it will take that lvc_t_fcat.

other wise you have to

loop at ifieldcat into wfieldcat.

wlvc_fieldcat = wfieldcat-<fieldname >.

for each and every field <because in some cases you cant use move-corresponding>

and append thewlvc_fcatto ilvc_t_fcat.

endloop.

regards

shiba dutta

Read only

Former Member
0 Likes
1,368

go to se24 and give the class name cl_alv_table_create check the methods parameter there. or you can call the method using pattern(ctrl+F6) also choose abap object radio button and give class name cl_alv_table_create and method name create_dynamic_table and it will give you all the parameters.

i believe you have to create field catalogby using reuse_alv_fieldcatalog_merge) first and then only you can create the dynamic int tbale for that.

regards

shiba dutta

Read only

Former Member
0 Likes
1,368

hi

good

  • Create a new Table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat

IMPORTING

ep_table = new_table.

go through this link

http://www.sap-basis-abap.com/sapab037.htm

thanks

mrutyun^

Read only

Former Member
0 Likes
1,368

Hi,

Place ur cursor on class name not on method, if u double click there, it will take to that class with list of methods. As usual place ur cursor on CREATE_DYNAMIC_TABLE, then go for parameters tab which will list out all the parameters of that interface.

U can use the FM : LVC_FIELDCATALOG_MERGE to create a field catalogue with the structure of ur DB table.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

  • I_BUFFER_ACTIVE =

I_STRUCTURE_NAME = 'ZOCM' <b>"UR DB table</b>

-SatyaPriya

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,368

Hello Zhang

If you are already on SAP release 6.40 (or higher) you can use the <b>TYPE HANDLE </b>statement to create any internal table you like.

The simplified version of de-referencing a data reference to a table type field symbol is already available since 6.20 (perhaps on 4.6c, too).


*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_RTTI_CREATE_STRUCTURES
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_rtti_create_structures.


TYPE-POOLS: abap.


DATA:
  celltab          TYPE lvc_t_styl.

DATA:
  go_table         TYPE REF TO cl_salv_table,
  go_sdescr        TYPE REF TO cl_abap_structdescr,
  go_tdescr        TYPE REF TO cl_abap_tabledescr,
  gdo_data         TYPE REF TO data,
  gdo_handle       TYPE REF TO data,
  gs_comp          TYPE abap_componentdescr,
  gt_components    TYPE abap_component_tab.
*
*    name       TYPE string,
*    type       TYPE REF TO cl_abap_datadescr,
*    as_include TYPE abap_bool,
*    suffix     TYPE string,

FIELD-SYMBOLS:
  <gd_fld>      TYPE ANY,
  <gs_struc>    TYPE ANY,
  <gt_itab>     TYPE table.


PARAMETER:
  p_tabnam      TYPE tabname  DEFAULT 'KNB1'.

START-OF-SELECTION.

* Create dynamically structure
  CREATE DATA gdo_data TYPE (p_tabnam).
  ASSIGN gdo_data->* TO <gs_struc>.
  CHECK ( <gs_struc> IS ASSIGNED ).


* Simulate dynamic addition of columns to ALV list
  DO 10 TIMES.
    ASSIGN COMPONENT syst-index OF STRUCTURE <gs_struc> TO <gd_fld>.

    CLEAR: gs_comp.
    gs_comp-type ?= cl_abap_datadescr=>describe_by_data( <gd_fld> ).
    gs_comp-name  = gs_comp-type->get_relative_name( ).
    APPEND gs_comp TO gt_components.

    go_sdescr  = cl_abap_structdescr=>create( gt_components ).
    go_tdescr  = cl_abap_tabledescr=>create( go_sdescr ).
    "   Create data refence followed by table creation
    CREATE DATA gdo_handle TYPE HANDLE go_tdescr.
    ASSIGN gdo_handle->* TO <gt_itab>.

*   Dynamic select
    SELECT        * FROM  (p_tabnam)
      INTO CORRESPONDING FIELDS OF TABLE <gt_itab>
           WHERE  bukrs  = '2000'.

    TRY.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table = go_table
          CHANGING
            t_table      = <gt_itab>.
        go_table->display( ).
      CATCH cx_salv_msg .
    ENDTRY.

  ENDDO.


  " Add table type as field to structure
  CLEAR: gs_comp.
  gs_comp-type ?= cl_abap_typedescr=>describe_by_data( celltab ).
  gs_comp-name  = 'CELLTAB'.
  APPEND gs_comp TO gt_components.

  go_sdescr  = cl_abap_structdescr=>create( gt_components ).
  go_tdescr  = cl_abap_tabledescr=>create( go_sdescr ).
  CREATE DATA gdo_handle TYPE HANDLE go_tdescr.
  ASSIGN gdo_handle->* TO <gt_itab>.

*   Dynamic select
  SELECT        * FROM  (p_tabnam)
    INTO CORRESPONDING FIELDS OF TABLE <gt_itab>
         WHERE  bukrs  = '2000'.



  " Simplified version of table creation:
  CLEAR: gdo_data.
  UNASSIGN <gt_itab>.

  CREATE DATA gdo_data TYPE (p_tabnam).
  ASSIGN gdo_data->* TO <gt_itab>.


END-OF-SELECTION.

Regards

Uwe

Read only

Former Member
0 Likes
1,369

u can create the dynamic internal table without using class too..and if u want to create field catalog also then create it using field catalog merge function.

Program to create dynamic internal table -


*&---------------------------------------------------------------------*
*& Report  ZGILL_FS                                                    *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  ZGILL_FS     line-size 250
                     line-count 65 .


*************Creating Dynamic internal table*************

parameter p_table type tabname.

field-symbols <tab> type table.
field-symbols <tab1> type any.


types: begin of itab,
      t_name type tabname,
      t_ref type ref to data,
      end of itab.

data itab1 type table of itab with non-unique key t_name.

perform fetch_data using p_table.
perform print_table using p_table.
*&---------------------------------------------------------------------*
*&      Form  fetch_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_TABLE  text
*----------------------------------------------------------------------*
FORM fetch_data  USING P_TABLE1 type tabname.

data itab2 type itab.
itab2-t_name = p_table1.

create data itab2-t_ref type table of (itab2-t_name) .

assign itab2-t_ref->* to <tab>.

append itab2 to itab1.

select * from (p_table1) up to 25 rows into corresponding fields of table <tab>.

ENDFORM.                    " fetch_data
*&---------------------------------------------------------------------*
*&      Form  print_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_TABLE  text
*----------------------------------------------------------------------*
FORM print_table  USING P_TABLE1 type tabname.

DATA t_ref1 TYPE REF TO data.
DATA itab2 TYPE itab.

FIELD-SYMBOLS <field> TYPE ANY.

READ TABLE itab1 INTO itab2 WITH KEY t_name = p_table1.

ASSIGN itab2-t_ref->* TO <tab>.

CREATE DATA t_ref1 LIKE LINE OF <tab>.

ASSIGN t_ref1->* TO <tab1>.

DO.
*READ TABLE <tab> ASSIGNING <tab1> INDEX 1.
READ TABLE <tab> ASSIGNING <tab1> INDEX SY-INDEX.
*WRITE:/ p_table1.
NEW-LINE.
IF sy-subrc <> 0.
EXIT.
ENDIF.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <tab1> TO <field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
WRITE: <field>,'    '.
ENDDO.
ENDDO.
ENDFORM.                    " print_table

Read only

former_member202734
Participant
0 Likes
1,368

Based on previous code example on dynamic ALV :

Get table T002 (or any other) as first structure and add any internal field (FIELD1 TYPE WERKS_D) in the ouput (FIELD1 should be populated also. Not done here) :


REPORT  zus_sdn_rtti_create_structure3.

TYPE-POOLS: abap.

DATA:
  go_table         TYPE REF TO cl_salv_table,
  go_sdescr        TYPE REF TO cl_abap_structdescr,
  go_tdescr        TYPE REF TO cl_abap_tabledescr,
  gdo_handle       TYPE REF TO data,
  gt_components    TYPE abap_component_tab.

FIELD-SYMBOLS:
  <gt_itab>     TYPE table.

PARAMETER:
  p_tabnam      TYPE tabname  DEFAULT 'T002'.

START-OF-SELECTION.
* get the table (structure) definition
  go_sdescr ?= cl_abap_typedescr=>describe_by_name( p_tabnam ).
* add each field (+ description) in component table
  gt_components = go_sdescr->get_components( ).

* / add new field
  DATA field1 TYPE werks_d.
  DATA gs_comp TYPE abap_componentdescr.
  CLEAR gs_comp.
  gs_comp-type ?= cl_abap_typedescr=>describe_by_data( field1 ).
  gs_comp-name = 'FIELD1'.
  APPEND gs_comp TO gt_components.
* \-------------------

* Structure type creation
  go_sdescr  = cl_abap_structdescr=>create( gt_components ).
* Table type creation
  go_tdescr  = cl_abap_tabledescr=>create( go_sdescr ).
* Create data refence followed by table creation
  CREATE DATA gdo_handle TYPE HANDLE go_tdescr.
  ASSIGN gdo_handle->* TO <gt_itab>.

*   Dynamic select
  SELECT        * FROM  (p_tabnam)
    UP TO 10 ROWS
    INTO CORRESPONDING FIELDS OF TABLE <gt_itab>.

  TRY.
      CALL METHOD cl_salv_table=>factory
        IMPORTING
          r_salv_table = go_table
        CHANGING
          t_table      = <gt_itab>.
      go_table->display( ).
    CATCH cx_salv_msg .
  ENDTRY.

Edited by: Vincent Noiset on Aug 28, 2008 2:51 PM