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

Former Member
0 Likes
1,318

Hi All,

1 . I have a field in my selection screen .Based on the user input the internal table columns should

be created.

2.

For example :

If i have 4 valuse given by user in the selection the program should crete 4 coulms .

A

B

C

D

Based on these 4 fields the data will be extracted from the Data base table.

A --- E

B---- F

C----G

D---H

the output will be displayed in a file like this

E F G H -


each value one column in the excel sheet.

Please let me know << Removed >>.

Edited by: Rob Burbank on May 14, 2009 11:01 AM

13 REPLIES 13
Read only

faisalatsap
Active Contributor
0 Likes
1,158

Hi,

Please Test the following Sample Code hope will help you to solve out your problem,

TABLES: dd02l.

PARAMETERS: tabname TYPE tabname OBLIGATORY.

DATA: itab_ref TYPE REF TO data.

CREATE DATA itab_ref TYPE STANDARD TABLE OF (tabname).
FIELD-SYMBOLS: <itab> TYPE STANDARD TABLE.
ASSIGN itab_ref->* TO <itab>.

SELECT * FROM (tabname) UP TO 10 ROWS
  INTO CORRESPONDING FIELDS OF TABLE <itab>.

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    filename              = 'C:\TEXT.XLS' " EXCEL File will save on you Local Drive C with name test.xls
    write_field_separator = 'X'
  TABLES
    data_tab              = <itab>.
IF sy-subrc NE 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

AT SELECTION-SCREEN.
  SELECT SINGLE * FROM dd02l
    WHERE tabname = tabname.
  IF sy-subrc NE 0.
    MESSAGE: 'Please Enter Correct Table Name' TYPE 'E'.
  ENDIF.

Please Reply if any Issue,

Best Regards,

Faisal

Edited by: Faisal Altaf on May 14, 2009 9:20 AM

Read only

Former Member
0 Likes
1,158

Hi...

use following sample code..

used from [url] saptechnical



REPORT  ZPP_FS_CHINTAN_2.

****************** HARDCODED "OLD STYLE" LOCAL TYPE
******************** THIS IS A NORMAL HARDCODED LOCAL TYPE

TYPES : BEGIN OF TYP_HARDCODED,
L_COUNT TYPE I,
LT_SFLIGHT TYPE SFLIGHT.
TYPES : END OF TYP_HARDCODED.
* CREATE A TABLE BASED ON HARDCODED TYPE
DATA : LT_HARDCODED TYPE TABLE OF TYP_HARDCODED.
****************** DYNAMIC "NEW WAVE" LOCAL TYPE *******************
TYPES: TYP_COUNT TYPE I.

FIELD-SYMBOLS : <LT_DYNAMIC> TYPE ANY TABLE.
DATA: DREF TYPE REF TO DATA,
ITAB_TYPE TYPE REF TO CL_ABAP_TABLEDESCR,
STRUCT_TYPE TYPE REF TO CL_ABAP_STRUCTDESCR,
ELEM_TYPE TYPE REF TO CL_ABAP_ELEMDESCR,
COMP_TAB TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
COMP_FLD TYPE CL_ABAP_STRUCTDESCR=>COMPONENT.

* WE READ INFORMATION ABOUT EACH FIELDS OF SFLIGHT (SEE ABAP FAQ #2)
STRUCT_TYPE ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( 'MARA' ).
* WE ALSO NEED THE INFORMATION ABOUT THE TYPE "TYP_COUNT", NOTE THAT
* WE SHOULD USE CLASS CL_ABAP_ELEMDESCR INSTEAD OF CL_ABAP_TYPEDESCR
ELEM_TYPE ?= CL_ABAP_ELEMDESCR=>DESCRIBE_BY_NAME( 'TYP_COUNT' ).

* WE READ ALL FLEIDS OF SFLIGHT AND CREATE A COMPONENT TABLE
COMP_TAB = STRUCT_TYPE->GET_COMPONENTS( ).

* WE ADD MANUALLY THE COUNTER

COMP_FLD-NAME = 'L_COUNT'.
COMP_FLD-TYPE = ELEM_TYPE.
INSERT COMP_FLD INTO COMP_TAB INDEX 1.

* WE CREATE THE STRUCTURE
STRUCT_TYPE = CL_ABAP_STRUCTDESCR=>CREATE( COMP_TAB ).
* ... AND THE INTERNAL TABLE

ITAB_TYPE = CL_ABAP_TABLEDESCR=>CREATE( STRUCT_TYPE ).
* THE NEW THING HERE IS THE "TYPE HANDLE" WHICH CREATE A POINTER TO A
* HANDLE

CREATE DATA DREF TYPE HANDLE ITAB_TYPE.

* WE FINALLY ASSIGN A FIELD SYMBOL TO THE POINTER BECAUSE WE CANNOT
* DIRECTLY ACCESS A POINTER.

ASSIGN DREF->* TO <LT_DYNAMIC>.

* AT THE END OF THIS SMALL PROGRAM, INTERNAL TABLE LT_HARDCODED AND
* LT_DYNAMIC ARE THE SAME

BREAK-POINT.

here 'L_COUNT' field is dynamically added in ITAB.

so loop for your table E, F , G and H....

like

loop at i_dyn into w_dyn

COMP_FLD-NAME = w_dyn-name.

COMP_FLD-TYPE = ELEM_TYPE.

INSERT COMP_FLD INTO COMP_TAB INDEX sy-index.

endloop...

Edited by: Chintan_SAP on May 14, 2009 9:30 AM

Edited by: Chintan_SAP on May 14, 2009 9:31 AM

Edited by: Chintan_SAP on May 14, 2009 9:32 AM

Read only

0 Likes
1,158

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = IT_LVC_CAT

IMPORTING

EP_TABLE = NEW_TABLE.

ASSIGN NEW_TABLE->* TO <L_TABLE>.

CREATE DATA NEW_LINE LIKE LINE OF <L_TABLE>.

ASSIGN NEW_LINE->* TO <L_LINE>. -


> i need to move this to work area.

How can i do that ?

Read only

0 Likes
1,158

Hi,

The main crux in creating a dynamic internal table using the method CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE is the fieldcatalog.

The fieldcatalog contains the details about the columns which you want.

If you specify 4 fields in the field-catalog along with their technical attributes then the method will create a dynamic internal table based on the fieldcatalog.

I hope this helps you in understanding the concept.

Regards,

Ankur Parab

Read only

0 Likes
1,158

But i just want to get the Header in a work area . How can i do that ?

Read only

0 Likes
1,158

You have an internal table and a work area, so just do a loop at your internal table.


LOOP AT <L_TABLE> INTO <L_LINE>. 

Read only

0 Likes
1,158

Hi Tashvi,

The code can be explained as follows:-

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = IT_LVC_CAT

IMPORTING

EP_TABLE = NEW_TABLE.

ASSIGN NEW_TABLE->* TO <L_TABLE>.

CREATE DATA NEW_LINE LIKE LINE OF <L_TABLE>.

ASSIGN NEW_LINE->* TO <L_LINE>. -


> i need to move this to work area

The method will create an internal table 'NEW_TABLE' depending upon the fieldcatalog IT_LVC_FCAT.

Note that NEW_TABLE is not an internal table but it is a data type.

The statement ASSIGN NEW_TABLE->* TO <L_TABLE> will basically assign the structure of NEW_TABLE to field symbol <L_TABLE>.

So <L_TABLE> will become a table of structure NEW_TABLE.

The statement CREATE DATA NEW_LINE LIKE LINE OF <L_TABLE> will create a plain stucture type which will have just the structure of NEW_TABLE

The statement ASSIGN NEW_LINE->* TO <L_LINE> will actually make teh field symbol <L_LINE> as a stucture or work area having a structure of NEW_TABLE.

So now you can loop <L_TABLE> into <L_LINE> safely.

Note that you will always have to check that both the fieldsymbols <L_TABLE> and <L_LINE> have been assigned before you do the loop statement else it might result in a dump.

I hope this makes you understand the concept.

Regards,

Ankur Parab

Read only

0 Likes
1,158

Let me explain you in detail.

In selection screen user may give Fieldname,rollname,domname,etc....

Purely depends on user selection...Each field equal to one column in the dynamic intrnal table.

call function 'DDIF_FIELDINFO_GET'

exporting

tabname = TABLENM -


.> if i give MARA here .It will get list of MARA fields with

  • field = FIELDNM fieldnames,data elements etc.... but this FM will get 44

LANGU = SY-LANGU fields data which are table properties.

  • Lfield = ' '

  • ALL_TYPES = ' '

  • IMPORTING

  • X030L_WA = WATAB

  • DDOBJTYPE =

  • DFIES_WA =

  • LINES_DESCR =

TABLES

DFIES_TAB = INTTAB

  • FIXED_VALUES =

EXCEPTIONS

NOT_FOUND = 1

INTERNAL_ERROR = 2

OTHERS = 3.

if sy-subrc <> 0.

WRITE:/ 'Table Name Not Found'.

Exit.

endif.

Select * from dd03l into table t_dd03l where fieldname in fieldnm.

select * from dd03t into table t_dd03t where fieldname in fieldnm and

DDLANGUAGE = 'EN'.

Sort t_dd03t by fieldname.

Delete adjacent duplicates from t_dd03t comparing fieldname.

sort t_dd03l by fieldname.

delete adjacent duplicates from t_dd03l comparing fieldname .

describe table t_dd03t lines n.

loop at t_dd03t.

if not t_dd03t-fieldname = 'SHLPORIGIN'.

is_lvc_cat-fieldname = t_dd03t-fieldname.

is_lvc_cat-seltext = t_dd03t-ddtext.

Read table t_dd03l with key fieldname = t_dd03t-fieldname.

if sy-subrc = 0.

IS_LVC_CAT-intlen = t_dd03l-leng.

append IS_LVC_CAT to IT_LVC_CAT.

endif.

else.

is_lvc_cat-fieldname = t_dd03t-fieldname.

IS_LVC_CAT-seltext = t_dd03t-ddtext.

IS_LVC_CAT-intlen = '60'.

append IS_LVC_CAT to IT_LVC_CAT.

endif.

endloop.

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = IT_LVC_CAT

IMPORTING

EP_TABLE = NEW_TABLE.

ASSIGN NEW_TABLE->* TO <L_TABLE>.

CREATE DATA NEW_LINE LIKE LINE OF <L_TABLE>.

ASSIGN NEW_LINE->* TO <L_LINE>. .......> Using this code i will be creating dynamic internal

table with user selection

Once this is done i need to get list of fields info from the INNTAB .

i have only one row in the dynamic internal table .

How can i know which column equal to which row in INNTAB.Please let me know.

Read only

0 Likes
1,158

Hi,

If i understand you correctly, you seem to have a problem in knowing the columns of the dynamic internal table.

Please note that the columns will be in a serialised way as u specifyin the fieldcatalog.

Suppose my fieldcatalog has 3 entries

A

B

C

Then the dynamic internal table will be as follows:-

A B C

If you want to access a particular column of the work area <L_LINE>

then you can access it as follows:-

field-symbols : <F_FIELD> type any.

Assign component 'A' of <L_LINE> to <F_FIELD>

or

Assign component 1 of <L_LINE> to <F_FIELD>

I hope this helps you.

Regards,

Ankur Parab

Read only

0 Likes
1,158

ASSIGN COMPONENT p_cnt of structure <l_line> TO <l_field>.

i have this statement correctly . When i check in Debug <l_field> is empty.

Please let me know << Removed >>.

Edited by: Rob Burbank on May 14, 2009 11:01 AM

Read only

0 Likes
1,158

That's possible, if the field you are currently assigning is empty as well. And that seems to be the case. Or you have already reached the last field of the structure, in which case to return code will not equal zero (is not initial).

Read only

0 Likes
1,158

Hi,

Please check my WIKI and you will understand.

https://wiki.sdn.sap.com/wiki/x/UYA_Bg

Does your internal table contain any data??

Regards,

Ankur Parab

Read only

Former Member
0 Likes
1,158

Tashvi - please do not use ASAP. Everyone's problem is important.

Rob