Sunday 24 August 2014

COLLECT STATEMENT IN SAP ABAP


In this SAP ABAP tutorial I will show you the use COLLECT STATEMENT in SAP ABAP
Collect statement is used to summarize the data of the internal table.
 SYNTAX : collect <work_area> into <internal_table>. 

Some points before using collect statement:
1.     Before using collect statement , all the fields of an internal table      that are not part of the key of the table should be of numeric type ( f, p, i).
2.     When we use collect statement          to add a new line or record in an internal table, the SAP system checks whether the line with the same key value already exist in the table. IF no such line exists, then collect will act as a normal append.
However, if the line having the same key value is found in the internal table, the collect statement will adds the content of the numeric field in the work area with the contents of the numeric fields in the existing entry of the table.


Program :       

REPORT  ztest_r_05.

TYPES BEGIN OF ty_shop,
          item 
TYPE char10,
          quantity 
TYPE i,
          amount   
TYPE i,
        
END OF   ty_shop.

DATA lt_shop TYPE STANDARD TABLE OF ty_shop INITIAL SIZE 1,
       ls_shop 
TYPE ty_shop,
       lt_shop_summ 
LIKE lt_shop.

FIELD-SYMBOLS <fs_shop> TYPE ty_shop.

DO TIMES.

  
CASE sy-index.

    
WHEN 1.

      ls_shop
-item 'SOAP'.
      ls_shop
-quantity 1.
      ls_shop
-amount   50.

      
APPEND ls_shop TO lt_shop.
      
CLEAR ls_shop.

    
WHEN 2.
      ls_shop
-item 'SAMPOO'.
      ls_shop
-quantity 1.
      ls_shop
-amount   120.

      
APPEND ls_shop TO lt_shop.
      
CLEAR ls_shop.

    
WHEN 3.

      ls_shop
-item 'FACE_WASH'.
      ls_shop
-quantity 1.
      ls_shop
-amount   100.

      
APPEND ls_shop TO lt_shop.
      
CLEAR ls_shop.

    
WHEN 4.
      ls_shop
-item 'SOAP'.
      ls_shop
-quantity 1.
      ls_shop
-amount   70.

      
APPEND ls_shop TO lt_shop.
      
CLEAR ls_shop.

    
WHEN 5.
      ls_shop
-item 'SAMPOO'.
      ls_shop
-quantity 1.
      ls_shop
-amount   200.

      
APPEND ls_shop TO lt_shop.
      
CLEAR ls_shop.

    
WHEN 6.

      ls_shop
-item 'FACE_WASH'.
      ls_shop
-quantity 1.
      ls_shop
-amount   240.

      
APPEND ls_shop TO lt_shop.
      
CLEAR ls_shop.

    
WHEN OTHERS.

  
ENDCASE.

ENDDO.

LOOP AT lt_shop ASSIGNING <fs_shop>.

  
WRITE / <fs_shop>-item <fs_shop>-quantity<fs_shop>-amount.

ENDLOOP.

ULINE.

WRITE 'Summarized records using Collect statement'.

LOOP AT lt_shop INTO ls_shop.

  
COLLECT ls_shop INTO lt_shop_summ.

ENDLOOP.

LOOP AT lt_shop_summ INTO ls_shop.

  
WRITE / <fs_shop>-item <fs_shop>-quantity<fs_shop>-amount.

ENDLOOP
.




Monday 18 August 2014

USE OF CLEAR , REFRESH, FREE IN SAP ABAP

In this SAP ABAP tutorial I will show you the use of clear, refresh and free .

Clear:
Syntax : clear <variable_name>.
Use : clear the contents of the variable.
Variables may be internal table , work area , variables declared using elementary data types etc. 
E.g.:
Program 1.
DATA lv_num TYPE i VALUE 1000.

WRITE 'Value before clear : 'lv_num.

CLEAR lv_num.

WRITE 'Value after clear : ',lv_num.

Program 2.
TYPES BEGIN OF ty_mara,
          matnr 
TYPE matnr," Material Number
          mtart 
TYPE mtart," Material Type
          matkl 
TYPE matkl," Material Group
        
END OF   ty_mara.

DATA lt_mara TYPE STANDARD TABLE OF ty_mara INITIAL SIZE 1" Internal table declaration

DATA lt_mara_header TYPE STANDARD TABLE OF ty_mara WITH HEADER LINE" Internal table with header line

