Tuesday, June 9, 2009

Interactive Reports

About interactive report

A classical report consists of one program that creates a single list. This means that when the list is displayed, it has to contain all the requested data, regardless of the number of details the user want to see. This procedure may result in extensive and cluttered lists from which the user has to pick the relevant data. The desired selections must be made before hand and the report must provide detailed information.

This is not possible using the classical report and for this ABAP/4 has provided reporting feature called INTERACTIVE REPORT. The list produced by classical report doesn’t allow user to interact with the system but the list produced by interactive report allows the user to interact with the system i.e., user can tell the system, that he needs further information. Depending upon what the user tells the system, the action is taken. Interactive reporting thus reduces information retrieval to the data actually required.

Interactive reporting allows the user to participate in retrieving and presenting data at each level during the session. Instead of presenting one extensive and detailed list with cluttered information, with interactive reporting you can create a condensed basic list from which the user can call detailed information by positioning the cursor and entering commands.

Detailed information is presented in secondary lists. A secondary list may either overlay the basic list completely or appear in an additional dialog window on the same screen. The secondary list can itself be interactive again. The basic list is not deleted when secondary list is created.



User can interact with the system by:


¨      Double clicking or pressing F2

¨      Selecting menu option


 Like classical report, the interactive report is also event driven. Both the action mentioned above trigger events and code is written to handle these events.  The events triggered by this action are as follows:


¨      At line-selection

¨      At user-command

¨      Top-of-Page During Line-Selection for Secondary Page Header info
 
 
 
Interactive report consists of one BASIC list and 20 secondary list. Basic list is produced by START-OF-SELECTION event. When the user double clicks on the basic list or chooses the menu option, the secondary list is produced. All the events associated with classical report except end-of-page are applicable only to basic list.
 
 

AT LINE-SELECTION event

 
 
 Double clicking is the way most users navigate through programs. Double clicking on basic list or any secondary list triggers the event AT LINE-SELECTION. SY-LSIND denotes the index of the list currently created. For BASIC list it is always 0.  Following piece of code shows how to handle the event.
 
 
Start-of-selection.
 
 
Write: / ‘this is basic list’.
At line-selection.
Write : ‘this is first secondary list’.
In this case the output will be displayed on basic list i.e.
This is basic list.
When user double clicks on this line, the event at line-selection gets triggered and secondary list is produced, i.e.
 
 
This is first secondary list.
 
 
You can go back to basic list by clicking on F3 or back icon on the standard tool bar.  For this list, the value of sy-lsind will be 1.

 

HIDE technique


 
In this case thins are much simpler. Consider the case, wherein you display fields from table sflight in basic list. When user double clicks on any sflight-carrid, you are displaying the detailed information related to that particular carrid on secondary list.  Hence there is a need to store the clicked carrid in some variable.  So that you can access this carrid for next list. ABAP/4 has facility; a statement called HIDE, which provides the above functionality.

 
HIDE command temporarily stores the content of clicked field in system area.

Syntax:
 
 
HIDE .
 
 This statement stores the contents of variable in relation to the current output line (system field SY-LINNO) internally in the so-called HIDE area. The variable must not necessarily appear on the current line.
 
 
 You have to place the HIDE statement always directly after the output statement i.e., WRITES for the variable .  As when you hide the variable, control is passed to next record.  While writing, WRITE statement takes that record from header and writes it on to the list, but when writing onto your interactive list you will miss out 1st record.
 
 
 To hide several variables, use chain HIDE statement.
 
 
 As soon as the user selects a line for which you stored HIDE fields, the system fills the variables in the program with the values stored.  A line can be selected.
 ¨      By an interactive event.
 For each interactive event, the HIDE fields of the line on which the cursor is positioned during the event are filled with the stored values.
The HIDE area is a table, in which the system stores the names and values of all HIDE fields for each list and line number.  As soon as they are needed, the system reads the values from the table.  (Please try to find the name of this table.)
 
Sy-lsind indicates the index of the list and can be used to handle all the secondary lists.  When the user double clicks on the line or presses F2, sy-lsind is increased by one and this new sy-lsind can be handled.  For example:
Write: / ‘this is basic list’.
·         Will create a basic list.
If sy-lsind = 1.
     Write: / ‘this is first secondary list’.
Elseif sy-lsind = 2.
      Write: / ‘This is second secondary list’.
Endif.
 
 
 When this code is executed,

