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

Entering data into a dynamically created table.

Former Member
0 Likes
1,348

Hi,

I have to create an alv whose field catalog is created dynamically through a few tables(that also determined at run time).

Now I need to populate this dynamically created table with values from the tables(that are also dynamically determined).

But the issue is that I cannot use into-corresponding

or appending-corresponing.

Below is a sample code...

<i> CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = li_fieldcat

IMPORTING

ep_table = lt_table.

ASSIGN lt_table->* TO <lt_tab1>.

LOOP AT li_report_tab_kum INTO lwa_tmp_tab.

SELECT SINGLE kotab

FROM t681

INTO lv_table

WHERE kvewe = 'E'

and kotabnr = lwa_tmp_tab-kotabnr.

SELECT *

FROM (lv_table)

INTO CORRESPONDING FIELDS OF TABLE <lt_tab1>

WHERE kappl = 'V'

AND kschl = lwa_tmp_tab-kschl

AND knuma = lwa_tmp_tab-knuma.

ENDLOOP.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

i_callback_program = sy-cprog

i_callback_user_command = 'USER_COMMAND'

it_fieldcat = li_fieldcatalog

is_layout = gs_layout

  • it_events = lt_events

  • it_sort = li_sort

TABLES

t_outtab = <lt_tab1>.</i>

Pls let me know a way out of this.

Thanks in advance.

5 REPLIES 5
Read only

naimesh_patel
Active Contributor
0 Likes
847

hello,

You have to use the ASSIgn component.

Regards,

Naimesh

Read only

Former Member
0 Likes
847

you can look to the example program below

DATA: g_t_upload_tab TYPE REF TO data.

DATA: g_t_upload_tab_chk TYPE REF TO data.

DATA: g_t_upload_tab_del TYPE REF TO data.

DATA: g_t_upload_tab_wa TYPE REF TO data.

DATA: g_t_upload_tab_chk_wa TYPE REF TO data.

DATA: g_t_upload_tab_del_wa TYPE REF TO data.

FIELD-SYMBOLS: TYPE STANDARD TABLE.

  • For ALV

  • Data for ALV

DATA: g_f_ok_code LIKE sy-ucomm,

g_f_container TYPE scrfname VALUE 'UPLOAD_TAB',

g_o_grid TYPE REF TO cl_gui_alv_grid,

g_o_custom_container TYPE REF TO cl_gui_custom_container.

DATA: g_t_fieldcat TYPE lvc_t_fcat.

DATA: g_r_fieldcat TYPE lvc_s_fcat.

DATA: g_r_disp_variant TYPE disvariant.

  • For summary

DATA: g_f_repid LIKE sy-repid.

  • For report heading

DATA: w_report_name_tx LIKE sy-repid,

grid TYPE REF TO cl_gui_alv_grid.

  • declare object of type eventhandler

DATA alv_layout TYPE lvc_s_layo.

DATA: g_f_del TYPE c,

g_f_ins TYPE c.

DATA: g_f_batch .

*----


*

  • SELECTION-SCREEN *

*----


*

  • Selection of file path

  • Application server

SELECTION-SCREEN BEGIN OF BLOCK frm1 WITH FRAME TITLE text-t01.

SELECTION-SCREEN: BEGIN OF LINE, POSITION 01.

PARAMETERS: g_p_ux RADIOBUTTON GROUP rad1 DEFAULT 'X'.

SELECTION-SCREEN: COMMENT 03(22) text-t02. "Name der Server-Datei

SELECTION-SCREEN POSITION 25.

PARAMETERS: g_p_dsn(50). " LOWER CASE. "PIP2KOR12042006

SELECTION-SCREEN: END OF LINE.

  • Presentation server

SELECTION-SCREEN: BEGIN OF LINE, POSITION 01.

PARAMETERS: g_p_pc RADIOBUTTON GROUP rad1.

SELECTION-SCREEN: COMMENT 03(20) text-t03. "Name der PC-Datei

SELECTION-SCREEN POSITION 25.