DATA ls_mara TYPE ty_mara." Work area declaration

CLEAR ls_mara lt_mara[]lt_mara, lt_mara_header[] lt_mara_header.

REFRESH :
It is used to clear the contents of only internal table. It will only clear the body of the internal table.
TYPES BEGIN OF ty_mara,
          matnr 
TYPE matnr," Material Number
          mtart 
TYPE mtart," Material Type
          matkl 
TYPE matkl," Material Group
        
END OF   ty_mara.
*-- Internal table declaration
DATA lt_mara TYPE STANDARD TABLE OF ty_mara INITIAL SIZE 1.
*-- Internal table with header line
DATA lt_mara_header TYPE STANDARD TABLE OF ty_mara WITH HEADER LINE
*--  Work area declaration
DATA ls_mara TYPE ty_mara.
*-- Use of refresh
REFRESH lt_mara[]lt_mara lt_mara_header[] .

N.B.: Both clear and refresh are used to clear the contents but they never release the memory space.

FREE :
It is used to clear the contents and release the memory of internal table only.
E.g.:
TYPES BEGIN OF ty_mara,
          matnr 
TYPE matnr," Material Number
          mtart 
TYPE mtart," Material Type
          matkl 
TYPE matkl," Material Group
        
END OF   ty_mara.
*-- Internal table declaration
DATA lt_mara TYPE STANDARD TABLE OF ty_mara INITIAL SIZE 1.
*-- Internal table with header line
DATA lt_mara_header TYPE STANDARD TABLE OF ty_mara WITH HEADER LINE.
*--  Work area declaration
DATA ls_mara TYPE ty_mara.
*-- Use of free
FREE lt_mara[]lt_mara_header[] .

Thanks a lot again friends for reading this tutorial. If you find this is useful and helpful to you, please consider it sharing to your friends using the social buttons below. 
Thank you and I invite everyone to connect with me on my facebook page ABAP TOPICS or follow me on goole plus. You can subscribe also to keep updated with latest posts. Enjoy reading this blog .
Please mail all your contributions to roushan.c.kumar@gmail.com . We request you to mention your Name & Organization you are working for. Your posts will be verified and posted in this site with your name.





Internal Table in SAP ABAP


Internal Table:





In real time different modules e.g. MM , FI , CO , SD etc. hits the database to update the values so that they want to see the values in particular way such as for SD – Sales related details, for FI – Financial related details etc. that may change the database value.



So, instead of hitting the database and changing the values at the database level SAP come up with a concept known as internal table.

Internal table is a variable which is used to extract the records from the database table and process the records. Internal table is like temporary table.

So, now different departments can make a copy of the database table , process them according to their requirements by using internal table without changing the database table.

Properties of internal table:

1. The data that is copied from database table is copied record by record and in the same sequence. So accessing data from internal table will also be done record by record and in the same sequence.

2. Internal table is a collection of group of structures and structure is a group of fields.

3. Internal table stores the records, manipulate and format the records according to user requirement.

4. Memory allocation of internal table is done at runtime as internal tables can store any number of records (may be 1 million or 1 billion).

5. The line type of an internal table can be any ABAP data types – elementary, structured or internal tables.

Steps to declare internal table:

1.Declare the structure of the internal table.

TYPES BEGIN OF ty_mara_t,
         matnr 
TYPE mtart,
         maktx 
TYPE maktx,
         matkl 
TYPE matkl,
        
END OF   ty_mara_t.

DATA BEGIN OF ls_mara_d,
         matnr 
TYPE mtart,
         maktx 
TYPE maktx,
         matkl 
TYPE matkl,
      
END OF    ls_mara_d.

DATA ls_mara_3 TYPE ty_mara_t.

DATA ls_mara_4 LIKE ls_mara_d.

Types – it is used to define user defined data types.
Data   - it is used to defined user defined data objects. 

2.Declare internal table using the structure.

DATA lt_mara_1 TYPE TABLE OF ty_mara_t,
       lt_mara_2 
LIKE TABLE OF ls_mara_d,
       lt_mara_3 
LIKE TABLE OF ls_mara_3,
       lt_mara_4 
LIKE TABLE OF ls_mara_4.

Thanks a lot again friends for reading this tutorial. If you find this is useful and helpful to you, please consider it sharing to your friends using the social buttons below.