·         Basic list is produced.
·         When the user clicks on the basic list, sy-lsind becomes one.
·         AT LINE-SELECTION event is triggered.
·         Whatever is written under IF Sy-lsind = 1, gets executed.
·         Secondary list is produced.
·         Again if user clicks on this list, sy-lsind becomes two.
·         AT LINE-SELECTION gets triggered.
·         Code written under IF Sy-lsind = 2, gets executed.
 
 
Example for Interactive Report Events
*&---------------------------------------------------------------------*
*& Report  ZBASU_INTERACTIVE_REPORT_EVENT                              *
*&---------------------------------------------------------------------*
REPORT  ZBASU_INTERACTIVE_REPORT_EVENT NO STANDARD PAGE HEADING
        LINE-SIZE 90  LINE-COUNT 40(2).
Tables: mara, kna1, vbak, vbap.
types:begin of ty_kna1,
      kunnr like kna1-kunnr,
      name1 like kna1-name1,
      end of ty_kna1.
types:begin of ty_vbln,
      vbeln  like vbak-vbeln,
      netwr like vbak-netwr,
      end of ty_vbln.
types:begin of ty_vbap,
      posnr  like vbap-posnr,
      matnr like mara-matnr,
      end of ty_vbap.
data: it_kna1 type standard table of ty_kna1,
      wa_kna1 like line of it_kna1.
data: it_vbln type standard table of ty_vbln,
      wa_vbln like line of it_vbln.
data: it_vbap type standard table of ty_vbap,
      wa_vbap like line of it_vbap.
 
START-OF-SELECTION.
 
select kunnr name1 from kna1 into  corresponding fields of table  it_kna1.
loop at it_kna1 into wa_kna1.
   write:/ wa_kna1-kunnr,
           wa_kna1-name1.
   hide: wa_kna1-kunnr.
endloop.
 
TOP-OF-PAGE.
WRITE:/ 'GALAXE SOLUTIONS'.
At line-selection.
 
case sy-lsind.
    when '1'.
   select vbeln netwr from vbak into corresponding fields of table  it_vbln.
loop at it_vbln into wa_vbln.
    write:/ wa_vbln-vbeln,
            wa_vbln-netwr.
       hide: wa_vbln-vbeln.
 
endloop.
 
   when '2'.
  select a~posnr b~matnr into corresponding fields of table it_vbap from
         vbap as a inner join mara as b on a~matnr = b~matnr.
loop at it_vbap into wa_vbap.
write:/ wa_vbap-posnr,
        wa_vbap-matnr. 
endloop.
 
endcase. 
Top-of-page during line-selection.
if sy-lsind = '1'.
    write:/ ' first list'. 
elseif sy-lsind = '2'.
    write:/ 'second list'. 
else.
   write:/ 'proper list no'. 
endif.

Modularization Techniques

Modularization means minimization, if the programs contain the same or similar blocks of statements or it is required to process the same function several times, we can avoid this redundancy or duplication of codes by using modularization techniques.

By modularizing the ABAP/4 program

1. We make them easy to read i.e. improves the readability
2. Improve their structure
3. Error handling and maintenance is easy
4. Updating can be done easily
5. Reusability i.e. procedures can be used in other programs as well
6. Encapsulation i.e. hides the internal architecture and data structure from other classes. It is important to use a series of functions that act as the means to access the data.
7. Reduces code redundancy i.e. avoids duplication of codes

Different modularization techniques available are:

1. SUBROUTINE
2. FUNCTION MODULE
3. METHODS
4. INCLUDES AND MACROS

METHODS: It describes the functions and behavior of classes and their instances in ABAP objects methods must be defined in classes.

MACROS: Macros designed based on place holder (place holder works like pointers in c language but in case of place holder changes effect on output.

INCLUDES: If I am defining the same variables in many programs, instead of defining in all programs we have define all the variables in one program and that program is included or added to the programs whenever needed that’s called include program. It cannot be executed independently it has to include in a program.

SUBROUTINES:
A subroutine is a reusable section of code. It’s like a mini program that can be called from another point in your program. Subroutine is generally for local modularization i.e. they are generally called from the program in which they defined.

We can use subroutine to write functions that are used repeatedly with in a program. You can define subroutine in any ABAP programs.

Syntax for defining subroutine

FORM (subroutine name) (parameter (Optional))
-----------------------------------
----------------------------------
-----------------------------------
END FORM.

Syntax for calling subroutines

PERFORM (subroutine name)(parameter)

Subroutines cannot be nested, therefore place subroutine definitions at the end at the program. One subroutines can call other subroutines and may also call themselves (recursive). Once a subroutine has finished running, the calling program carries on processing after the perform statement.

There are two types of subroutines:

1) Internal subroutine: If the source code or body of the subroutine will be in the same ABAP/4 program i.e. in the calling program which is called internal subroutine.

2) External subroutine: If the source code or body of the subroutine present other than the calling program which is called external subroutine. An external subroutine is one that resides in a different program that the perform statement that calls it.

Syntax for calling external subroutines:
Report ztn1811
Perform (S1) (ztn1811)

Report ztn1811
FORM s1
-----------
----------
ENDFORM.

PARAMENTERS IN SUBROUTINES

Parameters can be either local or reference to global variables. The memory for local parameters is allocated when the subroutine is called & freed when it ends. If we define variables on the form statement, the perform statement must pass a value to each of these variables.

Global variables: A global variable is one that is defined outside of a subroutine by using the tables or data statement. It can be accessed from any point in the program be it inside an event or inside a subroutine.

Local variables: A local variables is a variable that is defined inside a subroutine using local data or static’s statement. It is said to be local to subroutine.
The two types of parameters are:

Formal parameters: Parameter names that appear on the form statements are called formal parameters.

Ex: FORM s1, using P1 changing P2, P3
Here P1, P2, P3 are called formal parameters.

Actual parameters: Parameter names that appears on the perform statement are called actual parameters.

Ex: PERFORM S1 using P1 changing P2, P3.
Here P1, P2, P3 are called actual parameters.

There are three ways of passing parameters to a subroutine.

1) Pass by reference
2) Pass by value
3) Pass by value & result

1) Passing parameters by reference

During subroutine call, only the address of the actual parameter is transferred to the formal parameters i.e. pointer to the original memory or address location is passed. The formal parameter has no memory of its own & we work with the fields of the calling program with in a subroutine. So changes to the variable within the subroutine update the original memory location immediately i.e. the field contents in the calling program also changes.
Ex: Report xyz.
Data F1 value A.
Performs s1 using F1.
Write: / F1.

Form s1 using P1
P1 = X.
Write:/ P1.
End form

Here: Memory address for F1 is 1000 (Example)
F1 = A before calling s1
F1 = X after assignment in s1
P1 – P1 is a pointer to memory location 1000
O/p P1= x
F1= x
2. Passing parameters by value :

When you pass a parameters by value, new memory is allocated for the value i.e. the formal parameters are created as copies of the actual parameters, thus formal parameters have memory of their own i.e. the memory allocated when the subroutine is called and freed when the subroutine returns. Therefore changes to the formal parameters have no effect on the actual parameters.

EX: Report xyz
Data: F1 value A.
Perform s1 using F1.
Write: / F1.

From s1 using value (P1)
P1 =X.
Write: / P1.
End form.
Here: F1 = A before call to s1
F1 = A after assignment in s1
0/P P1 = X
F1 = A

3. Passing parameters by value & result:

Pass by value & result is similar to pass by reference like Pass by value, a new memory area is allocated & it frees when the subroutine ends. When the end form statement executes, it copies the value of the local memory area back into the original memory area changes to the parameter with in the subroutine are reflected in the original but not until subroutine returns.

Ex: Report xyz
Data: F1 value A
PERFORM: S1 changing F1
S2 changing F1
End of selection
Write: / F1.
Form s1 changing value (P1)
P1 =B.
Write:/ P1.
End form
Form S2 changing value (P2)
P2 = ‘X’.
Write: / P2.
Stop.
End form
0/P P1 = B
P2 = X
F1 = B

Leaving subroutine
You can exit out of a subroutine at any time using the following statements.

1) Stop: Immediately leaves the subroutine & goes directly to the end of selection event.
2) Exit: It leaves the loop, subroutine or comes out of the program & display the result without any condition.
3. Check: It also comes or immediately leaves the subroutine but it depends on logical expression. If it is false comes out of the loop or subroutine.

Classical Reports

Events in Classical report
Events associated with classical report are as follows and each one will be discussed in detail.

1. INITIALIZATION
2. AT SELECTION-SCREEN
3. AT SELECTION-SCREEN ON
4. START-OF-SELECTION
5. TOP-OF-PAGE
6. END-OF-SELECTION
7. END-OF-PAGE

In this case first three events are associated with selection screen. Rests of the events are associated with your list.

