Sap internal

602 views
560 views

Published on

Published in: Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
602
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
44
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sap internal

  1. 1. REPORT ZTYPES .* Table declaration (old method)DATA: BEGIN OF tab_ekpo OCCURS 0, "itab with header lineebeln TYPE ekpo-ebeln,ebelp TYPE ekpo-ebelp, END OF tab_ekpo.*Table declaration (new method) "USE THIS WAY!!!TYPES: BEGIN OF t_ekpo,ebeln TYPE ekpo-ebeln,ebelp TYPE ekpo-ebelp, END OF t_ekpo.DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0, "itabwa_ekpo TYPE t_ekpo. "work area (header line)* Build internal table and work area from existing internal tableDATA: it_datatab LIKE tab_ekpo OCCURS 0, "old methodwa_datatab LIKE LINE OF tab_ekpo.* Build internal table and work area from existing internal table,* adding additional fieldsTYPES: BEGIN OF t_repdata.INCLUDE STRUCTURE tab_ekpo. "could include EKKO table itself!!TYPES: bukrs TYPEekpo-werks,bstyp TYPEekpo-bukrs.TYPES: END OF t_repdata.DATA: it_repdata TYPE STANDARD TABLE OF t_repdata INITIAL SIZE 0, "itabwa_repdata TYPE t_repdata. "work area (header line)Appending Table LinesThere are several ways of adding lines to index tables. The following statements have no equivalent thatapplies to all internal tables.Appending a Single LineTo add a line to an index table, use the statement:APPEND line TO itab.line is either a work area wa that is convertible to the line type, or the expression INITIAL LINE. Ifyou use wa, the system adds a new line to the internal table itab and fills it with the contents of the workarea. INITIAL LINE appends a blank line containing the correct initial value for each field of thestructure. After each APPEND statement, the system field sy-tabix contains the index of the appendedline.Appending lines to standard tables and sorted tables with a non-unique key works regardless of whetherlines with the same key already exist in the table. Duplicate entries may occur. A runtime error occurs ifyou attempt to add a duplicate entry to a sorted table with a unique key. Equally, a runtime error occurs ifyou violate the sort order of a sorted table by appending to it.
  2. 2. Appending Several LinesYou can also append internal tables to index tables using the following statement:APPEND LINES OF itab1 TO itab2.This statement appends the whole of itab1 to itab2. itab1can be any type of table. The line typeof itab1 must be convertible into the line type of itab2.When you append an index table to another index table, you can specify the lines to be appended asfollows:APPEND LINES OF itab1 [FROM n1] [TO n2] TO itab2.n1 and n2 specify the indexes of the first and last lines of itab1 that you want to append to itab2.This method of appending lines of one table to another is about 3 to 4 times faster than appending themline by line in a loop. After the APPEND statement, the system field sy-tabix contains the index of thelast line appended. When you append several lines to a sorted table, you must respect the unique key (ifdefined), and not violate the sort order. Otherwise, a runtime error will occur.Ranked ListsYou can use the APPEND statement to create ranked lists in standard tables. To do this, create an emptytable, and then use the statement:APPEND wa TO itab SORTED BY f.The new line is not added to the end of the internal table itab. Instead, the table is sorted byfield f in descending order. The work area wamust be compatible with the line type of the internal table.You cannot use the SORTED BY addition with sorted tables.When you use this technique, the internal table may only contain as many entries as you specified inthe INITIAL SIZE parameter of the table declaration. This is an exception to the general rule, whereinternal tables can be extended dynamically. If you add more lines than specified, the last line isdiscarded. This is useful for creating ranked lists of limited length (for example "Top Ten"). You can usethe APPEND statement to generate ranked lists containing up to 100 entries. When dealing with largerlists, it is advisable to sort tables normally for performance reasons. DATA: BEGIN OF wa, col1(1) TYPE c, col2 TYPE i, END OF wa. DATA itab LIKE TABLE OF wa. DO 3 TIMES. APPEND INITIAL LINE TO itab. wa-col1 = sy-index. wa-col2 = sy-index ** 2. APPEND wa TO itab. ENDDO. LOOP AT itab INTO wa. WRITE: / wa-col1, wa-col2. ENDLOOP. The list output is: 0 1 1 0 2 4 0 3 9
  3. 3. This example creates an internal table itab with two columns that is filled in the DO loop.Each time the processing passes through the loop, an initialized line is appended and thenthe table work area is filled with the loop index and the square root of the loop index andappended.DATA: BEGIN OF line1, col1(3) TYPE c, col2(2) TYPE n, col3 TYPE i, END OF line1, tab1 LIKE TABLE OF line1.DATA: BEGIN OF line2, field1(1) TYPE c, field2 LIKE tab1, END OF line2, tab2 LIKE TABLE OF line2.line1-col1 = abc. line1-col2 = 12. line1-col3 = 3.APPEND line1 TO tab1.line1-col1 = def. line1-col2 = 34. line1-col3 = 5.APPEND line1 TO tab1.line2-field1 = A. line2-field2 = tab1.APPEND line2 TO tab2.REFRESH tab1.line1-col1 = ghi. line1-col2 = 56. line1-col3 = 7.APPEND line1 TO tab1.line1-col1 = jkl. line1-col2 = 78. line1-col3 = 9.APPEND line1 TO tab1.line2-field1 = B. line2-field2 = tab1.APPEND line2 TO tab2.LOOP AT tab2 INTO line2. WRITE: / line2-field1. LOOP AT line2-field2 INTO line1. WRITE: / line1-col1, line1-col2, line1-col3. ENDLOOP.ENDLOOP.The list output is:Aabc 12 3def 34 5Bghi 56 7jkl 78 9The example creates two internal tables tab1 and tab2. tab2 has a deep structurebecause the second component of line2 has the data type of internal table tab1. line1 isfilled and appended totab1. Then, line2 is filled and appended to tab2. Afterclearing tab1 with the REFRESH statement, the same procedure is repeated.DATA: BEGIN OF line, col1(1) TYPE c, col2 TYPE i, END OF line.
  4. 4. DATA: itab1 LIKE TABLE OF line, jtab LIKE itab.DO 3 TIMES. line-col1 = sy-index. line-col2 = sy-index ** 2. APPEND line TO itab1. line-col1 = sy-index. line-col2 = sy-index ** 3. APPEND line TO jtab.ENDDO.APPEND LINES OF jtab FROM 2 TO 3 TO itab1.LOOP AT itab1 INTO line. WRITE: / line-col1, line-col2.ENDLOOP.The list output is:1 12 43 92 83 27This example creates two internal tables of the same type, itab and jtab. Inthe DO loop, itab is filled with a list of square numbers, and jtab with a list of cubenumbers. Then, the last two lines ofjtab are appended to itab.DATA: BEGIN OF line3, col1 TYPE i, col2 TYPE i, col3 TYPE i, END OF line3.DATA itab2 LIKE TABLE OF line3 INITIAL SIZE 2.line3-col1 = 1. line3-col2 = 2. line3-col3 = 3.APPEND line3 TO itab2 SORTED BY col2.line3-col1 = 4. line3-col2 = 5. line3-col3 = 6.APPEND line3 TO itab2 SORTED BY col2.line3-col1 = 7. line3-col2 = 8. line3-col3 = 9.APPEND line3 TO itab2 SORTED BY col2.LOOP AT itab2 INTO line3. WRITE: / line3-col2.ENDLOOP.The list output is: 8 5The program inserts three lines into the internal table itab using the APPEND statementand the SORTED BY addition. The line with the smallest value for the field COL2 is deletedfrom the table, since the number of lines that can be appended is fixed to 2 through theINITIAL SIZE addition.

×