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

creating Internal table with type specified in table field.

Former Member
0 Likes
959

Hello experts,

I'm popping up some fields from database table one of which contains table name.

I want to create internal table of type table which is specified in that field.

Ex.

IT_TAB TYPE TABLE OF IT_RSTBL-DBSTRUCT

Can somebody help me to do that.

Thanks & regards,

Sagar

Hello experts,

I'm popping up some fields from database table one of which contains table name.

I want to create internal table of type table which is specified in that field.

Ex.

IT_TAB TYPE TABLE OF IT_RSTBL-DBSTRUCT

Can somebody help me to do that.

Thanks & regards,

Sagar

8 REPLIES 8
Read only

Former Member
0 Likes
923

take a look at this code

  • ======================================================================

  • Stefanos Moschidis - SAP Certified Technical Consultant

  • ======================================================================

REPORT Z_DOWNLOAD_UPLOAD_ANY_TABLE .

  • ----------------------------------------------------------------------

  • Program for

  • ----------------------------------------------------------------------

  • 1. Downloading Data of any DB table to a tab delimited ASCII file

*

  • ----------------------------------------------------------------------

  • 2. Checking if a tab delimited ASCII file has the structure of a

  • DB table and showing its contents

  • ----------------------------------------------------------------------

  • 3. Uploading a tab delimited ASCII file to a DB table with the same

  • structure

  • ----------------------------------------------------------------------

  • 4. Showing the data of any DB table

*

  • ======================================================================

  • ======================================================================

  • DATA DECLARATIONS

  • ======================================================================

TYPES : DATA_OBJECT TYPE REF TO DATA.

DATA : MITAB TYPE REF TO DATA .

TYPE-POOLS : SLIS .

DATA : IT_FIELDCAT TYPE STANDARD TABLE OF slis_fieldcat_alv

WITH HEADER LINE .

DATA : IT_FIELDCATALOG Type LVC_T_FCAT .

DATA : WA_FIELDCATALOG Type LVC_S_FCAT .

DATA : I_STRUCTURE_NAME LIKE DD02L-TABNAME .

DATA : I_CALLBACK_PROGRAM LIKE SY-REPID .

DATA : DYN_LINE TYPE DATA_OBJECT .

FIELD-SYMBOLS : <fs_ITAB> TYPE STANDARD TABLE .

DATA : TABLE_NAME_IS_VALID TYPE C .

DATA : DYNAMIC_IT_INSTANTIATED TYPE C .

CONSTANTS BUTTONSELECTED TYPE C VALUE 'X' .

  • ======================================================================

  • SELECTION SCREEN DEFAULT

  • ======================================================================

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_tabl.

PARAMETERS : MTABLE_N LIKE RSRD1-TBMA_VAL

mATCHCODE OBJECT DD_DBTB_16 OBLIGATORY .

selection-screen end of line.

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_file.

PARAMETERS : MFILENAM LIKE RLGRAP-FILENAME .

selection-screen end of line.

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_down.

PARAMETERS : P_DOWNLD RADIOBUTTON GROUP GRP1

user-command M_UCOMM .

selection-screen end of line.

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_chkf.

PARAMETERS : P_chkfil RADIOBUTTON GROUP GRP1 .

selection-screen end of line.

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_upld.

PARAMETERS : P_UPLOAD RADIOBUTTON GROUP GRP1 .

selection-screen end of line.

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_show.

PARAMETERS : P_show_t RADIOBUTTON GROUP GRP1 .

selection-screen end of line.

  • ======================================================================

  • AT SELECTION SCREEN OUTPUT

  • ======================================================================

AT SELECTION-SCREEN OUTPUT .

PERFORM CHECK_FILENAME .

  • ======================================================================

  • AT SELECTION SCREEN ON VALUE REQUEST FOR FILENAME

  • ======================================================================

AT SELECTION-SCREEN ON VALUE-REQUEST FOR MFILENAM .

PERFORM F4_FOR_FILENAME .

  • ======================================================================

  • Initialization .

  • ======================================================================

Initialization .

T_tabl = 'Table Name' .

T_file = 'File Name' .

T_down = 'Download Table' .

T_chkf = 'Check File to Upload' .

T_upld = 'Upload File' .

T_show = 'Show Table Contents' .

  • ======================================================================

  • START OF SELECTION

  • ======================================================================

START-OF-SELECTION .

PERFORM CHECK_TABLE_NAME_IS_VALID .

  • ======================================================================

  • END OF SELECTION

  • ======================================================================

END-OF-SELECTION .

IF TABLE_NAME_IS_VALID EQ ' ' .

message i398(00) with 'INVALID TABLE NAME' .

ELSE .

PERFORM INSTANTIATE_DYNAMIC_INTERNAL_T .

CHECK DYNAMIC_IT_INSTANTIATED = 'X' .

CASE BUTTONSELECTED .

WHEN P_DOWNLD .

PERFORM SELECT_AND_DOWNLOAD .

when P_chkfil .

perform CHECK_FILE_TO_UPLOAD .

WHEN P_UPLOAD .

PERFORM UPLOAD_FROM_FILE .

when P_SHOW_T .

PERFORM SHOW_CONTENTS .

ENDCASE .

ENDIF .

&----


*& Form CHECK_TABLE_NAME_IS_VALID

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM CHECK_TABLE_NAME_IS_VALID.

DATA MCOUNT TYPE I .

tables dd02l .

CLEAR TABLE_NAME_IS_VALID .

SELECT COUNT(*) INTO MCOUNT FROM TADIR

WHERE PGMID = 'R3TR'

AND OBJECT = 'TABL'

AND OBJ_NAME = MTABLE_N .

IF MCOUNT EQ 1 .

clear dd02l .

select single * from dd02l where TABNAME = MTABLE_N .

IF SY-SUBRC eq 0.

if dd02l-tabclass = 'TRANSP' .

TABLE_NAME_IS_VALID = 'X' .

endif .

ENDIF.

ENDIF .

ENDFORM. " CHECK_TABLE_NAME_IS_VALID

&----


*& Form SELECT_AND_DOWNLOAD

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM SELECT_AND_DOWNLOAD.

CLEAR : <FS_ITAB> .

SELECT * FROM (MTABLE_N)

INTO CORRESPONDING FIELDS OF TABLE <FS_ITAB> .

PERFORM CHECK_FILENAME.

CALL FUNCTION 'WS_DOWNLOAD'

EXPORTING

FILENAME = MFILENAM

FILETYPE = 'DAT'

TABLES

DATA_TAB = <FS_ITAB>

EXCEPTIONS

FILE_OPEN_ERROR = 1

FILE_WRITE_ERROR = 2

INVALID_FILESIZE = 3

INVALID_TYPE = 4

NO_BATCH = 5

UNKNOWN_ERROR = 6

INVALID_TABLE_WIDTH = 7

GUI_REFUSE_FILETRANSFER = 8

CUSTOMER_ERROR = 9

OTHERS = 10.

IF SY-SUBRC eq 0.

MESSAGE I398(00) WITH 'Table' mtable_n

'successfully downloaded to '

mfilenam .

ENDIF.

ENDFORM. " SELECT_AND_DOWNLOAD

&----


*& Form UPLOAD_FROM_FILE

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM UPLOAD_FROM_FILE.

DATA : ANS TYPE C .

DATA : lines_of_itab TYPE I .

DATA : MSY_SUBRC TYPE I .

CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'

EXPORTING

TEXTLINE1 = 'Are you sure you wish to upload'

TEXTLINE2 = 'data from ASCII File to DB table '

TITEL = 'Confirmation of Data Upload'

IMPORTING

ANSWER = ANS.

if ans = 'J' .

PERFORM CHECK_FILENAME.

CLEAR MSY_SUBRC .

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

FILENAME = MFILENAM

FILETYPE = 'DAT'

TABLES

DATA_TAB = <FS_ITAB>

EXCEPTIONS

CONVERSION_ERROR = 1

FILE_OPEN_ERROR = 2

FILE_READ_ERROR = 3

INVALID_TYPE = 4

NO_BATCH = 5

UNKNOWN_ERROR = 6

INVALID_TABLE_WIDTH = 7