INITIALIZATION

We have already seen how to fill default values for the selection criteria. But in many cases you need to calculate the value and then put it in selection criteria. For example, say, you are accepting date from user and you need to fill in the default value for lower range as sy-datum – 30 days and sy-datum for higher range. In this case you are calculating lower range and then filling the criteria. This can be done in INITIALIZATION event. Piece of code to do the above task would look like the following:

Tables: Sflight.
Select-options: fldate1 for sflight-fldate.
INITIALIZATION.
Data: date1 like SY-DATUM.
Date1 = sy-datum – 30.
Fldate1-low = date1.
Fldate1-high = sy-datum.
Append fldate1.
* Here appending is required because fldate1 is int’ table
This event is triggered when you execute your program for the first time i.e., before selection screen is displayed.

AT SELECTION-SCREEN

When user enters the values in the fields of selection screen and clicks on execute button, this event gets triggered. This event is basically for checking the values entered by the user for the fields of the selection screen i.e., data validity checking. This event is for entire selection screen. For example:
You are accepting carrid, connid, fldate from user and you don’t want to proceed if user enters no value for carrid and fldate. Using AT SELECTION-SCREEN can do this.

Select-options: carrid1 for sflight-carrid,
Connid1 for sflight-connid,
F1date1 for sflight-f1date.

AT SELECTION-SCREEN.
If carrid1-low ne ‘ ’ and fldate1-low = ‘ ’.
Error message.
Endif.

In this case, if both the fields are entered blank, then the user gets error message. Basically, this event is for many fields on selection screen. Usually, it is for the fields which are logically related.

AT SELECTION-SCREEN ON

When you want to check for specific value of a field. For example, carrid should be in the range of ‘LH’ and ‘SQ’. This can be done in this event. Basically, this event is for checking individual fields. You can have many AT selection-screen events in your program (i.e., for each field specified in the Select-Options).

Select-Options carrid1 for sflight-carrid.
AT SELECTION-SCREEN.
If carrid1-low ne ‘LH’ and carrid1-high ne ‘SQ’.
Error message.
Endif.
Here the system will not proceed on entering wrong values.

START-OF-SELECTION

This is the first event for your list. Once all the events are triggered for selection screen, the data is retrieved from database. Data declaration, select statements are done in this event. Consider the following example:

START-OF-SELECTION.
Data: mtype i.
Tables: sflight.
Select * from sflight where carrid = ‘LH’.
Write: / sflight-carrid,sflight-connid.
Endselect.

TOP-OF-PAGE

This event is triggered with first WRITE statement or whenever new page is triggered. Advantage of using this event is that, whatever you write under this event, is applicable to all the pages. If you don’t have any write statement before TOP-OF-PAGE or in START-OF-SELECTION, this event is not triggered at all. For example, if you want the name of company and column headers for all the pages, it can be written in this event.

TOP-OF-PAGE
Write: / ‘Bangalore’.
Write : / 10 ‘carrid’, 20 ‘connid’, 30 ‘fldate’.

END-OF-PAGE

This event is triggered at the end of page.
End-of-page.
Write : / ‘page number’, sy-pagno.
In this case page number will be written on every page.

Conditional triggering of EOP

Consider the following case.
REPORT ZDEMO1 line-count 15(3).
Top-of-page.
Write: ‘this line is written by top-of-page event’.
Start-of-selection.
Write: ‘this line is written by start-of-selection event’.
End-of-page.
Write : ‘this line is written by end-of-page event’.

In this case EOP will never be triggered, as end of page is never reached. The total Line-count defined for page = 15 in which 3 lines are for footer area. The output of the above code will be
This line is written by top of page event.
This line is written by start of selection event.

In output screen, only two lines are written and cursor remains still on 3rd line, the end-of-page event is not triggered. To trigger end of page event, cursor should reach at the last position, in this case on 11th line.

Such cases are quite common, and could be overcome by conditional triggering of end of page.

Sy-linct is the system variable, which gives total line count of a list.
Sy-linno is the system variable, which gives the current line number where the cursor is placed on the list.

Consider the following case:

Report zdemo1 line count 20(1).
Start-of-selection.
Data: m type i.
Write: / ‘this is first line’.
Do 5 times.
Write: / ‘the number is’, sy-index.
Enddo.
M = sy-linct, sy-linno – 1.
Skip x.
End-of-page.
Write: / ‘page end’.

The output of above example is as follows :
This is first line.
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
After skipping 10 lines
Page end