PARAMETERS: g_p_fil LIKE rlgrap-filename.

SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN END OF BLOCK frm1.

  • Table selection

SELECTION-SCREEN BEGIN OF BLOCK frm3 WITH FRAME TITLE text-t20.

SELECTION-SCREEN: BEGIN OF LINE, POSITION 01.

SELECTION-SCREEN: COMMENT 01(20) text-t05.

SELECTION-SCREEN POSITION 25.

PARAMETERS: g_p_tbnm TYPE ddobjname DEFAULT 'Z06PDE01_PAABZUG'.

SELECTION-SCREEN: END OF LINE.

  • For log file

SELECTION-SCREEN: BEGIN OF LINE, POSITION 01.

PARAMETERS: g_p_chk AS CHECKBOX.

SELECTION-SCREEN: COMMENT 03(22) text-t04.

SELECTION-SCREEN POSITION 25.

PARAMETERS: g_p_log(50). " LOWER CASE. "PIP2KOR12042006

SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN END OF BLOCK frm3.

  • Simulation and porduction mode

SELECTION-SCREEN BEGIN OF BLOCK frm2 WITH FRAME TITLE text-t10.

PARAMETERS: g_p_sm RADIOBUTTON GROUP rad2 DEFAULT 'X'.

PARAMETERS: g_p_pd RADIOBUTTON GROUP rad2.

  • Delete table before processing YES / NO

SELECTION-SCREEN BEGIN OF BLOCK frm4 WITH FRAME TITLE text-s05.

PARAMETERS: g_p_tdy RADIOBUTTON GROUP rad3.

PARAMETERS: g_p_tdn RADIOBUTTON GROUP rad3 DEFAULT 'X'.

SELECTION-SCREEN END OF BLOCK frm4.

  • Processing options

SELECTION-SCREEN BEGIN OF BLOCK frm5 WITH FRAME TITLE text-s06.

PARAMETERS: g_p_iu RADIOBUTTON GROUP rad4 DEFAULT 'X'.

PARAMETERS: g_p_in RADIOBUTTON GROUP rad4.

PARAMETERS: g_p_up RADIOBUTTON GROUP rad4.

PARAMETERS: g_p_dl RADIOBUTTON GROUP rad4.

SELECTION-SCREEN END OF BLOCK frm5.

SELECTION-SCREEN BEGIN OF LINE.

PARAMETERS: g_p_del AS CHECKBOX.

SELECTION-SCREEN COMMENT 3(50) text-022 FOR FIELD g_p_del.

SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN END OF BLOCK frm2.

PARAMETER : g_p_tr1(12) TYPE c NO-DISPLAY.

PARAMETER : g_p_tr2(12) TYPE c NO-DISPLAY.

*----


*

  • INITIALISATION

*----


*

  • Do not allow execution in P70

INITIALIZATION.

  • IF sy-sysid CS 'P'.

  • MESSAGE a398(00) WITH text-021.

  • ENDIF.

*----


*

  • AT SELECTION-SCREEN *

*----


*

  • Capture of path

AT SELECTION-SCREEN ON VALUE-REQUEST FOR g_p_fil.

PERFORM f4_for_datei.

AT SELECTION-SCREEN.

  • Check for empty path

IF NOT g_p_ux IS INITIAL AND g_p_dsn IS INITIAL.

MESSAGE e016(pn) WITH text-010.

ENDIF.

IF NOT g_p_pc IS INITIAL AND g_p_fil IS INITIAL.

MESSAGE e016(pn) WITH text-011.

ENDIF.

  • When the program enters validations second time after

  • SJOB and ENTER, SY-UCOMM will be space

  • So, in order to execute the validations, control using a flag

IF sy-ucomm = 'SJOB' .

g_f_batch = 'X'.

ENDIF.

  • Set the UCOMM if batch flag is set

IF g_f_batch = 'X'.

sy-ucomm = 'SJOB' .

ENDIF.

  • Do not execute in batch when uploaded from PC

