You are on page 1of 19

Select with JOINS

SELECT WITH JOINS: 1. This statement is used to fetch the data simultaneously from multiple tables. 2. That means the multiple tables must be joined using one or two fields. 3. There should be at least one common field between tables. 4. There are two types of joins. INNER JOIN: 1. In this join only the matching records between the tables will be fetched. 2. The unmatched records are not selected. OUTER JOIN: 1. In this join all the records from the first table or left table will be selected first. 2. If there are any matching records from second, third tables, then the data will be displayed. 3. Suppose if there are no matching records, the data will be displayed as blank from second and third tables.

SYNTAX: Select T1~F1 T1~F2 T2~F1 T2~F2 T3~F1

ABAP @ ARJUN, Igrow Soft

T3~F2 INTO TABLE <ITAB> FROM T1 AS <Alias name> <Inner Join/outer Join> T2 as <Alias name> on T1~F1 =T2~F1 <Inner Join/outer Join> T3 as <Aliasname> on T2~F1 = T3~F1 where <condition>.

ZCUST_DATA CNO 1001 1002 1003 CNAME AAA BBB CCC CITY HYD BLORE CHENNAI CNO 1001 1001 1001 1003 1003

ZCUST_BANKDATA BANKID CITY01 SBI01 SBH01 ICICI01 HDFC01 BANKNAME CITYBANK SBI SBH ICICI HDFC

Joined on ZCUST_DATA~CNO = ZCUST_BANKDATA~CNO


INNER JOIN OUTPUT CNO 1001 1001 1001 1003 1003 CNAME AAA AAA AAA CCC CCC BANKID CITY01 SBI01 SBH01 ICICI01 HDFC01 BANKNAME CITYBANK SBI SBH ICICI HDFC CNO 1001 1001 1001 1002 1003 1003 OUTERJOIN OUTPUT CNAME AAA AAA AAA BBB CCC CCC BANKID CITY01 SBI01 SBH01 ICICI01 HDFC01 BANKNAME CITYBANK SBI SBH ICICI HDFC

ABAP @ ARJUN, Igrow Soft

Ex on JOINS with 2 Tables TYPES : BEGIN OF ty_mara_makt, matnr TYPE mara-matnr, mtart TYPE mara-mtart, mbrsh TYPE mara-mbrsh, meins TYPE mara-meins, spras TYPE makt-spras, maktx TYPE makt-maktx, END OF ty_mara_makt. DATA : i_mara_makt TYPE TABLE OF ty_mara_makt . DATA : wa_mara_makt TYPE ty_mara_makt . SELECT mara~matnr mara~mtart mara~mbrsh mara~meins makt~spras makt~maktx INTO TABLE i_mara_makt FROM mara AS mara INNER JOIN makt AS makt ON mara~matnr = makt~matnr WHERE mara~mtart = 'FERT' AND makt~spras = 'EN' .

ABAP @ ARJUN, Igrow Soft

LOOP AT i_mara_makt INTO wa_mara_makt. WRITE : / wa_mara_makt-matnr , wa_mara_makt-mtart COLOR 6, wa_mara_makt-mbrsh , wa_mara_makt-meins , wa_mara_makt-spras COLOR 1, wa_mara_makt-maktx COLOR 1 . ENDLOOP. The output is :

ABAP @ ARJUN, Igrow Soft

Ex on JOINS with 3 Tables

ABAP @ ARJUN, Igrow Soft

The output is :

ABAP @ ARJUN, Igrow Soft

SelectFor All Entries: 1. This statement is used to replace select with joins. 2. Because JOINS statement cannot be used for more than three tables. 3. If we use more than three tables it puts heavy load on the Database, because the data has to be selected by comparing each table in the database server. 4. So it takes the long time for execution. 5. In such cases we go for SELECT FOR ALL ENTRIES. 6. This statement will never put load on the database. Because only two tables (Internal table and database tables) are compared. SYNTAX: Select F1 F2 F3.. From <DB. Table1> Into table <ITAB1> Where <conditions>. If ITAB1[] is not initial. Select F1 F2 F3.. From <DB.Table2> Into table <ITAB2> For all entries in <ITAB1> Where F1 = <ITAB1-F1> AND F2 = <ITAB1-F2>. Endif.
ABAP @ ARJUN, Igrow Soft

7. The prerequisite for the above statement is, the first ITAB should be checked whether it has any data. 8. Suppose if it has data, then for all entries statement will execute based on the condition and only matching records will be selected. 9. Suppose if first ITAB is empty then for all entries statement will fail and all the records will be selected, irrespective of condition. Finally the first ITAB should be checked for any records using below statement. If ITAB1[] is not initial. Select ... ... For all entries in . Endif.