In this case, with all write statement, you don’t reach to the end of page. After all write statement, m is calculated in this case:

M = 20 – 8 – 1, So m is 12. And 11 lines are skipped after write statement and end of page is reached. (In this case you have 6 write statement so 6 lines + 1 line for page number and 1 horizontal line which is displayed for any list. So cursor is on 8th line and is subtracted from total line count i.e, 20.)

Using Variants with selection criteria

In many cases you need report to execute report at regular interval for certain fixed values of selection criteria. That means each times you execute the report you need to enter its values again and again. ABAP/4 provides the facility by which you can define the values for selection screen and store it. Using VARIANTS can do this. It can be defined as group of values used for selection criteria while executing report. For a particular report, you create a variant which means variant created for particular report cannot be used for another report. The group of values for the selection criteria is saved and assigned a variant name. So every time you call a report, you need not specify the values for selection criteria but instead call the variant thus avoiding extra typing. User can have many variants for a single report. Each of them can be used as different type of information. For example, if a manager wants to see how an employee in personnel department or admin department has performed. He need not enter the department, one has to just execute the report with variant. In case he doesn’t know about the variant, which is available, he can display list of variants attached to the report and values assigned to each variant.

Creating variant
> Execute the report program. The selection screen is displayed.
> Enter the values for selection screen and click on saves.
> System displays the variant screen
> Enter the variant name and description for it.
> Save it.

Usually the variants are useful when you need to execute the report in background, which will be discussed in background processing.

Example for Classical Report Events.

*&---------------------------------------------------------------------*
*& Report ZBASU_CLASSICAL_REPORT_EVENTS *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT ZBASU_CLASSICAL_REPORT_EVENTS NO STANDARD PAGE HEADING LINE-SIZE 90 LINE-COUNT 40(2).

Tables: mara.

data: begin of itab occurs 0,
matnr like mara-matnr,
ersda like mara-ersda,
ernam like mara-ernam,
end of itab.

DATA: M TYPE I.

selection-screen: begin of block b1 with frame title text-002.
select-options: material for mara-matnr.
selection-screen: end of block b1.

INITIALIZATION.
MATERIAL-LOW = '100'.
MATERIAL-HIGH = '200'.
MATERIAL-SIGN = 'I'.
MATERIAL-OPTION = 'BT'.

AT SELECTION-SCREEN.

IF MATERIAL-LOW = ' ' AND MATERIAL-HIGH = ' '.
MESSAGE 'ENTER PROPER DATA' TYPE 'E'.
ENDIF.

START-OF-SELECTION.
SELECT MATNR ERSDA ERNAM FROM MARA INTO CORRESPONDING FIELDS OF TABLE ITAB WHERE MATNR IN MATERIAL.

TOP-OF-PAGE.

WRITE:/ 'GALAXE SOLUTIONS'.

END-OF-SELECTION.

LOOP AT ITAB.
WRITE:/ ITAB-MATNR,
ITAB-ERSDA,
ITAB-ERNAM.
ENDLOOP.

M = SY-LINCT - SY-LINNO - 2.
SKIP M.

END-OF-PAGE.

WRITE:/ 'BANGALORE', SY-DATUM.

Monday, June 1, 2009

Reports

About reports

Reports, in the R/3 system are online programs whose function is to retrieve data from database and display it or print it for the user. An end user often needs some information to look up, depending upon which various management decisions are taken, or to just see business results or simply to continue work. As R/3 is collection of all business applications, it has provided a very powerful feature to satisfy this crucial business need i.e., reports are involved at each and every step of business. This type of extracting, collecting and formatting data that is stored in database is done by REPORT program.

The program that is written to retrieve and display data is REPORT program and the data that is displayed on the screen when you execute the program is called as LIST (output of the report).

When you display data, you need to display the data, user needs. For example, user wants to see the all the employee, who has joined after 12th December 1997. In this case user has to pass this information, to the system, that he needs only those employee records where joining data is greater than 12th December 1997. For user, it is passing information to the system but for the system it is input from the user. System takes input from the user before it retrieves the data from the database. This is very common requirement of any report as the need of any business is to display data, which is required by user.