IF sy-ucomm = 'SJOB' AND NOT g_p_pc IS INITIAL.

MESSAGE e016(pn) WITH text-002.

ENDIF.

IF NOT g_p_chk IS INITIAL AND g_p_log IS INITIAL.

MESSAGE e016(pn) WITH text-010.

ENDIF.

  • Logging to Appl server must when executed in batch

IF sy-ucomm = 'SJOB' AND g_p_chk IS INITIAL.

MESSAGE e016(pn) WITH text-023 text-024.

sy-ucomm = 'SJOB'.

ENDIF.

  • If all the validations are successful, clear the flag

IF sy-ucomm = space .

CLEAR g_f_batch.

ENDIF.

*----


*

  • Start of selection *

*----


*

START-OF-SELECTION.

CREATE DATA g_t_upload_tab TYPE TABLE OF (g_p_tbnm).

CREATE DATA g_t_upload_tab_chk TYPE TABLE OF (g_p_tbnm).

CREATE DATA g_t_upload_tab_del TYPE TABLE OF (g_p_tbnm).

ASSIGN g_t_upload_tab->* TO .

  • Initialise the buttons for alv

g_f_ins = 'Y'.

g_p_tr2 = text-013.

  • Upload of data from file to table

  • Deletion of previously available records in table

PERFORM file_upload.

  • Write log file to application server

IF NOT g_p_chk IS INITIAL.

PERFORM write_log.

ENDIF.

*&----


*

*& Form f4_for_datei

*&----


*

  • Selection of file path

*----


*

FORM f4_for_datei.

  • Lokale Daten

DATA: l_file_table TYPE filetable.

DATA: l_rc TYPE i.

DATA: l_filename TYPE string.

DATA: filen TYPE string,

filename TYPE string,

path TYPE string.

CALL METHOD cl_gui_frontend_services=>file_open_dialog

EXPORTING

window_title = 'Ausgabefile'

CHANGING

file_table = l_file_table

rc = l_rc

EXCEPTIONS

file_open_dialog_failed = 1

cntl_error = 2

error_no_gui = 3

not_supported_by_gui = 4

OTHERS = 5.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ELSE.

LOOP AT l_file_table INTO l_filename.

ENDLOOP.

g_p_fil = l_filename.

ENDIF.

ENDFORM. " f4_for_datei

*&----


*

*& Form file_upload

*&----


*

  • Upload of file

*----


*

FORM file_upload .

  • Data upload from application server

  • Open dataset

IF NOT g_p_ux IS INITIAL.

OPEN DATASET g_p_dsn FOR INPUT IN BINARY MODE.

IF sy-subrc <> 0.

SET CURSOR FIELD 'G_P_DSN'.

MESSAGE e016(pn) WITH

'Eingangsdatei nicht geöffnet Sy-Subrc ='(e50)

sy-subrc.

ENDIF.

  • Write data into internal table

REFRESH .

ENDIF.

ENDDO.

  • Close dataset

CLOSE DATASET g_p_dsn.

  • Upload file to database

PERFORM upload_db.

  • Data upload from PC

ELSE.

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

filename = g_p_fil

filetype = 'DAT'

TABLES

data_tab = 0.

SET CURSOR FIELD 'g_p_fil'.

MESSAGE e016(pn) WITH

'Fehler in WS_UPLOAD Sy-Subrc ='(e60)

sy-subrc.

ENDIF.

IF sy-subrc = 0.

  • Upload file to table

PERFORM upload_db.

ENDIF.

ENDIF.

ENDFORM. " file_upload

*&----


*

*& Form upload_db

*&----


*

  • Upload of data to table

*----


*

FORM upload_db .

DATA : g_l_messg(256) TYPE c.

DATA : g_l_dbcnt(10) TYPE c.

DATA : g_f_lines type i.

  • Deletion of records

DESCRIBE TABLE 0.

  • Delete records from database

  • Delete only if the Delete before processing is yes.

IF NOT g_p_tdy IS INITIAL.

PERFORM delete_records.

ENDIF.