GUI_REFUSE_FILETRANSFER = 8

CUSTOMER_ERROR = 9

OTHERS = 10.

MSY_SUBRC = MSY_SUBRC + SY-SUBRC .

IF SY-SUBRC eq 0.

describe table <FS_ITAB> lines lines_of_itab .

IF LINES_OF_ITAB GT 0 .

DELETE (MTABLE_N) FROM TABLE <fs_itab> .

commit work .

insert (MTABLE_N) FROM TABLE <fs_itab> .

MSY_SUBRC = MSY_SUBRC + SY-SUBRC .

ENDIF .

ENDIF.

IF MSY_SUBRC EQ 0 .

MESSAGE I398(00) WITH LINES_OF_ITAB

'Record(s) inserted in table'

MTABLE_N .

ELSE .

MESSAGE I398(00) WITH

'Errors occurred No Records inserted in table'

MTABLE_N .

ENDIF .

endif .

ENDFORM. " UPLOAD_FROM_FILE

&----


*& Form F4_FOR_FILENAME

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM F4_FOR_FILENAME.

CALL FUNCTION 'WS_FILENAME_GET'

EXPORTING

DEF_PATH = 'C:\'

MASK = ',.,..'

MODE = '0'

IMPORTING

FILENAME = MFILENAM

EXCEPTIONS

INV_WINSYS = 1

NO_BATCH = 2

SELECTION_CANCEL = 3

SELECTION_ERROR = 4

OTHERS = 5.

ENDFORM. " F4_FOR_FILENAME

&----


*& Form CHECK_FILENAME

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM CHECK_FILENAME.

IF MFILENAM IS INITIAL

and not ( MTABLE_N is initial )

AND P_show_t ne BUTTONSELECTED.

CONCATENATE 'C:\' MTABLE_N '.TXT' INTO MFILENAM.

ENDIF .

ENDFORM. " CHECK_FILENAME

&----


*& Form INSTANTIATE_DYNAMIC_INTERNAL_T

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM INSTANTIATE_DYNAMIC_INTERNAL_T.

CLEAR DYNAMIC_IT_INSTANTIATED .

  • -----> Step 1 - Finding Field Names and ALV GRID Fieldcatalog

I_STRUCTURE_NAME = MTABLE_N .

CLEAR IT_FIELDCAT[] .

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = I_STRUCTURE_NAME

CHANGING

CT_FIELDCAT = IT_FIELDCAT[]

EXCEPTIONS

INCONSISTENT_INTERFACE = 1

PROGRAM_ERROR = 2

OTHERS = 3.

IF SY-SUBRC EQ 0.

  • -----> Step 2 - Creating Field Catalog of the Object

  • cl_alv_table_create

LOOP AT IT_FIELDCAT .

CLEAR WA_FIELDCATALOG .

MOVE-CORRESPONDING IT_FIELDCAT TO WA_FIELDCATALOG .

WA_FIELDCATALOG-REF_FIELD = IT_FIELDCAT-fieldname .

WA_FIELDCATALOG-REF_TABLE = mtable_n .

APPEND WA_FIELDCATALOG TO it_fieldcatALOG .

ENDLOOP .

  • -----> Step 3 - Creating Internal Table Dynamicaly

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcatALOG

IMPORTING

ep_table = MITAB .

ASSIGN MITAB->* TO <FS_ITAB> .

DYNAMIC_IT_INSTANTIATED = 'X' .

ENDIF.

ENDFORM. " INSTANTIATE_DYNAMIC_INTERNAL_T

&----


*& Form SHOW_CONTENTS

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM SHOW_CONTENTS.

CLEAR : <FS_ITAB> .

SELECT * FROM (MTABLE_N)

INTO CORRESPONDING FIELDS OF TABLE <FS_ITAB> .

I_CALLBACK_PROGRAM = SY-REPID .

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = I_CALLBACK_PROGRAM

IT_FIELDCAT = IT_FIELDCAT[]

TABLES

T_OUTTAB = <FS_ITAB>

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2.

ENDFORM. " SHOW_CONTENTS

&----


*& Form CHECK_FILE_TO_UPLOAD

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM CHECK_FILE_TO_UPLOAD.