> Reports are ABAP/4 programs.
> You use reports to evaluation data from database tables. The results of such an evaluation can be displayed on the screen or printed form.
> Reports are stand-alone programs.
> The user can execute reports directly via the program name, for example, by choosing System ® Utilities ® Reporting.
> A report program contains a collection of processing blocks for different events that are always triggered externally. In a report, you can react on events by programming the corresponding processing blocks or ignore the events by not writing the corresponding processing blocks. A report itself never creates events.
> Reports can use logical databases or select statements defined by developer.
> For each application, SAP supplies logical databases. Or you can easily create logical database yourself.
> Event control of a report corresponds to a certain scheme:
When a report is executed, the ABAP/4 processor creates together with the logical database used (if any) a sequence of certain events for which you can program processing blocks. The chronology of the events is (more or less)

Steps involved in creating a Report:

1. Processing the selection screen
After starting a report, the selection screen allows the user to enter limits or control values for further report processing. The report can contain several processing blocks for events during selection screen processing, for example, for checking the input values.

2. Reading the database
After selection screen processing come the events for reading the database. Either the report reads data from relational databases it using the corresponding ABAP/4 statements (open SQL) or leaves this task to a logical database. In the latter case, the logical database creates a sequence of events to allow the report to copy the data.

3. Evaluating data and creating lists
During or after reading the database the report creates the output list. During list creation, several events allow you to layout the output list (for example, layout the page header).

4. Outputting a list
The last part of the processing sequence controlled by the ABAP/4 processor is the list output on the screen or printer. When displaying the list on the screen, user can trigger other reports, that are interactive and are event driven. For example, by clicking the mouse. By programming processing blocks for these events, you change a normal report to a so-called Interactive report. If a report does not contain event keywords, the entire coding of the report belongs to a single processing block, which is called by a standard event. This standard event is triggered directly after processing the selection screen.

Selection criteria

System accepts inputs from user through SELECTION CRITERIA.
Selection criteria are nothing but input fields which allows the user to restrict information given to program for processing further data. If you don’t specify any criteria for selection, your report program produces a long list of data, which might not be needed by the user. Basically, selection criteria are the fields where user enters some value for which he needs information. Through selection criteria user can enter discrete value or ranges. For example, user wants to see all the records of the employees, who have joined between 12th December 1997 and 12th May 1998. This range can be entered in selection criteria. As the user becomes more specific for mentioning the criteria, the list will be smaller and more specific.

Syntax:
SELECT-OPTIONS (field) for (table field).

Field is the variable, which you declare for accepting input from the user.
Table field is reference field.

SELECT-OPTIONS: fld1 for sflight-fldate,
carrid1 for sflight-carrid.
Maximum length of the name Select-Options variable is 8.
When system executes this statement, the selection screen is displayed and is like this.

When you enter the desired information and click on execute button, rest of the program is executed, that is retrieval of data from database, which matches this information and the list is displayed.

Behavior of SELECT-OPTIONS

When the Select-Options statement is executed the system creates the internal table with the same variable name (in this case it will be carrid1). This table is also called as selection table. The main purpose of selection table is to store selection criteria. The table has four standard fields, which are as follows:

> SIGN is a variable, which denotes the system whether the result should be included with those particular criteria. It can contain either I or E. I denotes Inclusion. The criteria are included. E denotes Exclusion. The criteria are excluded from the result.

> LOW the data type of LOW is the same as the field type of the database table, for which you are using selection criteria. This acts as lower limit of the selection.

> HIGH the data type of HIGH is the same field type of the database table, for which you are using the selection criteria. This acts as higher limit. If you don’t enter HIGH value then the LOW value defines a single value selection.

> OPTION is two-character field, which contains operators like EQ, GT, LT, GE, and LE.
When the user enters both the values i.e., high and low then this field acts as BT (between). If you don’t enter high value then all other operators can be Applicable.

For each Select-Options statement system creates internal table.

Default values for select-options

If you want to display default values for the selection criteria before your screen is displayed, give default values for the selection table fields i.e., low or high.

SELECT-OPTIONS: CARRID1 FOR SFLIGHT-CARRID DEFAULT CARRID1-LOW = ‘LH’ AND CARRID1-HIGH = ‘SQ’.

In this case selection screen is displayed with default values ‘LH’ for lower range and ‘SQ’ for higher range. User can use same values or overwrite these values with new values, whichever he needs.


Parameters

Parameter statement is used to accept input from user. PARAMETER statement is used when you want user to enter data and depending upon what he enters you need to take action. The parameter statement declares the variable and also allows system to accept data into that variable.

Syntax.
Parameters: num type I.
Here parameter statement declares the variable and creates the selection screen on which user enters the data i.e., in this case num is declared of type I and user can enter any number. Entered value is stored in the same variable and can be used in program.