ENDIF.

  • For Simulation

IF NOT g_p_sm IS INITIAL.

  • Below records will be uploaded

PERFORM write_tab.

  • For production

ELSE.

  • Begin of deletion PIP2KOR24072005

  • MODIFY (g_p_tbnm) FROM TABLE <f_s_upload_tab>.

  • COMMIT WORK.

  • IF sy-subrc = 0.

  • MESSAGE s016(pn) WITH text-001.

  • " records have been uploaded

  • ENDIF.

  • End of deletion PIP2KOR24072005

  • Begin of insertion PIP2KOR24072005

  • For Processing Options

CASE 'X'.

WHEN g_p_iu.

MODIFY (g_p_tbnm) FROM TABLE LINES g_f_lines.

g_l_dbcnt = g_f_lines - sy-dbcnt.

CONCATENATE text-037 g_l_dbcnt INTO g_l_messg.

MESSAGE i016(pn) WITH g_l_messg.

EXIT.

ENDIF.

ENDCASE.

  • End of insertion PIP2KOR24072005

  • Log after uploading

  • Below records have been uploaded

PERFORM write_tab.

  • If requested, delete the dataset

IF g_p_del = 'X' AND g_p_ux = 'X'.

DELETE DATASET g_p_dsn.

ENDIF.

ENDIF.

ENDFORM. " upload_db

*&----


*

*& Form write_tab

*&----


*

  • write tab

*----


*

FORM write_tab .

  • ALV display

PERFORM call_alv.

ENDFORM. " write_tab

*&----


*

*& Form delete_records

*&----


*

  • delete records from database

*----


*

FORM delete_records .

  • Deletion of records already existing in DB table

  • In production mode

IF NOT g_p_pd IS INITIAL .

  • For Log

REFRESH .

  • deletion of records

DELETE FROM (g_p_tbnm).

COMMIT WORK.

  • Write log

  • Below Records are deleted

PERFORM write_tab_del.

CLEAR .

ELSE.

  • In simulation mode - List of records that will be deleted

REFRESH .

  • Below Records will be deleted

PERFORM write_tab_del.

CLEAR .

ENDIF.

ENDFORM. " delete_records

*&----


*

*& Form write_tab_del

*&----


*

  • write deleted records

*----


*

FORM write_tab_del .

  • ALV display

PERFORM call_alv.

ENDFORM. " write_tab_del

*&----


*

*& Form call_alv_del

*&----


*

*

*----


*

FORM call_alv .

  • Display:

PERFORM display_data.

ENDFORM. " call_alv_del

*&----


*

*& Form display_data

*&----


*

  • display data

*----


*

FORM display_data.

  • Build the Field catalog

PERFORM build_catalog.

  • Call the screen

CALL SCREEN 100.

ENDFORM. " display_data

*&----


*

*& Form build_catalog

*&----


*

  • build catalog

*----


*

FORM build_catalog.

  • Build the catalog using internal table definition

g_f_repid = sy-repid.

g_r_disp_variant-report = sy-repid.

g_r_disp_variant-handle = '0100'.

PERFORM build_catalog_from_itab_disp

TABLES g_t_fieldcat

USING g_f_repid

g_f_repid.

ENDFORM. " build_catalog

*&----


*

*& Module STATUS_0100 OUTPUT

*&----


*

  • PBO for screen 0100

*----


*

MODULE status_0100 OUTPUT.

  • Set PF Status and title bar

SET PF-STATUS 'MAIN100'.

IF g_f_ins = 'Y'.

  • Fill the table

[].

SET TITLEBAR 'TITLE2' WITH text-020.

ENDIF.

  • Set the ALV grid

IF g_o_custom_container IS INITIAL.

IF sy-batch IS INITIAL .

  • Instantiate the Custom container

CREATE OBJECT g_o_custom_container

EXPORTING container_name = g_f_container.

ENDIF.

  • Instantiate the Grid control

CREATE OBJECT g_o_grid

EXPORTING i_parent = g_o_custom_container.

