Copying Internal Tables in ABAP

gram77
gram77 used Ask the Experts™
on
*&---------------------------------------------------------------------*
*& Report  ZSUBROUTINE_TABLES
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsubroutine_tables.
TYPES : BEGIN OF line_type,
          eno(3) TYPE n,
          ename(30) TYPE c,
          esal TYPE i,
        END OF line_type.

DATA itab TYPE line_type OCCURS 10 WITH HEADER LINE.
DATA jtab TYPE STANDARD TABLE OF line_type.
"DATA jtab TYPE line_type OCCURS 10 WITH HEADER LINE.

PERFORM fill TABLES itab.
jtab = itab[].
PERFORM output TABLES jtab.


*&---------------------------------------------------------------------*
*&      Form  fill
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_ITAB     text
*----------------------------------------------------------------------*
FORM fill TABLES p_itab LIKE itab[].
  p_itab-eno = '14'.
  p_itab-ename = 'Aman'.
  p_itab-esal = 3000.
  APPEND p_itab.

  p_itab-eno = '142'.
  p_itab-ename = 'Raman'.
  p_itab-esal = 5000.
  APPEND p_itab.
ENDFORM.                    "fill

*&---------------------------------------------------------------------*
*&      Form  output
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_JTAB     text
*----------------------------------------------------------------------*
FORM output TABLES p_jtab LIKE jtab[].
  LOOP AT p_jtab.
    WRITE : / p_jtab-eno, p_jtab-ename, p_jtab-esal.
  ENDLOOP.
ENDFORM.                    "output

The code in bold if uncommented raises error, why.
In the commented code, Both itab and jtab are declared in a similar way.
Error: The type of "ITAB" cannot be converted to the type of "JTAB".
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
SAP Technical Consultant
Commented:
Dear gram77

If you want to declare variable jtab as below

DATA jtab TYPE line_type OCCURS 10 WITH HEADER LINE.

Open in new window


than the assignment statement
jtab = itab[].

Open in new window

will need to be changed to
jtab[] = itab[].

Open in new window


This is due to the HEADER LINE clause in the Data declaration clause for jtab.

According to SAP help



... WITH HEADER LINE ...



Effect

This addition to the statements DATA TABLE OF, DATA RANGE OF, and the obsolete statement DATA OCCURS (which is not allowed in classes) declares a further data object alongside the internal table, known as the header line. This object has exactly the same name as the internal table and has the row type of the internal table as its data type. A header line cannot be declared for internal tables with a table-like row type. This is possible for structured row types with table-like components, however.

If you specify the name of an internal table itab in an operand position of an ABAP statement, the statement determines whether the table body or header line is used. As a rule, all table-specific statements such as SORT or LOOP use the internal table, whereas all other statements use the header line. Exceptions to this rule are listed under Internal Tables with Header Line.

To address the table body instead of the header line in statements, you can append [] to the table name:

... itab[] ...

For internal tables without a header line, the table body is always used. An internal table with a header line cannot be a component of a structure or a row of another internal table.



Thanks and Regards
onn

Author

Commented:
Excellent.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial