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

DYNAMIC INTERNAL TABLE -FIELDCATALOG?????

Former Member
0 Likes
2,728

Hi Experts ,

I require urgent help on dynamic internal table creation.

I want to create a Dynamic Internal Table with >30 but <50 fields where the first 4 fields of the table are diffrent and all fields generated after that are of type QUAN with length 13 .i want to have the contents displayed as an ALV,wherein i dont have to declare 50 fields in the field catalog! how do i go about doing dis?

HELP Please!!!

Regards,

Goldie.

1 ACCEPTED SOLUTION
Read only

gopi_narendra
Active Contributor
0 Likes
1,607

Welcome to SDN

Check this blog and implement it

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

Its much easier to understand.

Regards

Gopi

6 REPLIES 6
Read only

gopi_narendra
Active Contributor
0 Likes
1,608

Welcome to SDN

Check this blog and implement it

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

Its much easier to understand.

Regards

Gopi

Read only

0 Likes
1,607

hi

Gopi iv seen Rich Heilman's blog before it works fine for dict str/ db tabs...iv created the Dynamic Itab not from a dbtab , but my query is on how to display 40 fields in d ALV without Declaring each field in d field catalog!

If u do come across some useful info regarding my query pl do let me know ...

Thanks&Regards,

Goldie.

Read only

Former Member
0 Likes
1,607

HI

just use this code and change the fields according to ur requiremnt.

  • STEP: 1 - get backend field catalog (currently displayed alv)

CLEAR: tl_fieldcatalog. REFRESH: tl_fieldcatalog.

CALL METHOD w_grid->get_backend_fieldcatalog

IMPORTING

et_fieldcatalog = tl_fieldcatalog.

  • STEP: 2 - create a new fieldcatalog for dynamic internal table

CLEAR: sl_fieldcatalog.

CLEAR: t_outtab_fieldname. REFRESH: t_outtab_fieldname.

CLEAR: tl_fieldcatalog_new. REFRESH: tl_fieldcatalog_new.

CLEAR: t_download_fieldname. REFRESH: t_download_fieldname.

CLEAR: t_download_fieldheading. REFRESH: t_download_fieldheading.

LOOP AT tl_fieldcatalog INTO sl_fieldcatalog.

  • STEP: 2.1 - populate data in T_OUTTAB_FIELDNAME

APPEND sl_fieldcatalog-fieldname TO t_outtab_fieldname.

  • STEP: 2.2 - populate TL_FIELDCATALOG_NEW & T_DOWNLOAD_FIELDNAME

IF sl_fieldcatalog-no_out EQ ''.

IF sl_fieldcatalog-fieldname NE 'STATUS'

OR sl_fieldcatalog-fieldname NE 'MESG_STATUS'

OR sl_fieldcatalog-fieldname NE 'ZLOCK'

OR sl_fieldcatalog-fieldname NE 'T_PLANT'

OR sl_fieldcatalog-fieldname NE 'T_CSR'.

  • If field is COMM_PLANT, change its length

IF sl_fieldcatalog-fieldname EQ 'COMM_PLANT'.

sl_fieldcatalog-outputlen = 1800.

sl_fieldcatalog-intlen = 1800.

sl_fieldcatalog-dd_outlen = 1800.

ENDIF. "comm_plant

sl_fieldcatalog_new = sl_fieldcatalog.

APPEND sl_fieldcatalog_new TO tl_fieldcatalog_new.

APPEND sl_fieldcatalog-fieldname TO t_download_fieldname.

APPEND sl_fieldcatalog-scrtext_l TO t_download_fieldheading.

CLEAR: sl_fieldcatalog, sl_fieldcatalog_new.

ENDIF.

ENDIF.

ENDLOOP.

  • STEP: 3 - create dynamic internal table

FREE: ref_download.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

  • I_STYLE_TABLE =

it_fieldcatalog = tl_fieldcatalog_new

IMPORTING

ep_table = ref_download

  • E_STYLE_FNAME =

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.

ASSIGN ref_download->* TO <ft_download>.

CREATE DATA ref_wa LIKE LINE OF <ft_download>.

ASSIGN ref_wa->* TO <fs_download>.

  • STEP: 4 - populate data in dynamic internal table

LOOP AT t_outtab INTO wa_outtab.

LOOP AT t_download_fieldname.

ASSIGN COMPONENT t_download_fieldname OF STRUCTURE

<fs_download> TO <fs_download_field>.

IF t_download_fieldname-field EQ 'COMM_PLANT'.

  • STEP: 4.1 - get long text from database table

CLEAR: wal_table.

SELECT SINGLE * FROM zshaven_plnt_txt

INTO wal_table

WHERE vbeln = wa_outtab-vbeln

AND posnr = wa_outtab-posnr

AND del_no = wa_outtab-del_no

AND del_itm = wa_outtab-del_itm.

IF sy-subrc EQ 0.

  • STEP: 4.2 - break long-text into separate lines

CLEAR: tl_text. REFRESH: tl_text.

SPLIT wal_table-plant_comm

AT '~'

INTO TABLE tl_text.

  • STEP: 4.3 - Combine these separate lines with space in

  • between two lines

CLEAR: wal_text, final_text.

LOOP AT tl_text INTO wal_text.

IF final_text IS INITIAL.

final_text = wal_text.

ELSE.

CONCATENATE final_text '-' wal_text

INTO final_text.

REPLACE '-' WITH ' ' INTO final_text.

ENDIF.

ENDLOOP.

  • STEP: 4.4 - move long text to work-area

<fs_download_field> = final_text.