ENDIF.

  • CLEAR <f_s_output_alv>.

  • Display the ALV (Summary or detail)

CALL METHOD g_o_grid->set_table_for_first_display

EXPORTING

is_variant = g_r_disp_variant

i_save = 'A'

i_default = 'X'

CHANGING

it_outtab = 0.

ENDIF.

ENDMODULE. " STATUS_0100 OUTPUT

*&----


*

*& Module USER_COMMAND_0100 INPUT

*&----


*

  • PAI for screen 0100

*----


*

MODULE user_command_0100 INPUT.

  • to react on oi_custom_events:

CALL METHOD cl_gui_cfw=>dispatch.

CASE g_f_ok_code.

WHEN 'EXIT'.

LEAVE PROGRAM.

WHEN 'CANCEL' OR 'BACK'.

LEAVE TO SCREEN 0.

WHEN 'DEL'.

g_f_del = 'Y'.

CLEAR g_f_ins.

g_p_tr1 = text-014.

CLEAR g_p_tr2.

LEAVE TO SCREEN 0100.

WHEN 'INS'.

g_f_ins = 'Y'.

CLEAR g_f_del.

g_p_tr2 = text-013.

CLEAR g_p_tr1.

LEAVE TO SCREEN 0100.

ENDCASE.

CLEAR g_f_ok_code.

ENDMODULE. " USER_COMMAND_0100 INPUT

*&----


*

*& Form build_catalog_from_itab *

*&----


*

  • Build the catalog *

  • Convert to the ABAP objects interface *

*----


*

FORM build_catalog_from_itab_disp

TABLES t_l_r_int_fieldcat_tab TYPE lvc_t_fcat

USING i_pgm_name

  • i_itab_name

i_incl_name.

TYPE-POOLS: slis.

  • Work Fields for ALV

DATA: l_r_int_fieldcat_def TYPE slis_t_fieldcat_alv,

l_r_int_fieldcat_tab TYPE lvc_t_fcat WITH HEADER LINE,

l_r_int_fieldcat_tab_temp TYPE slis_t_fieldcat_alv.

  • Build the field catalog dynamically by reading the definition of

  • internal table to be displayed (i_itab_name)

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_program_name = i_pgm_name

i_structure_name = g_p_tbnm

i_client_never_display = 'X'

CHANGING

ct_fieldcat = l_r_int_fieldcat_tab_temp[]

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

  • Convert the catalog to suit ABAP objects ALV interface

PERFORM convert_catalog_disp TABLES l_r_int_fieldcat_tab_temp

t_l_r_int_fieldcat_tab.

ENDFORM. " BUILD_CATALOG__from_itab_disp

*&----


*

*& Form CONVERT_CATALOG

*&----


*

  • Convert the dynamically built catalog into the format required

  • by ABAP objects ALV

*----


*

FORM convert_catalog_disp TABLES t_l_r_int_fieldcat_tab_temp TYPE

slis_t_fieldcat_alv t_l_r_int_fieldcat_tab TYPE lvc_t_fcat.

DATA:l_r_int_fieldcat_tab_temp_wa TYPE slis_t_fieldcat_alv WITH

HEADER LINE.

LOOP AT t_l_r_int_fieldcat_tab_temp INTO l_r_int_fieldcat_tab_temp_wa.

MOVE-CORRESPONDING l_r_int_fieldcat_tab_temp_wa TO

t_l_r_int_fieldcat_tab.

t_l_r_int_fieldcat_tab-ref_field =

l_r_int_fieldcat_tab_temp_wa-ref_fieldname.

t_l_r_int_fieldcat_tab-ref_table =

l_r_int_fieldcat_tab_temp_wa-ref_tabname.

t_l_r_int_fieldcat_tab-roundfield =

l_r_int_fieldcat_tab_temp_wa-roundfieldname.

t_l_r_int_fieldcat_tab-decimals_o =

l_r_int_fieldcat_tab_temp_wa-decimals_out.

t_l_r_int_fieldcat_tab-decmlfield =

l_r_int_fieldcat_tab_temp_wa-decimalsfieldname.

t_l_r_int_fieldcat_tab-dd_outlen =

l_r_int_fieldcat_tab_temp_wa-ddic_outputlen.

t_l_r_int_fieldcat_tab-scrtext_l =

l_r_int_fieldcat_tab_temp_wa-seltext_l.

t_l_r_int_fieldcat_tab-scrtext_m =

l_r_int_fieldcat_tab_temp_wa-seltext_m.

t_l_r_int_fieldcat_tab-scrtext_s =

l_r_int_fieldcat_tab_temp_wa-seltext_s.

t_l_r_int_fieldcat_tab-reptext =

l_r_int_fieldcat_tab_temp_wa-reptext_ddic.

APPEND t_l_r_int_fieldcat_tab.

ENDLOOP.

ENDFORM. " CONVERT_CATALOG_disp

*&----


*

*& Form write_log

*&----


*

  • log file on application server

*----


*

FORM write_log .

  • Data upload from application server

  • Open dataset

OPEN DATASET g_p_log FOR OUTPUT IN BINARY MODE.

IF sy-subrc <> 0.

SET CURSOR FIELD 'G_P_LOG'.

MESSAGE e016(pn) WITH

'Ausgabedatei nicht geöffnet Sy-Subrc ='(e50)

sy-subrc.

ENDIF.

  • Write data

IF NOT g_p_pd IS INITIAL.

TRANSFER text-008 TO g_p_log.

ELSE.

TRANSFER text-009 TO g_p_log.

ENDIF.

  • Log for Inserted records

LOOP AT TO g_p_log.

ENDLOOP.

TRANSFER '' TO g_p_log.

IF NOT g_p_pd IS INITIAL.

TRANSFER text-005 TO g_p_log.

ELSE.

TRANSFER text-006 TO g_p_log.

ENDIF.

  • Log for Deleted records

LOOP AT TO g_p_log.

ENDLOOP.

  • Close dataset

CLOSE DATASET g_p_dsn.

ENDFORM. " write_log

Reward if helps.

Thanks,

Krishna

Read only

0 Likes
847

I have used 'assign'...

I am facing an issue at the select statement:

SELECT *

FROM (lv_table)

INTO CORRESPONDING FIELDS OF TABLE <lt_tab1>

WHERE kappl = 'V'

AND kschl = lwa_tmp_tab-kschl

AND knuma = lwa_tmp_tab-knuma.

Read only

0 Likes
847

Hello,

have you declared the field-symbols for <lt_tab1>

like this.

Field-symbols <lt_tab1> type any table.

What is the issue you are facing.

Thanks,

Krishna

Read only

0 Likes
847

Hi

Considering your data have create a sample program. Please check if this helps you.

Report ZAATEST.

data: gt_fieldcat type LVC_T_FCAT,

gt_table type ref to data.

data: ls_fieldcat type lvc_s_fcat.

field-symbols: <gt_table> type table.

data: it_dfies like dfies occurs 0 with header line,

lv_table type ddobjname.

select single kotab from t681 into lv_table

where kvewe = 'A' and kotabnr = '005' and kappl = 'V'.

CALL FUNCTION 'DDIF_NAMETAB_GET'

EXPORTING

TABNAME = lv_table

TABLES

DFIES_TAB = it_dfies

EXCEPTIONS

NOT_FOUND = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

ENDIF.

loop at it_dfies.

ls_fieldcat-fieldname = it_dfies-fieldname.

ls_fieldcat-inttype = it_dfies-inttype.

ls_fieldcat-outputlen = it_dfies-outputlen.

append ls_fieldcat to gt_fieldcat.

endloop.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = gt_fieldcat

IMPORTING

ep_table = gt_table.

ASSIGN gt_table->* TO <gt_table>.

SELECT *

FROM (lv_table)

into table <gt_table>.

This has worked for me to populate data in <gt_table>

Thanks

Eswar

Note: Reward for helpful answers.