‎2007 Jan 26 8:46 AM
Please explain me the diff betweem collect & Describe statement with some example.
‎2007 Jan 26 8:49 AM
*collect will sum up the numeric fields with same non-numeric fields
loop at itab.
collect itab.
endloop.
*Describe can be used many ways
below statement will give the number of records in internal table
describe table itab line v_lines
*this statement will give the properties of field f
describe field f type v_type
‎2007 Jan 26 8:49 AM
<b>DESCRIBE:</b>
DESCRIBE stmt is used to describe the attributes.
DESCRIBE FIELD - Describes the attributes of a field.
DESCRIBE LIST - Describes the attributes of a list.
DESCRIBE TABLE - Describes the attributes of the internal table.
Think of this scenario in dialog programming. While using Table control we need to find the number of lines , so that we can able to position our cursor for the deletion or updation of a record. To get the number of lines we use that Describe stmt.
<b>COLLECT:</b>
COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .
If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.
If, besides its default key fields, the internal table contains number fields,the contents of these number fields are added together if the internal table already contains an entry with the same key fields.
If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.
If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab .
After COLLECT , the system field SY-TABIX contains the index of the - existing or new - table entry with default key fields which match those of the entry to be processed.
COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or compression are unimportant, or two values with identical default key field values could not possibly occur in your particular task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the statement to use.
If you process a table with COLLECT , you should also use COLLECT to fill it. Only by doing this can you guarantee that the internal table will actually be unique or compressed, as described above and COLLECT will run very efficiently.
If you use COLLECT with an explicitly specified work area, it must be compatible with the line type of the internal table.
‎2007 Jan 26 8:56 AM
hi,
COLLECT:
The following special statement allows you to summate entries in an internal table:
COLLECT wa INTO itab.
itab must have a flat line type, and all of the fields that are not part of the table key must have a numeric type (f, i, p). You specify the line wathat you want to add as a work area that is compatible with the line type of itab.
Example is : demo_int_tables_COLLECT
DESCRIBE:
You may sometimes need to find out the attributes of a data object at runtime that were not statically available. For example, you may need to find out the type of a generic interface parameter in a subroutine. To do this, you would use the statement:
DESCRIBE FIELD <f> [LENGTH <l>] [TYPE <t> [COMPONENTS <n>]]
[OUTPUT-LENGTH <o>] [DECIMALS <d>]
[EDIT MASK <m>] [HELP-ID <h>].
example:
DATA: text(8) TYPE c,
len TYPE i.
DESCRIBE FIELD text LENGTH len IN CHARACTER MODE.
Field LEN contains the value 8.
hope it helps.
rgds.
‎2007 Jan 26 9:07 AM
Hi Johnn,
- <b>Collect</b> is used to sum the numeric column of your internal table ( it's used like "APPEND" ) for all lines with the same key.
ex : itab : using append
field1 | 10
field1 | 20
field2 | 15-
using collect :
field1 | 30
field2 | 15- <b>Describe</b> is used to know the number of line in your internal table.
describe table itab lines w_lines.Hope this helps,
Erwan
‎2007 Jan 26 2:26 PM
Hi Sreeni,
I hope John isn't in a hurry, because if he must read your reply...
‎2007 Jan 26 2:01 PM
Hi
Check the following Differences between Colloct & Describe Satements with Examples
COLLECT
Basic form
COLLECT [wa INTO] itab.
Addition
... SORTED BY f
Effect
COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .
If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.
If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields.
If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.
If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab .
After COLLECT , the system field SY-TABIX contains the index of the - existing or new - table entry with default key fields which match those of the entry to be processed.
Notes
COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or compression are unimportant, or two values with identical default key field values could not possibly occur in your particular task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the statement to use.
If you process a table with COLLECT , you should also use COLLECT to fill it. Only by doing this can you guarantee that
the internal table will actually be unique or compressed, as described above and
COLLECT will run very efficiently.
If you use COLLECT with an explicitly specified work area, it must be compatible with the line type of the internal table.
Example
Compressed sales figures for each company
DATA: BEGIN OF COMPANIES OCCURS 10,
NAME(20),
SALES TYPE I,
END OF COMPANIES.
COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 10.
COLLECT COMPANIES.
COMPANIES-NAME = 'Tiger'. COMPANIES-SALES = 20.
COLLECT COMPANIES.
COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 30.
COLLECT COMPANIES.
The table COMPANIES now has the following appearance:
NAME SALES
Duck 40
Tiger 20
Addition
... SORTED BY f
Effect
COLLECT ... SORTED BY f is obsolete and should no longer be used. Use APPEND ... SORTED BY f which has the same meaning.
Note
Performance
The cost of a COLLECT in terms of performance increases with the width of the default key needed in the search for table entries and the number of numeric fields with values which have to be added up, if an entry is found in the internal table to match the default key fields.
If no such entry is found, the cost is reduced to that required to append a new entry to the end of the table.
A COLLECT statement used on a table which is 100 bytes wide and has a key which is 60 bytes wide and seven numeric fields is about approx. 50 msn (standardized microseconds).
Note
Runtime errors
COLLECT_OVERFLOW : Overflow in integer field when calculating totals.
COLLECT_OVERFLOW_TYPE_P : Overflow in type P field when calculating totals.
DESCRIBE - Supply attributes of a field
Basic form
DESCRIBE FIELD f.
Effect
Supplies the attributes of the field f . You must specify at least one of the additions:
Additions
1. ... LENGTH len
2. ... TYPE typ
3. ... TYPE typ COMPONENTS n
4. ... OUTPUT-LENGTH len
5. ... DECIMALS n
6. ... EDIT MASK mask
Addition 1
... LENGTH len
Effect
Returns the length of the field f in the field
len .
Example
DATA: FLD(8),
LEN TYPE P.
DESCRIBE FIELD FLD LENGTH LEN.
Result: LEN contains the value 8.
Addition 2
... TYPE typ
Effect
Returns the data type of f in the field typ
Example
DATA: FLD(8) TYPE N,
F_TYPE.
DESCRIBE FIELD FLD TYPE F_TYPE.
Result: F_TYPE contains the value 'N' .
Note
Along with the elementary data types you can specify under
DATA (C, N, etc.), several other data types are created either
with reference to Dictionary fields or during generation. These data
types, which are also returned by DESCRIBE , have the following
type IDs:
h Internal table s 2-byte integer with leading sign b 1-byte integer without leading sign u Structure without internal table v Structure containing at least one internal table
For compatibility reasons, ... TYPE typ returns C rather than u or v with structures.
Addition 3
... TYPE typ COMPONENTS n
Effect
Similar to ... TYPE typ except that, with structures in typ , u or v are returned and in the number of structure components is set in n . If f is not a structure, n is set to 0.
Example
Recursive processing of the pages of an ABAP/4 data structure:
FORM TEST USING F.
DATA: TYP(1) TYPE C, N TYPE I.
FIELD-SYMBOLS: <F>.
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE F TO <F>.
IF SY-SUBRC <> 0. EXIT. ENDIF.
DESCRIBE FIELD <F> TYPE TYP COMPONENTS N.
IF N > 0. " Equivalent is TYP = 'u' OR TYP = 'v'
PERFORM TEST USING <F>.
ELSE.
PERFORM DO_SOMETHING USING <F>.
ENDIF.
ENDDO.
ENDFORM.
Addition 4
... OUTPUT-LENGTH len
Effect
Enters the output length of the field f in the variable len .
Example
DATA: FLD(4) TYPE P,
O_LEN TYPE P.
DESCRIBE FIELD FLD OUTPUT-LENGTH O_LEN.
Result: O_LEN contains the value 8.
Addition 5
... DECIMALS n
Effect
Enters the number of decimal places for the field f (defined in addition ... DECIMALS of the DATA statement or in the ABAP/4 Dictionary ) in the variable n .
Example
DATA: FLD(8) TYPE P DECIMALS 2,
DEC TYPE P.
DESCRIBE FIELD FLD DECIMALS DEC.
Resultat: DEC contains the value 2.
Addition 6
... EDIT MASK mask
Effect
If the field f has a conversion routine in the ABAP/4 Dictionary , this is placed in the field mask in the form " ==conv ". " conv " stands for the name of the conversion routine, e.g. " ==ALPHA " in the conversion routine " ALPHA ". In this form, mask can then be used in the addition USING EDIT MASK mask of the WRITE statement.
Example
Check whether there is a conversion routine for the field "customer number" in the table SBOOK :
TABLES SBOOK.
DATA: CONV_EXIT(10).
DESCRIBE FIELD SBOOK-CUSTOMID EDIT MASK CONV_EXIT.
IF CONV_EXIT <> SPACE. ... ENDIF.
Result: CONV_EXIT contains the value " ==ALPHA ".
Basic form
DESCRIBE TABLE itab.
Effect
Returns the attributes of the internal table itab . You must use at least one of the additions listed below.
Additions
1. ... LINES lin
2. ... OCCURS n
Addition 1
... LINES lin
Effect
Places the number of filled lines of the table t in the field lin .
Example
DATA: BEGIN OF TAB OCCURS 10,
X,
END OF TAB.
DATA: LIN TYPE P.
...
CLEAR TAB. REFRESH TAB.
MOVE '?' TO TAB-X.
APPEND TAB.
DESCRIBE TABLE TAB LINES LIN.
Result: LIN contains the value 1.
Addition 2
... OCCURS n
Effect
Transfers the size of the OCCURS parameter from the table definition to the variable n .
Example
DATA: BEGIN OF TAB OCCURS 10,
X,
END OF TAB.
OCC TYPE P.
DESCRIBE TABLE TAB OCCURS OCC.
Result: OCC contains the value 10.
Note
If the table is meant to accept more lines than specified by the OCCURS parameter, the parameter value is roughly doubled as long as the table size remains smaller than 8 KB; this table area is held in the roll area. If the table exceeds the maximum permitted size, the OCCURS parameter is not increased and the remaining part of the table is rolled out to the paging area (see DATA ).
For this reason, the OCCURS value determined by the DESCRIBE statement may differ from that in the DATA statement.
The runtime required to execute the DESCRIBE TABLE statement is approx. 4 msn (standardized microseconds).
Basic form
DESCRIBE DISTANCE BETWEEN f1 AND f2 INTO f3.
Effect
Determines the distance between the fields f1 and f2 and places the result (in bytes) in f3 .
Example
Determine the distance between two components of the demo table SBOOK in the flight reservation system:
TABLES SBOOK.
DATA DIST TYPE I.
DESCRIBE DISTANCE BETWEEN SBOOK-CARRID
AND SBOOK-BOOKID
INTO DIST.
Result: DIST contains the value 15 because exactly two fields, SFLIGHT-CONNID (4 bytes) and SBOOK-FLDATE (8 bytes), lie between the SBOOK components CARRID and BOOKID ; also, SBOOK-CARRID is itself 3 bytes long. The sum of these values gives the distance between the two components in bytes.
Variants
1. DESCRIBE LIST NUMBER OF LINES lin.
2. DESCRIBE LIST NUMBER OF PAGES n.
3. DESCRIBE LIST LINE lin PAGE pag.
4. DESCRIBE LIST PAGE pag.
Effect
Returns the attributes of a list. All variants have the addition ... INDEX idx allowing you to determine the attributes of a particular list level ( SY-LSIND = 0,1,2,3,... ).
Note
You should use this key word only in exceptional cases (e.g. when editing an 'anonymous' list in a program other than that which generated the list). In all other cases, you should save the relevant values when you generate the list.
Take care when attempting to retrieve the list attributes being set up ( ...INDEX SY-LSIND ), since some attributes (number of pages, number of lines, ...) may not have been updated yet.
Variant 1
DESCRIBE LIST NUMBER OF LINES lin.
Addition
... INDEX idx
Effect
Returns the number of lines in the list.
The return code value is set as follows:
SY-SUBRC = 0 OK
SY-SUBRC <> 0 List does not exist (only with the addition INDEX )
Addition
... INDEX idx
Effect
Returns the attributes of the list level idx (0, 1,2,3,...).
Example
After line selection, determine the number of lines in the displayed list:
DATA: LN LIKE SY-PAGNO. ...
AT LINE-SELECTION.
DESCRIBE LIST NUMBER OF LINES LN.
The variable LN now contains the number of lines in the displayed list.
Variant 2
DESCRIBE LIST NUMBER OF PAGES n.
Addition
... INDEX idx
Effect
Returns the number of pages in the list.
The return code value is set as follows:
SY-SUBRC = 0 OK
SY-SUBRC <> 0 List does not exist (only with the addition INDEX )
Addition
... INDEX idx
Effect
Returns the attributes of the list level idx (0, 1,2,3,...).
Example
After line selection, determine the number of pages in the displayed list:
DATA: PN LIKE SY-PAGNO. ...
AT LINE-SELECTION.
DESCRIBE LIST NUMBER OF PAGES PN.
The variable PN now contains the number of pages in the displayed list (i.e. the contents of the system field SY-PAGNO after the list has been generated!).
Variant 3
DESCRIBE LIST LINE lin PAGE pag.
Addition
... INDEX idx
Effect
Returns the number of the page for the line lin in the list.
Note
In interactive reporting, line selection causes a value to be assigned to the system field SY-LILLI (absolute number of selected list line). The system field SY-CPAGE contains the page number for the first displayed line in the list. The selected line does not have to belong to this page (in cases where several pages are displayed at the same time). The page number may be of interest even with direct reading of lines (see READ LINE ).
The return code value is set as follows:
SY-SUBRC = 0 OK
SY_SUBRC = 4 Line does not exist
SY-SUBRC = 8 List does not exist
Addition
... INDEX idx
Effect
Returns the attributes of the list level idx (0, 1,2,3,...).
Example
After line selection, determine the page number for the selected line (SY-LILLI) :
DATA: PAGENUMBER LIKE SY-PAGNO. ...
AT LINE-SELECTION.
DESCRIBE LIST LINE SY-LILLI PAGE PAGENUMBER.
The variable PAGENUMBER now contains the page number for the line SY-LILLI (i.e. the contents of the system field SY-PAGNO when outputting the line SY-LILLI !).
Variant 4
DESCRIBE LIST PAGE pag
Additions
1. ... INDEX idx
2. ... LINE-SIZE col
3. ... LINE-COUNT lin
4. ... LINES lin
5. ... FIRST-LINE lin
6. ... TOP-LINES lin
7. ... TITLE-LINES lin
8. ... HEAD-LINES lin
9. ... END-LINES lin
Effect
Returns the attributes of the page pag in the list.
The return code value is set as follows:
SY-SUBRC = 0 OK
SY_SUBRC = 4 Page does not exist
SY-SUBRC = 8 List does not exist
Addition 1
... INDEX idx
Effect
Returns the attributes of the list level idx (0, 1,2,3,...).
Addition 2
... LINE-SIZE col
Effect
Returns the line length for the page pag (see NEW-PAGE...LINE-SIZE ).
Addition 3
... LINE-COUNT lin
Effect
Returns the permitted number of lines for the page pag (see NEW-PAGE...LINE-COUNT ).
Addition 4
... LINES lin
Effect
Returns the number of lines output on the page pag .
Addition 5
... FIRST-LINE lin
Effect
Returns the absolute line number of the first line of the page pag .
Addition 6
... TOP-LINES lin
Effect
Returns the number of lines output by page header processing (i.e. standard title + column headers + TOP-OF-PAGE ).
Addition 7
... TITLE-LINES lin
Effect
Returns the number of lines output as standard title lines by page header processing (see NEW-PAGE...NO-TITLE/WITH-TITLE ).
Note
The value of TITLE-LINES is contained in TOP-LINES .
Addition 8
... HEAD-LINES lin
Effect
Returns the number of lines output as column headers by page header processing (see NEW-PAGE...NO-HEADING/WITH-HEADING ).
Note
The value of HEAD-LINES is contained in TOP-LINES .
Addition 9
... END-LINES lin
Effect
Returns the number of lines reserved for end-of-page processing (see END-OF-PAGE ).
Example
Determine the number of lines output on the third page of the basic list ( SY-LSIND = 0) in the event TOP-OF-PAGE :
DATA: TOP TYPE I,
HEAD TYPE I,
TITLE TYPE I,
REAL_TOP TYPE I.
DESCRIBE LIST INDEX 0 PAGE 3
TOP-LINES TOP
HEAD-LINES HEAD
TITLE-LINES TITLE.
REAL_TOP = TOP - TITLE - HEAD.
Examples
Determine the absolute number of lines in the displayed list:
DATA: LN TYPE I, "number of lines on a page
FLN TYPE I, "number of first line on a page
PN TYPE I, "number of a page
LIST_LINES TYPE I. "total number of lines in list
Determine number of last page:
DESCRIBE LIST NUMBER OF PAGES PN.
Determine number of first line of last page and number of lines on that page:
DESCRIBE LIST PAGE PN FIRST-LINE FLN LINES LN.
Number of list lines = number of first line of last page + number of lines - 1.
LIST_LINES = FLN + LN - 1.
Or: Count lines of all pages in a loop:
CLEAR LIST_LINES.
DO PN TIMES.
DESCRIBE LIST PAGE SY-INDEX LINES LN.
ADD LN TO LIST_LINES.
ENDDO.
or:
DESCRIBE LIST NUMBER OF LINES LIST_LINES.
Regards
Sreeni