ENDIF. "subrc

ELSE. "t_download_fieldname

READ TABLE t_outtab_fieldname

WITH KEY field = t_download_fieldname-field.

ASSIGN COMPONENT t_outtab_fieldname-field OF STRUCTURE

wa_outtab TO <fs_outtab_field>.

<fs_download_field> = <fs_outtab_field>.

ENDIF.

ENDLOOP.

  • STEP: 4.5 - Move data from work-area to dynamic internal table

APPEND <fs_download> TO <ft_download>.

CLEAR: <fs_download>.

ENDLOOP.

  • STEP: 5 - download

CALL FUNCTION 'DOWNLOAD'

EXPORTING

filename = 'C:\zshaven.xls'

filetype = 'DAT'

filetype_no_show = 'X'

filetype_no_change = 'X'

TABLES

data_tab = <ft_download>

fieldnames = t_download_fieldheading

EXCEPTIONS

invalid_filesize = 1

invalid_table_width = 2

invalid_type = 3

no_batch = 4

unknown_error = 5

gui_refuse_filetransfer = 6

customer_error = 7

OTHERS = 8.

IF sy-subrc <> 0.

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

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

ENDIF.

REWARD IF HELPFUL;

vivekanand

Read only

0 Likes
1,607

hi,

id like to thank u for d post

iv just started wid ur code ....im a lil lost wid d declaration if u could post dat part too it will b helpful!will try it out and chk if it solves my problem!

Thanks & Regards,

Goldie.

Read only

0 Likes
1,607

HI,

see this code.

REPORT ZDYN_ITAB.

PARAMETERS:DB_TABLE(30).

DATA FCAT1 TYPE LVC_T_FCAT.

DATA:DYN_ITAB TYPE REF TO DATA,

WA TYPE REF TO DATA.

FIELD-SYMBOLS: <DISP_TABLE> TYPE TABLE,

<WA> TYPE ANY.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = DB_TABLE

CHANGING

CT_FIELDCAT = FCAT1[].

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = FCAT1[]

IMPORTING

EP_TABLE = DYN_ITAB.

ASSIGN DYN_ITAB->* TO <DISP_TABLE>.

CREATE DATA WA LIKE LINE OF <DISP_TABLE>.

ASSIGN WA->* TO <WA>.

SELECT * FROM (db_table) INTO <WA>.

APPEND <WA> TO <DISP_table>.

ENDSELECT.

DESCRIBE TABLE <DISP_table>.

WRITE:/ SY-TFILL.

rgds,

bharat.

Read only

Former Member
0 Likes
1,607

Hi,

Please check the following code it willdefinitly serve your perpose.

type-pools: slis.

field-symbols: <dyn_table> type standard table,

<dyn_wa>.

data: alv_fldcat type slis_t_fieldcat_alv,

it_fldcat type lvc_t_fcat.

selection-screen begin of block b1 with frame title text-001.

parameters: p_flds(5) type c.

selection-screen end of block b1.

start-of-selection.

  • build the dynamic internal table

perform build_dyn_itab.

  • write 5 records to the alv grid

do 5 times.

perform build_report.

enddo.

  • call the alv grid.

perform call_alv.

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

  • Build_dyn_itab

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

form build_dyn_itab.

data: new_table type ref to data,

new_line type ref to data,

wa_it_fldcat type lvc_s_fcat.

  • Create fields .

  • clear wa_it_fldcat.

  • wa_it_fldcat-fieldname = name1.

  • wa_it_fldcat-datatype = 'mara-matnr'.

    • wa_it_fldcat-intlen = 5.

  • append wa_it_fldcat to it_fldcat .

*

*clear wa_it_fldcat.

  • wa_it_fldcat-fieldname = sy-index.

  • wa_it_fldcat-datatype = 'CHAR'.

  • wa_it_fldcat-intlen = 5.

  • append wa_it_fldcat to it_fldcat .

*

do p_flds times.

clear wa_it_fldcat.

wa_it_fldcat-fieldname = sy-index.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 6.

append wa_it_fldcat to it_fldcat .

enddo.

  • Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = it_fldcat

importing

ep_table = new_table.

assign new_table->* to <dyn_table>.

  • Create dynamic work area and assign to FS

create data new_line like line of <dyn_table>.

assign new_line->* to <dyn_wa>.

endform.

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

  • Form build_report

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

form build_report.

data: fieldname(20) type c.

data: fieldvalue(5) type c.

data: index(3) type c.

field-symbols: <fs1>.

do p_flds times.

index = sy-index.

  • Set up fieldvalue

concatenate 'FLD' index into

fieldvalue.

condense fieldvalue no-gaps.

assign component index of structure <dyn_wa> to <fs1>.

<fs1> = fieldvalue.

enddo.

  • Append to the dynamic internal table

append <dyn_wa> to <dyn_table>.

endform.

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

  • CALL_ALV

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

form call_alv.

data: wa_cat like line of alv_fldcat.

*clear wa_cat.

  • wa_cat-fieldname = matnr.

  • wa_cat-seltext_s = sy-index.

  • wa_cat-outputlen = 'ma'.

  • append wa_cat to alv_fldcat.

*

do p_flds times.

clear wa_cat.

wa_cat-fieldname = sy-index.

wa_cat-seltext_s = sy-index.

wa_cat-outputlen = '6'.

append wa_cat to alv_fldcat.

enddo.

  • Call ABAP List Viewer (ALV)

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

it_fieldcat = alv_fldcat

tables

t_outtab = <dyn_table>.

endform.

Reward if found use full

Regards,

Ram.