Application Development 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: 

Correct syntax to append records to my dynamic internal table.

Former Member
0 Kudos

Hi all,

Please read through the step 1 to 14, to get a clear understanding of my issue. The crux being i am incorrectly populating a dynamic table. I suspect the Assign statement to be the cause. Your suggestions will be greatly appreciated. Needless to mention the solution provider will be rewarded with handsome points.

Regards,

AG.

1. I have a type declared as follows:

TYPES: BEGIN OF ty_list,

werks LIKE mseg-werks, "Plant

matkl LIKE ekpo-matkl, "Material Group

matnr LIKE mseg-matnr, "Material

erfmg_01 LIKE mseg-erfmg, "Quantity

dmbtr_01 LIKE mseg-dmbtr, "Amount

erfmg_02 LIKE mseg-erfmg, "Quantity

dmbtr_02 LIKE mseg-dmbtr, "Amount

erfmg_03 LIKE mseg-erfmg, "Quantity

dmbtr_03 LIKE mseg-dmbtr, "Amount

erfmg_04 LIKE mseg-erfmg, "Quantity

dmbtr_04 LIKE mseg-dmbtr, "Amount

erfmg_05 LIKE mseg-erfmg, "Quantity

dmbtr_05 LIKE mseg-dmbtr, "Amount

erfmg_06 LIKE mseg-erfmg, "Quantity

dmbtr_06 LIKE mseg-dmbtr, "Amount

erfmg_07 LIKE mseg-erfmg, "Quantity

dmbtr_07 LIKE mseg-dmbtr, "Amount

erfmg_08 LIKE mseg-erfmg, "Quantity

dmbtr_08 LIKE mseg-dmbtr, "Amount

erfmg_09 LIKE mseg-erfmg, "Quantity

dmbtr_09 LIKE mseg-dmbtr, "Amount

erfmg_10 LIKE mseg-erfmg, "Quantity

dmbtr_10 LIKE mseg-dmbtr, "Amount

erfmg_11 LIKE mseg-erfmg, "Quantity

dmbtr_11 LIKE mseg-dmbtr, "Amount

erfmg_12 LIKE mseg-erfmg, "Quantity

dmbtr_12 LIKE mseg-dmbtr, "Amount

END OF ty_list.

2. I have an internal table as follow:

it_list TYPE STANDARD TABLE OF ty_list WITH DEFAULT KEY INITIAL SIZE 0

3. i have a field symbol as follows:

field-symbols <fs_list> LIKE LINE OF it_list,

4. i have a field catalog declared as follows:

DATA: LT_FIELDCATALOG type LVC_T_FCAT.

5. i have a header work area for the fieldcatalog as below:

DATA: LS_FIELDCATALOG type LVC_S_FCAT.

6. i build the field catalog by adding 3 obligatory fields.

*Appending the plant field.

ls_fieldcatalog-col_pos = l_contr.

ls_fieldcatalog-fieldname = 'WERKS'.

ls_fieldcatalog-seltext = 'PLANT'.

ls_fieldcatalog-outputlen = 5.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

*

*Appending the material group field.

add 1 to l_contr.

ls_fieldcatalog-col_pos = l_contr.

ls_fieldcatalog-fieldname = 'MATKL'.

ls_fieldcatalog-seltext = 'MATERIAL_GROUP'.

ls_fieldcatalog-outputlen = 10.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

*Appending the material field.

add 1 to l_contr.

ls_fieldcatalog-col_pos = l_contr.

ls_fieldcatalog-fieldname = 'MATNR'.

ls_fieldcatalog-seltext = 'MATERIAL'.

ls_fieldcatalog-outputlen = 18.

APPEND ls_fieldcatalog TO lt_fieldcatalog.

7. i build the rest of the field catalog dynamically based on a value from table p_it_mnth

if the month is present i add into the field catalog.

LOOP AT p_it_mnth ASSIGNING <fs_mnth>.

CASE <fs_mnth>-month_id.

WHEN 4.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_04'. l_mcurr = 'DMBTR_04'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 5.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_05'. l_mcurr = 'DMBTR_05'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 6.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_06'. l_mcurr = 'DMBTR_06'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 7.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_07'. l_mcurr = 'DMBTR_07'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 8.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_08'. l_mcurr = 'DMBTR_08'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 9.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_09'. l_mcurr = 'DMBTR_09'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 10.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_10'. l_mcurr = 'DMBTR_10'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 11.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_11'. l_mcurr = 'DMBTR_11'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 12.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_12'. l_mcurr = 'DMBTR_12'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 1.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_01'. l_mcurr = 'DMBTR_01'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 2.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_02'. l_mcurr = 'DMBTR_02'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN 3.

ADD 1 TO l_contr.

l_mquan = 'ERFMG_03'. l_mcurr = 'DMBTR_03'.

