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

Create table data

Former Member
0 Likes
503

Hi,

i use Create data statement for table but in zhr_lt i out structure that i build in se11 is there onother why exept build strcture in se 11?

Regards

CREATE DATA dytable TYPE TABLE OF zhr_lt

ASSIGN dytable->* TO <d>.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
444

Try this code..it creates int. table dynamically based on the database table name given in the selection screen.

REPORT z_demo_alv_jg.

***********************************************************************

  • TYPE-POOLS *

***********************************************************************

TYPE-POOLS: slis.

***********************************************************************

  • INTERNAL TABLES/WORK AREAS/VARIABLES *

***********************************************************************

DATA: i_fieldcat TYPE slis_t_fieldcat_alv,

i_index TYPE STANDARD TABLE OF i WITH HEADER LINE,

w_field TYPE slis_fieldcat_alv,

p_table LIKE dd02l-tabname,

dy_table TYPE REF TO data,

dy_tab TYPE REF TO data,

dy_line TYPE REF TO data.

***********************************************************************

  • FIELD-SYMBOLS *

***********************************************************************

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,

<dyn_wa> TYPE ANY,

<dyn_field> TYPE ANY,

<dyn_tab_temp> TYPE STANDARD TABLE.

***********************************************************************

  • SELECTION SCREEN *

***********************************************************************

PARAMETERS: tabname(30) TYPE c,

lines(5) TYPE n.

***********************************************************************

  • START-OF-SELECTION *

***********************************************************************

START-OF-SELECTION.

  • Storing table name

p_table = tabname.

  • Create internal table dynamically with the stucture of table name

  • entered in the selection screen

CREATE DATA dy_table TYPE STANDARD TABLE OF (p_table).

ASSIGN dy_table->* TO <dyn_table>.

IF sy-subrc <> 0.

MESSAGE i000(z_zzz_ca_messages) WITH ' No table found'.

LEAVE TO LIST-PROCESSING.

ENDIF.

  • Create workarea for the table

CREATE DATA dy_line LIKE LINE OF <dyn_table>.

ASSIGN dy_line->* TO <dyn_wa>.

  • Select data from table

SELECT * FROM (p_table)

INTO TABLE <dyn_table>

UP TO lines ROWS.

REFRESH <dyn_tab_temp>.

  • Display report

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

i_callback_program = sy-repid

i_structure_name = p_table

i_callback_user_command = 'USER_COMMAND'

i_callback_pf_status_set = 'SET_PF_STATUS'

TABLES

t_outtab = <dyn_table>

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

Regards,

Joy.

3 REPLIES 3
Read only

Former Member
0 Likes
445

Try this code..it creates int. table dynamically based on the database table name given in the selection screen.

REPORT z_demo_alv_jg.

***********************************************************************

  • TYPE-POOLS *

***********************************************************************

TYPE-POOLS: slis.

***********************************************************************

  • INTERNAL TABLES/WORK AREAS/VARIABLES *

***********************************************************************

DATA: i_fieldcat TYPE slis_t_fieldcat_alv,

i_index TYPE STANDARD TABLE OF i WITH HEADER LINE,

w_field TYPE slis_fieldcat_alv,

p_table LIKE dd02l-tabname,

dy_table TYPE REF TO data,

dy_tab TYPE REF TO data,

dy_line TYPE REF TO data.

***********************************************************************

  • FIELD-SYMBOLS *

***********************************************************************

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,

<dyn_wa> TYPE ANY,

<dyn_field> TYPE ANY,

<dyn_tab_temp> TYPE STANDARD TABLE.

***********************************************************************

  • SELECTION SCREEN *

***********************************************************************

PARAMETERS: tabname(30) TYPE c,

lines(5) TYPE n.

***********************************************************************

  • START-OF-SELECTION *

***********************************************************************

START-OF-SELECTION.

  • Storing table name

p_table = tabname.

  • Create internal table dynamically with the stucture of table name

  • entered in the selection screen

CREATE DATA dy_table TYPE STANDARD TABLE OF (p_table).

ASSIGN dy_table->* TO <dyn_table>.

IF sy-subrc <> 0.

MESSAGE i000(z_zzz_ca_messages) WITH ' No table found'.

LEAVE TO LIST-PROCESSING.

ENDIF.

  • Create workarea for the table

CREATE DATA dy_line LIKE LINE OF <dyn_table>.

ASSIGN dy_line->* TO <dyn_wa>.

  • Select data from table

SELECT * FROM (p_table)

INTO TABLE <dyn_table>

UP TO lines ROWS.

REFRESH <dyn_tab_temp>.

  • Display report

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

i_callback_program = sy-repid

i_structure_name = p_table

i_callback_user_command = 'USER_COMMAND'

i_callback_pf_status_set = 'SET_PF_STATUS'

TABLES

t_outtab = <dyn_table>

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

Regards,

Joy.

Read only

0 Likes
444

Hi Joyjit Ghosh,

Thanks but i cant declare str in my program like

types: begin of itab

....

and use it like :

CREATE DATA dya_table TYPE TABLE OF itab

ASSIGN dya_table->* TO <at_a>.

Reagrds

Read only

0 Likes
444

Try this code..this is another variant for creating dynamic table

REPORT zcdf_dynamic_table.

DATA:

r_dyn_table TYPE REF TO data,

r_wa_dyn_table TYPE REF TO data,

r_dock_ctnr TYPE REF TO cl_gui_docking_container,

r_alv_grid TYPE REF TO cl_gui_alv_grid,

t_fieldcat1 TYPE lvc_t_fcat, "with cell color

t_fieldcat2 TYPE lvc_t_fcat, "without cell color

wa_fieldcat LIKE LINE OF t_fieldcat1,

wa_cellcolors TYPE LINE OF lvc_t_scol,

wa_is_layout TYPE lvc_s_layo.

FIELD-SYMBOLS:

<t_dyn_table> TYPE STANDARD TABLE,

<wa_dyn_table> TYPE ANY,

<t_cellcolors> TYPE lvc_t_scol,

<w_field> TYPE ANY.

START-OF-SELECTION.

  • Build field catalog based on your criteria.

wa_fieldcat-fieldname = 'FIELD1'.

wa_fieldcat-inttype = 'C'.

wa_fieldcat-outputlen = '10'.

wa_fieldcat-coltext = 'My Field 1'.

wa_fieldcat-seltext = wa_fieldcat-coltext.

APPEND wa_fieldcat TO t_fieldcat1.

wa_fieldcat-fieldname = 'FIELD2'.

wa_fieldcat-inttype = 'C'.

wa_fieldcat-outputlen = '10'.

wa_fieldcat-coltext = 'My Field 2'.

wa_fieldcat-seltext = wa_fieldcat-coltext.

APPEND wa_fieldcat TO t_fieldcat1.

  • Before adding cell color table, save fieldcatalog to pass

  • to ALV call. The ALV call needs a fieldcatalog without

  • the internal table for cell coloring.

t_fieldcat2[] = t_fieldcat1[].

  • Add cell color table.

  • CALENDAR_TYPE is a structure in the dictionary with a

  • field called COLTAB of type LVC_T_SCOL. You can use

  • any structure and field that has the type LVC_T_SCOL.

wa_fieldcat-fieldname = 'T_CELLCOLORS'.

wa_fieldcat-ref_field = 'COLTAB'.

wa_fieldcat-ref_table = 'CALENDAR_TYPE'.

APPEND wa_fieldcat TO t_fieldcat1.

  • Create dynamic table including the internal table

  • for cell coloring.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = t_fieldcat1

IMPORTING

ep_table = r_dyn_table

EXCEPTIONS

generate_subpool_dir_full = 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.

  • Get access to new table using field symbol.

ASSIGN r_dyn_table->* TO <t_dyn_table>.

  • Create work area for new table.

CREATE DATA r_wa_dyn_table LIKE LINE OF <t_dyn_table>.

  • Get access to new work area using field symbol.

