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 column,How to create .

Former Member
0 Likes
715

Hi all

My requirement is to convert the layout as bellow:

before:

column1 column2 column3

1 A 1

1 B 1

1 C 2

2 A 1

2 C 1

convert to :

column1 A B C

1 1 1 2

2 1 0 1

The detail requirement is first to select all the customers and then to get the customers sales informations . the customer's name should be the internal field name .

can anyone give me some suggestions? thanks a lot.

1 ACCEPTED SOLUTION
Read only

anversha_s
Active Contributor
0 Likes
687

hi,

chk this.

Types: begin of ttab,
fld1(10) type c,
fld2 type sy-datum,
end of ttab.

data: itab type table of ttab.

You can also build an internal table at runtime.
Here is a sample program.





report zxy_0003
       no standard page heading.
 
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_check type c.
selection-screen end of block b1.
 
start-of-selection.
 
  perform build_dyn_itab.
  perform build_report.
 
  loop at <dyn_table> into <dyn_wa>.
    write:/ <dyn_wa>.
  endloop.
 
 
************************************************************************
*  Build_dyn_itab
************************************************************************
form build_dyn_itab.
 
  data: index(3) type c.
 
  data: new_table type ref to data,
        new_line  type ref to data,
        wa_it_fldcat type lvc_s_fcat.
 
* Create fields
  clear index.
  do 10 times.
    index = sy-index.
    clear wa_it_fldcat.
    concatenate 'Field' index into
             wa_it_fldcat-fieldname .
    condense  wa_it_fldcat-fieldname no-gaps.
    wa_it_fldcat-datatype = 'CHAR'.
    wa_it_fldcat-intlen = 5.
    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 10 times.
 
    index = sy-index.
 
* Set up fieldname
    concatenate 'FIELD' index into
             fieldname .
    condense   fieldname  no-gaps.
 
* Set up fieldvalue
    concatenate 'FLD' index into
             fieldvalue.
    condense   fieldvalue no-gaps.
 
    assign component  fieldname  of structure <dyn_wa> to <fs1>.
    <fs1> =  fieldvalue.
 
  enddo.
 
* Append to the dynamic internal table
  append <dyn_wa> to <dyn_table>.
 
endform.

check these links:

http://searchsap.techtarget.com/tip/1,289483,sid21_gci554038,00.html

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

rgds

Anver

5 REPLIES 5
Read only

anversha_s
Active Contributor
0 Likes
688

hi,

chk this.

Types: begin of ttab,
fld1(10) type c,
fld2 type sy-datum,
end of ttab.

data: itab type table of ttab.

You can also build an internal table at runtime.
Here is a sample program.





report zxy_0003
       no standard page heading.
 
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_check type c.
selection-screen end of block b1.
 
start-of-selection.
 
  perform build_dyn_itab.
  perform build_report.
 
  loop at <dyn_table> into <dyn_wa>.
    write:/ <dyn_wa>.
  endloop.
 
 
************************************************************************
*  Build_dyn_itab
************************************************************************
form build_dyn_itab.
 
  data: index(3) type c.
 
  data: new_table type ref to data,
        new_line  type ref to data,
        wa_it_fldcat type lvc_s_fcat.
 
* Create fields
  clear index.
  do 10 times.
    index = sy-index.
    clear wa_it_fldcat.
    concatenate 'Field' index into
             wa_it_fldcat-fieldname .
    condense  wa_it_fldcat-fieldname no-gaps.
    wa_it_fldcat-datatype = 'CHAR'.
    wa_it_fldcat-intlen = 5.
    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 10 times.
 
    index = sy-index.
 
* Set up fieldname
    concatenate 'FIELD' index into
             fieldname .
    condense   fieldname  no-gaps.
 
* Set up fieldvalue
    concatenate 'FLD' index into
             fieldvalue.
    condense   fieldvalue no-gaps.
 
    assign component  fieldname  of structure <dyn_wa> to <fs1>.
    <fs1> =  fieldvalue.
 
  enddo.
 
* Append to the dynamic internal table
  append <dyn_wa> to <dyn_table>.
 
endform.

check these links:

http://searchsap.techtarget.com/tip/1,289483,sid21_gci554038,00.html

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

rgds

Anver

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
687

Hi,

Check this piece of code which I got from SDN.

REPORT zmaschl_create_data_dynamic .

TYPE-POOLS: slis.

DATA: it_fcat TYPE slis_t_fieldcat_alv,

is_fcat LIKE LINE OF it_fcat.

DATA: it_fieldcat TYPE lvc_t_fcat,

is_fieldcat LIKE LINE OF it_fieldcat.

DATA: new_table TYPE REF TO data.

DATA: new_line TYPE REF TO data.

FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,

<l_line> TYPE ANY,

<l_field> TYPE ANY.

  • Build fieldcat

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'SYST'

CHANGING

ct_fieldcat = it_fcat[].

LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.

MOVE-CORRESPONDING is_fcat TO is_fieldcat.

is_fieldcat-fieldname = is_fcat-fieldname.

is_fieldcat-ref_field = is_fcat-fieldname.

is_fieldcat-ref_table = is_fcat-ref_tabname.

APPEND is_fieldcat TO it_fieldcat.

ENDLOOP.

  • Create a new Table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat

IMPORTING

ep_table = new_table.

  • Create a new Line with the same structure of the table.

ASSIGN new_table->* TO <l_table>.

CREATE DATA new_line LIKE LINE OF <l_table>.

ASSIGN new_line->* TO <l_line>.

  • Test it...

DO 30 TIMES.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

<l_field> = sy-index.

INSERT <l_line> INTO TABLE <l_table>.

ENDDO.

LOOP AT <l_table> ASSIGNING <l_line>.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

WRITE <l_field>.

ENDLOOP.

Read only

Former Member
0 Likes
687

hi,

check this link

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

Read only

0 Likes
687

Hi Anversha

thank you very much for your powerful help~~

Read only

Former Member
0 Likes
687

Hi,

REPORT zmaschl_create_data_dynamic .

TYPE-POOLS: slis.

DATA: it_fcat TYPE slis_t_fieldcat_alv,

is_fcat LIKE LINE OF it_fcat.

DATA: it_fieldcat TYPE lvc_t_fcat,

is_fieldcat LIKE LINE OF it_fieldcat.

DATA: new_table TYPE REF TO data.

DATA: new_line TYPE REF TO data.

FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,

<l_line> TYPE ANY,

<l_field> TYPE ANY.

  • Build fieldcat

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'SYST'

CHANGING

ct_fieldcat = it_fcat[].

LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.

MOVE-CORRESPONDING is_fcat TO is_fieldcat.

is_fieldcat-fieldname = is_fcat-fieldname.

is_fieldcat-ref_field = is_fcat-fieldname.

is_fieldcat-ref_table = is_fcat-ref_tabname.

APPEND is_fieldcat TO it_fieldcat.

ENDLOOP.

  • Create a new Table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat

IMPORTING

ep_table = new_table.

  • Create a new Line with the same structure of the table.

ASSIGN new_table->* TO <l_table>.

CREATE DATA new_line LIKE LINE OF <l_table>.

ASSIGN new_line->* TO <l_line>.

  • Test it...

DO 30 TIMES.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

<l_field> = sy-index.

INSERT <l_line> INTO TABLE <l_table>.

ENDDO.

LOOP AT <l_table> ASSIGNING <l_line>.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

WRITE <l_field>.

ENDLOOP.

http://www.sap-img.com/ab030.htm

Regards,

Laxmi.