PERFORM CHECK_FILENAME.

  • CLEAR MSY_SUBRC .

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

FILENAME = MFILENAM

FILETYPE = 'DAT'

TABLES

DATA_TAB = <FS_ITAB>

EXCEPTIONS

CONVERSION_ERROR = 1

FILE_OPEN_ERROR = 2

FILE_READ_ERROR = 3

INVALID_TYPE = 4

NO_BATCH = 5

UNKNOWN_ERROR = 6

INVALID_TABLE_WIDTH = 7

GUI_REFUSE_FILETRANSFER = 8

CUSTOMER_ERROR = 9

OTHERS = 10.

  • MSY_SUBRC = MSY_SUBRC + SY-SUBRC .

IF SY-SUBRC eq 0.

I_CALLBACK_PROGRAM = SY-REPID .

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = I_CALLBACK_PROGRAM

IT_FIELDCAT = IT_FIELDCAT[]

TABLES

T_OUTTAB = <FS_ITAB>

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2.

endif .

ENDFORM. " CHECK_FILE_TO_UPLOAD

Read only

Former Member
0 Likes
923

Hi Sagar

Please check below example:

types: begin of t_dat,
         fld1(10) type c,
         fld2(15) type c,
       end of t_dat.
data: it_dat type table of t_dat,
      wa_dat type t_dat.

data: it_fld1 type table of t_dat-fld1.

We can declare as above. Note that below declaration is wrong:

data: it_fld1 type table of it_dat-fld1. Since IT_DAT is not declared with TYPES...

Hope it helps you.

Kind Regards

Eswar

Read only

Former Member
0 Likes
923

If your requirement is to have few fields in itab refering to database table fields use the syntax

DATA: BEGIN OF ITAB OCCURS 0,

F1 TYPE I,

--<b>

FX LIKE <database table name>-<fieldname>,</b>

END OF ITAB.

or

if u want itab of type database table then use the following syntax

DATA: BEGIN OF ITAB OCCURS 0.

<b>INCLUDE STRUCTURE <database tablename></b>

DATA:END OF ITAB.

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
923

Hi,

If you want to create a internal table of type standard table,

data it_tab type standard table of database_tablename.

If you want to create your own types,then

types : begin of ty,

matnr type mara-matnr,

end of ty.

data itab type standard table of ty.

Read only

Former Member
0 Likes
923

What I'm asking for is ..........

I like to create Internal Table from dictionary table whose name is specified in Table-field.

Thanks & Regards,

Sagar

Read only

0 Likes
923

Write the following code in report program and execute this code.

Then, go to SE38 , enter the report name that u given in the INSERT REPORT command->display.

Here the internal table is created for you.

I/P for this report is the table name that you wants to point to for creating an internal table. i.e we are creating ITAB of structure data dictionary table; whose name is known at runtime.

PARAMETERS: NN(4) TYPE C.

DATA: NNN(30).

DATA: BEGIN OF INCTABL OCCURS 10,

LINE(72),

END OF INCTABL.

INCTABL-LINE = 'REPORT YPROGRAM2.'.

APPEND INCTABL.

DEFINE M.

CONCATENATE 'TABLES:' &1'.' INTO NNN.

INCTABL-LINE = NNN.

APPEND INCTABL.

END-OF-DEFINITION.

M NN.

CLEAR NNN.

INCTABL-LINE = 'DATA: BEGIN OF ITAB OCCURS 0.'.

APPEND INCTABL.

DEFINE MM.

NNN = 'INCLUDE STRUCTURE'.

NNN+19 = &1.

CONCATENATE NNN '.' INTO NNN.

INCTABL-LINE = NNN.

APPEND INCTABL.

END-OF-DEFINITION.

MM NN.

INCTABL-LINE = 'DATA: END OF ITAB.'.

APPEND INCTABL.

LOOP AT INCTABL.

WRITE:/ INCTABL-LINE.

ENDLOOP.

INSERT REPORT 'YPROGRAM2' FROM INCTABL.

Message was edited by:

M B

Read only

0 Likes
923

Check the code that i mentioned.

Read only

Former Member
0 Likes
923

thanx...