ASSIGN r_wa_dyn_table->* TO <wa_dyn_table>.

  • Get data into table from somewhere. Field names are

  • known at this point because field catalog is already

  • built. Read field names from the field catalog or use

  • COMPONENT <number> in a DO loop to access the fields. A

  • simpler hard coded approach is used here.

ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'ABC'.

ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'XYZ'.

APPEND <wa_dyn_table> TO <t_dyn_table>.

ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'TUV'.

ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'DEF'.

APPEND <wa_dyn_table> TO <t_dyn_table>.

  • Color cells based on your criteria. In this example, a test on

  • FIELD2 is used to decide on color.

LOOP AT <t_dyn_table> INTO <wa_dyn_table>.

ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.

  • Get access to internal table used to color cells.

ASSIGN COMPONENT 'T_CELLCOLORS'

OF STRUCTURE <wa_dyn_table> TO <t_cellcolors>.

CLEAR wa_cellcolors.

wa_cellcolors-fname = 'FIELD2'.

IF <w_field> = 'DEF'.

wa_cellcolors-color-col = '7'.

ELSE.

wa_cellcolors-color-col = '5'.

ENDIF.

APPEND wa_cellcolors TO <t_cellcolors>.

MODIFY <t_dyn_table> FROM <wa_dyn_table>.

ENDLOOP.

  • Display screen. Define screen 100 as empty, with next screen

  • set to 0 and flow logic of:

*

  • PROCESS BEFORE OUTPUT.

  • MODULE initialization.

*

  • PROCESS AFTER INPUT.

CALL SCREEN 100.

----


  • MODULE initialization OUTPUT

----


MODULE initialization OUTPUT.

  • Set up for ALV display.

IF r_dock_ctnr IS INITIAL.

CREATE OBJECT r_dock_ctnr

EXPORTING

side = cl_gui_docking_container=>dock_at_left

ratio = '90'.

CREATE OBJECT r_alv_grid

EXPORTING i_parent = r_dock_ctnr.

  • Set ALV controls for cell coloring table.

wa_is_layout-ctab_fname = 'T_CELLCOLORS'.

  • Display.

CALL METHOD r_alv_grid->set_table_for_first_display

EXPORTING

is_layout = wa_is_layout

CHANGING

it_outtab = <t_dyn_table>

it_fieldcatalog = t_fieldcat2.

ELSE. "grid already prepared

  • Refresh display.

CALL METHOD r_alv_grid->refresh_table_display

EXPORTING

i_soft_refresh = ' '

EXCEPTIONS

finished = 1

OTHERS = 2.

ENDIF.

ENDMODULE. " initialization OUTPUT

Steps:

1. Define ur type statement:

ex:

types : begin of ty_xxx,

.....

end of ty_xxx.

2. Define ur structure

data: w_xxx type ty_xxx.

3. Define field catalog for this stucture

  • Build field catalog based on your criteria.

wa_fieldcat-fieldname = 'FIELD1'.

wa_fieldcat-inttype = 'C'.

wa_fieldcat-outputlen = '10'.

wa_fieldcat-coltext = 'My Field 1'.

wa_fieldcat-seltext = wa_fieldcat-coltext.

APPEND wa_fieldcat TO t_fieldcat1.

wa_fieldcat-fieldname = 'FIELD2'.

wa_fieldcat-inttype = 'C'.

wa_fieldcat-outputlen = '10'.

wa_fieldcat-coltext = 'My Field 2'.

wa_fieldcat-seltext = wa_fieldcat-coltext.

APPEND wa_fieldcat TO t_fieldcat1.

4. Create the int. table

  • Create dynamic table including the internal table

  • for cell coloring.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = t_fieldcat1

IMPORTING

ep_table = r_dyn_table

EXCEPTIONS

generate_subpool_dir_full = 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.

  • Get access to new table using field symbol.

ASSIGN r_dyn_table->* TO <t_dyn_table>.

  • Create work area for new table.

CREATE DATA r_wa_dyn_table LIKE LINE OF <t_dyn_table>.

  • Get access to new work area using field symbol.

ASSIGN r_wa_dyn_table->* TO <wa_dyn_table>.

Regards,

Joy.