PERFORM append_to_fldcatalog USING l_mquan l_mcurr l_contr.

WHEN OTHERS.

CONTINUE.

ENDCASE.

ENDLOOP.

8. i am declaring some field symbols as follows:

FIELD-SYMBOLS: <FS_DATA> type ref to DATA,

<FS_1> type STaNDARD table,

<FS_2>,

9. i am declaring a new line as follows:

DATA: NEW_LINE type ref to data.

10. i am declaring another field symbol as follows:

DATA: LT_DATA type ref to DATA.

11. assign the target table and call function to create a dynamic internal table based on the field catalog.

assign LT_DATA to <FS_DATA>.

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = LT_FIELDCATALOG

importing

ep_table = <FS_DATA>

exceptions

generate_subpool_dir_full = 1

others = 2

.

if sy-subrc <> 0.

endif.

12. assigning values to the work areas as below after creating the dynamic table.

      • So <FS_1> now points to our dynamic internal table.

assign <FS_DATA>->* to <FS_1>.

      • Next step is to create a work area for our dynamic internal table.

create data NEW_LINE like line of <FS_1>.

        • Access contents of internal table

ASSIGN NEW_LINE->* TO <FS_2>.

13. appending values into the dynamic internal table as below:

LOOP AT IT_LIST ASSIGNING <FS_LIST>.

  • assign lt_fieldcatalog to <fs_fldc>.

LOOP AT LT_FIELDCATALOG assigning <fs_fldc>.

assign component <fs_fldc>-fieldname of structure <fs_list> to <fs_2>.

append <fs_2> to <fs_1>.

ENDLOOP.

ENDLOOP.

14. i get the below output:

1. werks is populated with "CHEN" and appended as first row.

2. werks is again populated with "SERVI" and the remaining "CES" is carried to the MATKL field and populated as second row ( instead of being the second field in the first row of the dynamic internal table).

3. the quantity and amount are populated in the last amount field and appended instead of being distributed to the corresponding fields in the same row .

CHEN

SERVI CES

0.000

0.00

0.000

0.00

0.000

0.00

0.000

0.00

0.000

0.00

0.000

0.00

0.000

0.00

4.000

50010.00

2 REPLIES 2

Former Member
0 Kudos

hi,

Compare your program with the below code .This is very useful program.

REPORT zmtable LINE-SIZE 255

LINE-COUNT 65.

*----


*

  • Program written by Stan Shuralyov

  • Maintain Table dynamicly (Table name is entered on Selection Screen)

  • Very powerfull program. Please maintain authorisation access and

  • restrict maintenance to Z-tables

  • Version 6.20 and up

  • (can be used in 4.6C with some modifications to block try - endtry

  • and classes)

*----


*

  • Version was revised thanks to François Henrotte comment

  • (www.eponasolutions.com)

  • Enhanced functionality with dynamic selection screen

*----


*

TABLES: sscrfields. "Fields on selection screens

TYPE-POOLS rsds.

DATA ds_clauses TYPE rsds_where.

DATA: BEGIN OF ifield OCCURS 0,

fieldname LIKE dd03l-fieldname,

position LIKE dd03l-position,

keyflag LIKE dd03l-keyflag,

datatype LIKE dd03l-datatype.

DATA: END OF ifield.

DATA: sl_step LIKE sy-tabix,

ss_step LIKE sy-subrc,

ss_act(1) TYPE c,

sl_lines LIKE sy-tfill,

sl_status LIKE sy-subrc,

sl_subrc LIKE sy-subrc,

sl_update(1) TYPE c,

sl_mandt(1) TYPE c,

len(6) TYPE n,

f_value(255) TYPE c,

sl_datum LIKE sy-datum,

sl_uzeit LIKE sy-uzeit,

price1(15) TYPE c,

price2(15) TYPE c,

mess(60) TYPE c,

d_stat LIKE sy-subrc,

m_stat LIKE sy-subrc,

slchar(6) TYPE c.

DATA: ref_ptr TYPE REF TO cx_root. "Root class more common

DATA: text TYPE string.

DATA: sl_index LIKE sy-tabix.

DATA: zauth LIKE dd02l-tabname.

DATA: num TYPE i,

max_len TYPE i,

check_len TYPE i,

sl_sel(1) TYPE c.

TYPE-POOLS: icon.

*----


*

  • SELECTION-SCREEN.

*----


*

SELECTION-SCREEN BEGIN OF LINE.

  • text-012 - 'Table Name'

SELECTION-SCREEN COMMENT 1(25) text-012.

PARAMETERS: tabname LIKE dd02l-tabname DEFAULT 'ZSCARE'.

  • text-003 - 'Selection'

SELECTION-SCREEN PUSHBUTTON 75(9) text-003 USER-COMMAND sta1.

