Inline Declarations in SAP-ABAP


Inline Declarations
 
  • In SAP NetWeaver 7.40, SAP introduced inline declarations.
  • Inline declarations are allowing us to declare variable, workarea, internal tables inline as and when required.
  • Hence, no additional data declarations are required.
 
1. Variables
 
*&Assigning Values to Variables
 
Example Code:
 
REPORT ZINLINE_DECLARATIONS.
 
DATA(LV_BOOK) = 'welcome to Inline declarations'.
 
WRITE LV_BOOK.
 
Output:
 
 
2. Internal Table & Work area

Custom Table used to illustrate the below scenarios.


Table Contents:

*&Assigning data into internal tables and Work areas

Example Code:
 
*& Data retrieval from Data base table to Internal Table
 

REPORT ZINLINE_DECLARATIONS.

SELECT FROM YABAPT0001
   
INTO TABLE @DATA(I_DATA).           "Inline Internal table Declaration
*
FORMAT COLOR 2.
WRITE:/       'YCUSTID',
      
15 '|''YCUSTNAME',
      
40 '|''YCITY'   .
ULINE.
*
*& Processing Logic

LOOP AT I_DATA INTO DATA(W_DATA).       "Inline Workarea Declaration

    
WRITE:/ W_DATA-YCUSTID,
       
15 '|'W_DATA-YCUSTNAME,
        
40 '|'W_DATA-YCITY .
ENDLOOP. 

  Output:
 

3. Read Table
 
a. Read Table using Index
 
b. Read Table using Free Key
 
*& Program to use Read Statement using Inline Statements
 
*& a. Read Table using Table Index
 
*& Example Code
  REPORT ZINLINE_DECLARATIONS.
 
SELECT * FROM YABAPT0001
 INTO TABLE @DATA(I_DATA).           "Inline Internal table Declaration

FORMAT COLOR 2.

WRITE:/       'YCUSTID',
          15 '|', 'YCUSTNAME',
          40 '|', 'YCITY'   . 
 
DATA(W_DATA) =  I_DATA[ 1 ].

IF SY-SUBRC = 0.
  WRITE:/ W_DATA-YCUSTID,
            15 '|', W_DATA-YCUSTNAME,
             40 '|', W_DATA-YCITY .
ENDIF. 

Output:
  

 
*& b. Read Table using Free Key
 
Note: Use above code and replace reads statement with below read statement
 
REPORT ZINLINE_DECLARATIONS.
 
SELECT * FROM YABAPT0001
   INTO TABLE @DATA(I_DATA).           "Inline Internal table Declaration

FORMAT COLOR 2.
WRITE:/       'YCUSTID',
      15 '|', 'YCUSTNAME',
      40 '|', 'YCITY'   . 

DATA(W_DATA) =  I_DATA[ YCUSTID = '1001' ].

IF SY-SUBRC = 0.
  WRITE:/ W_DATA-YCUSTID,
       15 '|', W_DATA-YCUSTNAME,
       40 '|', W_DATA-YCITY.
ENDIF.
 
Output:


  
4. Lines (We can use instead of DESCRIBE)
 
*& Inline declaration to know the no of lines in internal table using 'Lines'
 
Example Code:
 
REPORT ZINLINE_DECLARATIONS.
 
SELECT * FROM YABAPT0001
   INTO TABLE @DATA(I_DATA).  
 
*& Lines - To know the no of lines of Internal Table
 
FORMAT COLOR 5.

DATA(LV_LINES) = LINES( I_DATA ).
IF LV_LINES IS NOT INITIAL.
  WRITE:/ 'No of lines in internal table:', LV_LINES.
ENDIF.
Output:
 

 
 
5. Modify
 
  1. Modify table row contents based on INDEX
  2. Modify the contents based on FREE-KEY

a. Modify table row contents based on INDEX
 
Requirement: Modify the YCUSTNAME of record 2 as 'Ram' from 'customer1'.
 Example Code:
REPORT ZINLINE_DECLARATIONS.
 
SELECT * FROM YABAPT0001
INTO TABLE @DATA(I_DATA).           
a. Modify table row contents based on INDEX
TRY.
    I_DATA[ 2 ]-ycustNAME = 'Ram'.
  CATCH CX_SY_ITAB_LINE_NOT_FOUND.
ENDTRY. 

*& Processing Logic
LOOP AT I_DATA INTO data(w_data).
  WRITE:/ W_DATA-ycustID,
       15 '|', W_DATA-ycustNAME,
       40 '|', W_DATA-ycity,'|'.
ENDLOOP.

Output:
 
 
b. Modify table row contents based on FREE-KEY
 

Requirement: Modify the customer Name of record 2 as 'seetha' from 'customer1' using Key.

Example Code:
REPORT ZINLINE_DECLARATIONS.
  
SELECT * FROM YABAPT0001
INTO TABLE @DATA(I_DATA).           

* b. Modify the contents based on FREE-KEY
TRY.
I_DATA[ YCUSTID = '1002' ]-YCUSTNAME = 'Seetha'.
 CATCH CX_SY_ITAB_LINE_NOT_FOUND.
ENDTRY.

*& Processing Logic

LOOP AT I_DATA INTO DATA(W_DATA).
  WRITE:/ W_DATA-YCUSTID,
       15 '|', W_DATA-YCUSTNAME,
       40 '|', W_DATA-YCITY.
ENDLOOP. 
Output:
 

 
 

Comments

  1. this is very useful content and we can easily get to know concept...thank you naga ..thanks alot....keep going ..

    ReplyDelete

Post a Comment

Popular posts from this blog

SAP ABAP-Internal Table Operations and Example Program

HR ABAP Interview Questions and Answers

BDC (Batch Data Communication) - SAP ABAP