Data: m type I
Parameters: num type I
M = num – 5
Write: / ‘The number is’, m.
You can define default values with parameter statement for example
Parameter: num type I default 12.
In this case when selection screen is displayed the default value is displayed. User can either use same value or overwrite the value.
Parameter of type character and length = 1, can be displayed as Checkbox and Radiobutton.
Parameter: C1 as Checkbox,
C2 as Checkbox.
Parameter: R1 Radiobutton group g1,
R2 Radiobutton group g1.

When parameter is defined as Radiobutton, it needs to be attached to one group. Only one Radiobutton of one group can be clicked.

Every parameter can be associated with language dependent text that is displayed on the selection screen. This can be done with the help of text elements.

WRITE Statement

The basic APAB/4 statement for outputting data on the screen is WRITE.

Syntax:
WRITE (field) (option).

This statement outputs the field (f) to the current list in its standard output format.
By default, the list is displayed on the screen.
The field (field)can be any variable or table field or just literal.

PROGRAM ZDEMO
WRITE: /‘HELLO’.

When you start this program, the system leaves the current screen i.e., your editor screen and branches to the output screen, which is also called as list screen:

The list screen has the same name as the title of the program specified in the program attributes. First line on the screen contains the list header. By default, the list header is the same as the title of the program. The current page number (1) appears on the right. The list header is followed by one line and then the output is displayed.

Write : ‘HELLO’.
Write : ‘WORK HARD’

On the screen, the output is normally left justified. But in above case, because we have used two WRITE statements, the output fields are displayed one after the other, each separated by one column (i.e., one blank). If there is not enough space for an output field on the current line, a new line is started.

Almost all system-defined fields are right justified except FLOAT, INTEGER, and PACKED i.e., number field. The numeric data types F, P, and I are right justified and padded with blanks on the left. If there is sufficient space, thousands of separators are also output. If a type P field contains decimal places, the default output length is increased by one.

With the data type D, the internal format of a date differs from its output format. When you use the WRITE statement for outputting data, the system automatically outputs dates of type D in the format specified in the user’s master record (e.g. DD/MM/YYYY or MM/DD/YYYY).

Internal Tables

By definition, internal tables are user defined structured data types. These internal tables can be created as a replica of data base table, using some or all fields of one table or more tables. These internal tables are created only during run time no memory is reserved.

Why we need internal table
Long life data is stored in database tables when we are directly modifying data there is every possibility that we may loose data by accident, which we can not afford to do so, As such we need some intermediate etc tables to do some operations upon satisfactory results, we can modify database tables.

Defining Internal Tables:

Internal table with header line: When we create internal table with header line, a default work area is created.
Internal table with out header line: In this case, we need to create explicit work area to work with it. When we need to nest the tables with in tables, we need to use this type of internal table.

Types of internal tables

1. Standard tables: - Standard tables have an internal linear index. The system can access records either by using the table index or the key. The response time for key access is proportional to the number of entries in the tables. The key of standard table is always non-unique. Standard tables can always be filled very quickly, since the system does not have to check whether there are already existing entries.

Key access to a standard table uses a linear search. This means that the time required for a search is in linear relation to the number of table entries. You should use index operations to access standard tables.

2. Sorted Tables: - Defines the tables as one that is always saved correctly sorted by key. The system can access records either by using the table index or the key. The response time for key access is logarithmically proportional to the number of table entries, since the system uses a binary search. The key of a sorted table can be either unique or non-unique.

If the key is not unique, the system takes the entry with the lowest index. The runtime required for key access is logarithmically related to the number of table entries.

3. Hashed tables: - Hashed tables have no linear index. Hashed table is accessed using its key. The response time is independent of the number of table entries and is constant, since the system accesses the table entries using a hash algorithm. The key of a hashed table must be unique.

Defines the tables as one that is managed with an internal hash procedure. You can only access a hashed table using the generic key operations or other generic operations (sort, loop and so on) Explicit or implicit index operations (such as loop... from and insert itab with in and loop) are not allowed.

4. Index Table: - A table that can be accessed using an index. Index table is only used to specify the type of generic parameters in a from or function. That means that you can not create a table of type Index. Standard and sorted tables are index tables.
Initializing Internal Tables

Clear: Clears the header (Work Area) line of the internal table.
Syntax: clear Itab.

Clear []: Clears the body of the internal table without clearing the work area or header.
Syntax: Clear itab [].