SELECTION-SCREEN END OF LINE.

  • numrows(text) - 'Max Number of ROWS'

PARAMETERS: numrows LIKE sy-subrc DEFAULT '100'.

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

  • At Selection-Screen *

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

AT SELECTION-SCREEN.

CASE sscrfields-ucomm.

WHEN 'STA1'.

CLEAR sl_sel.

CALL FUNCTION 'ZSTAN_SELECTIONS'

EXPORTING

tabname = tabname

IMPORTING

ds_clauses = ds_clauses

EXCEPTIONS

table_not_valid = 1

other_error = 2

OTHERS = 3.

IF sy-subrc = 0.

sl_sel = 'X'.

ENDIF.

ENDCASE.

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

*At Selection-Screen Output *

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

AT SELECTION-SCREEN OUTPUT.

SELECT SINGLE tabname

INTO tabname

FROM dd02l

WHERE tabname = tabname

AND as4local = 'A'

AND ( tabclass = 'TRANSP' OR tabclass = 'POOL'

OR tabclass = 'CLUSTER' ).

IF sy-subrc <> 0.

MESSAGE 'Table is not valid' TYPE 'S'.

RETURN.

ENDIF.

*----


*

  • START-OF-SELECTION.

*----


*

START-OF-SELECTION.

DEFINE: acheck.

zauth = 'ZTABAUTH'.

select single statu

into sl_update

from (zauth)

where tabname = tabname

and bname = sy-uname.

if sy-subrc .

  • For DELETION

DATA: ptr_itd TYPE REF TO data.

FIELD-SYMBOLS: .

  • For MODIFICATION

DATA: ptr_itm TYPE REF TO data.

FIELD-SYMBOLS: '.

  • Standard list status with 'SAVE' button

SET PF-STATUS 'STLI'.

CLEAR sl_update.

  • Maintain authorisation access in table ZTABAUTH

  • Key fields: tabname - Table name

  • bname = sy-uname - User name

  • statu = 'X' - maintain

  • ' ' - view

  • Check authorisation access

acheck.

SELECT fieldname position keyflag datatype

INTO TABLE ifield

FROM dd03l

WHERE tabname = tabname

AND fieldname NOT LIKE '.INCLU%'

ORDER BY position.

FIELD-SYMBOLS: LINES sl_lines.

  • Show two extra lines to allow addition up to 2 new lines

IF sl_update = 'X'.

DO 2 TIMES.

APPEND INITIAL LINE TO .

IF sy-tabix LE sl_lines.

ss_step = 1.

ELSE.

CLEAR ss_step.

ENDIF.

  • In field SS_STEP put D - to delete record

  • M - to modify/add new record

IF sl_update = 'X'.

WRITE:/ icon_change AS ICON.

IF ss_step = 1.

WRITE: ss_act INPUT ON.

ELSE.

ss_act = 'M'.

WRITE: ss_act.

CLEAR ss_act.

ENDIF.

ELSE.

WRITE:/ icon_display AS ICON.

WRITE: ss_act COLOR 2.

ENDIF.

LOOP AT ifield.

  • Maintain client dependant tables in the same client

IF ifield-datatype = 'CLNT'.

sl_mandt = 'X'.

CONTINUE.

ENDIF.

CONCATENATE itabname '-' ifield-fieldname INTO tab_field.

ASSIGN (tab_field) TO .

ENDLOOP.

ENDLOOP.

*----


*

  • END-OF-SELECTION.

*----


*

END-OF-SELECTION.

*----


*

  • AT USER-COMMAND.

*----


*

AT USER-COMMAND.

CASE sy-ucomm.

WHEN 'SAVE'.

IF sl_update = 'X'.

CLEAR: sl_step,

sl_status,

sl_subrc,

.

ENDIF.

IF d_stat IS INITIAL AND m_stat IS INITIAL.

MESSAGE 'No changes were done' TYPE 'S'.

ELSE.

MESSAGE text TYPE 'S'.

ENDIF.

LEAVE.

ELSE.

MESSAGE text TYPE 'I'.

EXIT.

ENDIF.

ELSE.

LEAVE.

ENDIF.

ENDCASE.

*----


END----

-


  • Below is Function for a Dynamic Selection Screen

FUNCTION zstan_selections.

*"----

-


""Local interface:

*" IMPORTING

*" VALUE(TABNAME) LIKE DD02L-TABNAME DEFAULT 'ZSCARE'

*" EXPORTING

*" VALUE(DS_CLAUSES) TYPE RSDS_WHERE

*" EXCEPTIONS

*" TABLE_NOT_VALID

*" OTHER_ERROR

*"----

-


DATA texpr TYPE rsds_texpr.

DATA twhere TYPE rsds_twhere.

DATA trange TYPE rsds_trange.

DATA BEGIN OF qcat. "Selections View for