Ex on SelectFor All Entries Develop a material master report to display material details along with description details

ABAP @ ARJUN, Igrow Soft

ABAP @ ARJUN, Igrow Soft

Save->Act->Test. Second way: As per performance, this is a good stmtBcoz we are using Binary Search.

Save->Act->Test.

ABAP @ ARJUN, Igrow Soft

Third Way: In Real Time, we always move the data into Final Int.Table

Save->Act->Test.

ABAP @ ARJUN, Igrow Soft

SELECTION SCREEN:
An input screen to a program or report is called selection screen. We can design the selection screen using the following statements.

PARAMETERS: SYNTAX: 1. 2. 3. 4. Parameters : <pname> type <table-fname>. Parameters : <pname> type <table-fname> obligatory. Parameters : <pname> as checkbox. Parameters : <pname1> radiobuttongroup <group name>, <pname2> radiobuttongroup <group name> default X, <pname3> radiobuttongroup <group name>. 5. Parametrs : <pname>(length) type <Data type>.

ABAP @ ARJUN, Igrow Soft

Note: Whenever a checkbox or radio button is selected on the selection screen, the character X will be stored internally in the parameter variable. SELECT-OPTIONS: This statement is used to create two input fields so that the user can enter a range of values. In addition to that he can also enter i. ii. iii. iv. Multiple single values (1, 7, 13, 98,257.) Multiple ranges (1-10, 27-30.) Exclude single values (3, 10, 15) Exclude ranges (11-15, 95-99.. )

All these options are available when you click on multiple selection or extension box Syntax: Select-options <so_name> for <table-fname>. Select-options <so_name> for <table-fname> No Intervals. Select-options <so_name> for <table-fname> No-Extension. Select-options <so_name> for <table-fname> No Intervals No- Extension. NOTE: Whenever we use select-options we should use IN instead of = or EQ in the where condition of a select statement.
ABAP @ ARJUN, Igrow Soft

multiple selection.

SELECT-OPTIONS INTERNAL TABLE: Whenever we declare select-options, by default an internal table will be created with four fields, using old syntax i.e. occurs 0 with headerline. The 4 fields are: SIGN: Stores sign of numbers entered on selection-screen. The signs are: I - including (only specified values will be selected). E- Excluding ( only specified value will be excluded). By default I is stored. OPTION: Stores relational operator between the numbers. i. ii. The operators are: BT, NB, EQ, NE, LT, LE, GT, and GE. By default BT is stored..

LOW: Stores lower value from the range entered on selection screen. HIGH: Stores higher value from the range entered on selection screen.

ABAP @ ARJUN, Igrow Soft

SELECTION SCREEN MISCELLANEOUS COMMANDS:

Ex on selection-screen commands: Selection-screen begin of block b1 with frame title text-001. Select-options: s_kunnr for kna1-kunnr. Selection-screen uline.

ABAP @ ARJUN, Igrow Soft

Parameters: p_land1 type kna1-land1. Selection-screen skip. Parameters: p_matnr type mara-matnr. Selection-screen end of block b1. Selection-screen begin of block b2 with frame title text-002. Selection-screen begin of line. Selection-screen comment 2(15) text-003. Parameters: p_lifnr type lfa1-lifnr. Selection-screen comment 30(10) text-004. Parameters: p_land2 type lfa1-land1. Selection-screen end of line.

Selection-screen end of block b2.

ABAP @ ARJUN, Igrow Soft

Modularization
It is a technique of dividing a main program into smaller programs or modules for better reusability and readability. In real time, every program must be divided into modules. In Abap, Modularization is implemented using following techniques. 1.Include Programs 2.Function Modules 3.Subroutines 4.Classes 5.Macros These are Include Programs: These are sub programs which are used for reusability purpose. These programs cannot be executed independently. Theses must be inserted in Main programs for execution. This program does not contain any parameter interface i.e. No importing, Exporting Parameters. Syntax : INCLUDE <Prog Name>
ABAP @ ARJUN, Igrow Soft

Ex on INCLUDE program : Include z_modularization_top.

Tables kna1

Data: i_mara type table of mara Data: wa_mara type mara.

Selection-screen begin of block b1 with frame title text-001. Select-options: s_kunnr for kna1-kunnr. Parameters: p_land1 type kna1-land1. Selection-screen end of block b1. Include zmodularization_getdata.
Select * from mara Into table i_mara Where kunnr in s_kunnr and

land1 = p_land1

Include zmodularization_dispdata.

Loop at I_KNA1 INTO WA_KNA1 . WRITE : / ENDLOOP .

ABAP @ ARJUN, Igrow Soft

ABAP @ ARJUN, Igrow Soft