Refresh : Delete all table entries, memory space remains occupied i.e. only removes only contents of internal table.

Free : Delete all table entries, memory space is released i.e. de-allocates memory associated with internal table.

Operations of Internal Tables

1. Append: It will take data from work area or header into internal table body.
Syntax: Append itab (append itab to itab)
Append wa_itab to itab.

2. Collect: It will work similar to append operation but it will adds the same integer fields otherwise it will append it.
Syntax: Collect itab.

3. Insert: Used to insert data at any place. For this we have to specify the index no otherwise its goes to dump screen.
Syntax: insert itab index (Index number).

4. Modify: This operation will modify the existing records.
Syntax: Modify itab index (Index number).

5. Read: Used to read a single record from itab with key or index and it will read from application server to the screen.
Syntax: read itab with key (field name)

6. Sort: Used to sort the internal tables and by default it is ascending.
Syntax: Sort itab by (Field name).

7. Delete: Used to delete the data from internal tables but you need to give index number or range and field names.
Syntax: Delete itab by index (Index number)

8. Describe: Used to describe the internal tables like how many records and what kind of internal table etc.
Syntax: Describe table itab lines (variable name)

Example for internal table operations
*&---------------------------------------------------------------------*
*& Report ZBASU_INTERNAL_TABLE_OPERATION *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT ZBASU_INTERNAL_TABLE_OPERATION.

DATA: BEGIN OF ITAB OCCURS 0,
F1,
F2 TYPE I,
F3(3),
END OF ITAB.

DATA: LIN.

******Append Operation
ITAB-F1 = 'A'.
ITAB-F2 = 10.
ITAB-F3 = 'BC'.
APPEND ITAB.
CLEAR ITAB.

ITAB-F1 = 'B'.
ITAB-F2 = 15.
ITAB-F3 = 'BD'.
APPEND ITAB.
CLEAR ITAB.

******* Collect Operation
ITAB-F1 = 'A'.
ITAB-F2 = 20.
ITAB-F3 = 'BC'.
COLLECT ITAB.
CLEAR ITAB.

ITAB-F1 = 'C'.
ITAB-F2 = 25.
ITAB-F3 = 'BF'.
APPEND ITAB.
CLEAR ITAB.

*******Insert Operation
ITAB-F1 = 'D'.
ITAB-F2 = 30.
ITAB-F3 = 'BG'.
INSERT ITAB INDEX 2.
CLEAR ITAB.

*******Modify Operation
ITAB-F1 = 'F'.
ITAB-F2 = 40.
ITAB-F3 = 'BH'.
MODIFY ITAB INDEX 2.
CLEAR ITAB.

ITAB-F1 = 'F'.
MODIFY ITAB TRANSPORTING F1 WHERE F1 = 'D'.
CLEAR ITAB.

*******Sort Operation
SORT ITAB.
SORT ITAB BY F2.

******Read Operation
READ TABLE ITAB INDEX SY-TABIX INTO ITAB.
READ TABLE ITAB INDEX 3 INTO ITAB.
READ TABLE ITAB INTO ITAB WITH KEY F1 = 'D'.

WRITE:/ ITAB-F1,
ITAB-F2,
ITAB-F3.

******Describe Operation
DESCRIBE TABLE ITAB LINES LIN.

******Delete Operation
DELETE ITAB INDEX 2.
DELETE ITAB WHERE F1 = 'A'.

WRITE:/ 'NO OF LINES IN MY INTERNAL TABLE', LIN.
LOOP AT ITAB.
WRITE:/ ITAB-F1,
ITAB-F2,
ITAB-F3.
ENDLOOP.

Monday, April 27, 2009

Table Types

Table types are construction blueprints for internal tables that are stored in the ABAP Dictionary. When you create a table type in the ABAP Dictionary, you specify the line type, access type, and key. The line type can be any data type from the ABAP Dictionary, that is, a data element, a structure, a table type, or the type of a database table. You can also enter a predefined Dictionary type directly as the line type, in the same way that you can with a domain.

In an ABAP program, you can use the TYPE addition to refer directly to a table type. If you define a local data type in a program by referring to a table type as follows:
TYPES dtype TYPE table.

The construction blueprint of the table type is used to create a local internal table dtype in the program. The predefined Dictionary data types of the domains used by the data elements in the structure are converted into the corresponding ABAP types. The semantic attributes of the data elements are used for the corresponding components of the internal table in the program.