INCLUDE STRUCTURE rsdsqcat. "Free Selectoptions

DATA END OF qcat.

DATA BEGIN OF tabs OCCURS 10.

INCLUDE STRUCTURE rsdstabs.

DATA END OF tabs.

DATA BEGIN OF fields OCCURS 10.

INCLUDE STRUCTURE rsdsfields.

DATA END OF fields.

DATA BEGIN OF efields OCCURS 10.

INCLUDE STRUCTURE rsdsfields.

DATA END OF efields.

DATA selid LIKE rsdynsel-selid.

DATA actnum LIKE sy-tfill.

DATA title LIKE sy-title VALUE 'Selection Screen'.

DATA: maxnum LIKE sy-subrc VALUE '69'.

CLEAR tabs.

tabs-prim_tab = tabname.

COLLECT tabs.

DATA: position LIKE dd03l-position.

DATA: keyflag LIKE dd03l-keyflag.

CLEAR fields.

fields-tablename = tabname.

fields-sign = 'I'.

DATA: step LIKE sy-subrc.

SELECT fieldname keyflag position

INTO (fields-fieldname, keyflag, position)

FROM dd03l

WHERE tabname = tabname

AND fieldname NOT LIKE '.INCLU%'

AND datatype NE 'CLNT'

ORDER BY position.

ADD 1 TO step.

CHECK step LE maxnum.

IF keyflag <> 'X'.

efields = fields.

APPEND efields.

ENDIF.

APPEND fields.

ENDSELECT.

IF sy-subrc <> 0.

RAISE table_not_valid.

ENDIF.

CALL FUNCTION 'FREE_SELECTIONS_INIT'

EXPORTING

expressions = texpr

kind = 'F'

IMPORTING

selection_id = selid

expressions = texpr

where_clauses = twhere

field_ranges = trange

number_of_active_fields = actnum

TABLES

tables_tab = tabs

fields_tab = fields

fields_not_selected = efields

EXCEPTIONS

fields_incomplete = 01

fields_no_join = 02

field_not_found = 03

no_tables = 04

table_not_found = 05

expression_not_supported = 06

incorrect_expression = 07

illegal_kind = 08

area_not_found = 09

inconsistent_area = 10

kind_f_no_fields_left = 11

kind_f_no_fields = 12

too_many_fields = 13.

IF sy-subrc = 0.

CALL FUNCTION 'FREE_SELECTIONS_DIALOG'

EXPORTING

selection_id = selid

title = title

IMPORTING

where_clauses = twhere

expressions = texpr

field_ranges = trange

number_of_active_fields = actnum

TABLES

fields_tab = fields

EXCEPTIONS

internal_error = 01

no_action = 02

no_fields_selected = 03

no_tables_selected = 04

selid_not_found = 05.

IF sy-subrc = 0.

CLEAR ds_clauses.

MOVE tabname TO ds_clauses-tablename.

READ TABLE twhere WITH KEY ds_clauses-tablename INTO ds_clauses.

IF sy-subrc <> 0.

RAISE other_error.

ENDIF.

ELSE.

RAISE other_error.

ENDIF.

ELSE.

RAISE other_error.

ENDIF.

ENDFUNCTION.

Don't forget to reward if useful...

Former Member
0 Kudos

Thanks muralikrishna kaipha, i have managed to resolve it with a simple work around. my complete code is on another thread" Incorrect appending to my dynamic internal table " in EXPERT FORUMS->ABAP DEVELOPMENT->ABAP PERFORMANCE AND TUNING.

i am pasting the extract for you information.

assign LT_DATA to <FS_DATA>.

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = LT_FIELDCATALOG

importing

ep_table = <FS_DATA>

exceptions

generate_subpool_dir_full = 1

others = 2

.

if sy-subrc <> 0.

endif.

      • So <FS_1> now points to our dynamic internal table.

assign <FS_DATA>->* to <FS_1>.

      • Next step is to create a work area for our dynamic internal table.

create data NEW_LINE like line of <FS_1>.

        • Access contents of internal table

ASSIGN NEW_LINE->* TO <FS_2>.

LOOP AT IT_LIST ASSIGNING <FS_LIST>.

move-corresponding <fs_list> to <fs_2>.

append <fs_2> to <fs_1>.

  • write:/ <fs_2>.

ENDLOOP.

data: l_lng type i.

data: l_wcn type string.

LOOP AT <fs_1> ASSIGNING <FS_2>.

l_lng = strlen( <Fs_2> ).

concatenate l_wcn <fs_2> into l_wcn.

if l_lng gt 255.

l_lng = l_lng - 255.

concatenate l_wcn <fs_2>+256(l_lng) into l_wcn.

endif.

write:/ l_wcn.

clear: l_wcn, l_lng.

ENDLOOP.

regards,

AG.