Skip to Content
author's profile photo Former Member
Former Member

Inner join command

Hi Forum,

How to use Inner join command?

Please explain with an example.

Thanks,

Mahathi

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Nov 20, 2007 at 04:59 AM

    Inner Join and Outer Join

    The data that can be selected with a view depends primarily on whether the view implements an inner join or an outer join. With an inner join, you only get the records of the cross-product for which there is an entry in all tables used in the view. With an outer join, records are also selected for which there is no entry in some of the tables used in the view.

    The set of hits determined by an inner join can therefore be a subset of the hits determined with an outer join.

    Database views implement an inner join. The database therefore only provides those records for which there is an entry in all the tables used in the view. Help views and maintenance views, however, implement an outer join.

    Specifying Database Tables

    The FROM clause determines the database tables from which the data specified in the SELECT clause is read. You can specify either a single table or more than one table, linked using inner or outer joins. The names of database tables may be specified statically or dynamically, and you can use alias names. You can also use the FROM clause to bypass the SAP buffer and restrict the number of lines to be read from the database.

    "Database table" can equally mean an ABAP Dictionary view. A view links two or more database tables in the ABAP Dictionary, providing a static join that is available systemwide. You can specify the name of a view wherever the name of a database table may occur in the FROM clause.

    The FROM clause has two parts - one for specifying database tables, and one for other additions:

    SELECT... FROM <tables> <options>...

    In <tables>, you specify the names of database tables and define joins. <options> allows you to specify further additions that control the database access.

    Specifying Database Tables Statically

    To specify the name of a database table statically, use the following:

    SELECT... FROM <dbtab> [AS <alias>] <options> . ..

    The database table <dbtab> must exist in the ABAP Dictionary. The AS addition allows you to specify an alternative name <alias> that you can then use in the SELECT; FROM, WHERE, and GROUP BY clauses. This can eliminate ambiguity when you use more than one database table, especially when you use a single database table more than once in a join. Once you have defined an alias, you may no longer use the real name of the database table

    Specifying Database Tables Dynamically

    To specify the name of a database table dynamically, use the following:

    SELECT... FROM (<name>) <options> . ..

    The field <name> must contain the name of a database table in the ABAP Dictionary. The table name must be written in uppercase. When you specify the name of a database table dynamically, you cannot use an empty INTO clause to read all of the columns into the work area <dbtab>. It is also not possible to use alternative table names.

    Specifying Two or More Database Tables as an Inner Join

    In a relational database, you normally need to read data simultaneously from more than one database table into an application program. You can read from more than one table in a single SELECT statement, such that the data in the tables all has to meet the same conditions, using the following join expression:

    SELECT...

    ...

    FROM <tab> [INNER] JOIN <dbtab> [AS <alias>] ON <cond> <options>

    ...

    where <dbtab> is a single database table and <tab> is either a table or another join expression. The database tables can be specified statically or dynamically as described above. You may also use aliases. You can enclose each join expression in parentheses. The INNER addition is optional.

    A join expression links each line of <tab> with the lines in <dbtab> that meet the condition <cond>. This means that there is always one or more lines from the right-hand table that is linked to each line from the left-hand table by the join. If <dbtab> does not contain any lines that meet the condition <cond>, the line from <tab> is not included in the selection.

    The syntax of the <cond> condition is like that of the WHERE clause, although individual comparisons can only be linked using AND. Furthermore, each comparison must contain a column from the right-hand table <dbtab>. It does not matter on which side of the comparison it occurs. For the column names in the comparison, you can use the same names that occur in the SELECT clause, to differentiate columns from different database tables that have the same names.

    The comparisons in the condition <cond> can appear in the WHERE clause instead of the ON clause, since both clauses are applied equally to the temporary table containing all of the lines resulting from the join. However, each join must contain at least one comparison in the condition <cond>.

    Specifying Two or More Database Tables as a Left Outer Join

    In an inner join, a line from the left-hand database table or join is only included in the selection if there is one or more lines in the right-hand database table that meet the ON condition <cond>. The left outer join, on the other hand, reads lines from the left-hand database table or join even if there is no corresponding line in the right-hand table.

    SELECT...

    ...

    FROM <tab> LEFT [OUTER] JOIN <dbtab> [AS <alias>] ON <cond>

    <options>

    ...

    <tab> and <dbtab> are subject to the same rules and conditions as in an inner join. The OUTER addition is optional. The tables are linked in the same way as the inner join with the one exception that all lines selected from <tab> are included in the final selection. If <dbtab> does not contain any lines that meet the condition <cond>, the system includes a single line in the selection whose columns from <dbtab> are filled with null values.

    In the left outer join, more restrictions apply to the condition <cond> than in the inner join. In addition to the above restrictions:

    EQ or = is the only permitted relational operator.

    There must be at least one comparison between columns from <tab> and <dbtab>.

    The WHERE clause may not contain any comparisons with columns from <dbtab>. All comparisons using columns from <dbtab> must appear in the condition <cond>.

    Client Handling

    As already mentioned, you can switch off the automatic client handling in Open SQL statements using a special addition. In the SELECT statement, the addition comes after the options in the FROM clause:

    SELECT... FROM <tables> CLIENT SPECIFIED. ..

    If you use this addition, you can then address the client fields in the individual clauses of the SELECT statement.

    Disabling Data Buffering

    If buffering is allowed for a table in the ABAP Dictionary, the SELECT statement always reads the data from the buffer in the database interface of the current application server. To read data directly from the database table instead of from the buffer, use the following:

    SELECT... FROM <tables> BYPASSING BUFFER. ..

    This addition guarantees that the data you read is the most up to date. However, as a rule, only data that does not change frequently should be buffered, and using the buffer where appropriate improves performance. You should therefore only use this option where really necessary.

    Restricting the Number of Lines

    To restrict the absolute number of lines included in the selection, use the following:

    SELECT... FROM <tables> UP TO <n> ROWS. ..

    If <n> is a positive integer, the system reads a maximum of <n> lines. If <n> is zero, the system reads all lines that meet the selection criteria. If you use the ORDER BY clause as well, the system reads all lines belonging to the selection, sorts them, and then places the first <n> lines in the selection set.

    Examples

    Specifying a database table statically:

    REPORT demo_select_static_database.

    DATA wa TYPE scarr.

    SELECT *

    INTO wa

    FROM scarr UP TO 4 ROWS.

    WRITE: / wa-carrid, wa-carrname.

    ENDSELECT.

    The output is:

    The system reads four lines from the database table SCARR.

    Specifying a database table dynamically:

    REPORT demo_select_dynamic_database.

    DATA wa TYPE scarr.

    DATA name(10) TYPE c VALUE 'SCARR'.

    SELECT *

    INTO wa

    FROM (name) CLIENT SPECIFIED

    WHERE mandt = '000'.

    WRITE: / wa-carrid, wa-carrname.

    ENDSELECT.

    A condition for the MANDT field is allowed, since the example uses the CLIENT SPECIFIED option. If NAME had contained the value ‘scarr’ instead of ‘SCARR’, a runtime error would have occurred.

    Inner join:

    REPORT demo_select_inner_join.

    DATA: BEGIN OF wa,

    carrid TYPE spfli-carrid,

    connid TYPE spfli-connid,

    fldate TYPE sflight-fldate,

    bookid TYPE sbook-bookid,

    END OF wa,

    itab LIKE SORTED TABLE OF wa

    WITH UNIQUE KEY carrid connid fldate bookid.

    SELECT pcarrid pconnid ffldate bbookid

    INTO CORRESPONDING FIELDS OF TABLE itab

    FROM ( ( spfli AS p

    INNER JOIN sflight AS f ON pcarrid = fcarrid AND

    pconnid = fconnid )

    INNER JOIN sbook AS b ON bcarrid = fcarrid AND

    bconnid = fconnid AND

    bfldate = ffldate )

    WHERE p~cityfrom = 'FRANKFURT' AND

    p~cityto = 'NEW YORK' AND

    fseatsmax > fseatsocc.

    LOOP AT itab INTO wa.

    AT NEW fldate.

    WRITE: / wa-carrid, wa-connid, wa-fldate.

    ENDAT.

    WRITE / wa-bookid.

    ENDLOOP.

    This example links the columns CARRID, CONNID, FLDATE, and BOOKID of the table SPFLI, SFLIGHT, and SBOOK, and creates a list of booking numbers for all flights from Frankfurt to New York that are not fully booked. An alias name is assigned to each table.

    Left outer join:

    REPORT demo_select_left_outer_join.

    DATA: BEGIN OF wa,

    carrid TYPE scarr-carrid,

    carrname TYPE scarr-carrname,

    connid TYPE spfli-connid,

    END OF wa,

    itab LIKE SORTED TABLE OF wa

    WITH NON-UNIQUE KEY carrid.

    SELECT scarrid scarrname p~connid

    INTO CORRESPONDING FIELDS OF TABLE itab

    FROM scarr AS s

    LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid AND

    p~cityfrom = 'FRANKFURT'.

    LOOP AT itab INTO wa.

    WRITE: / wa-carrid, wa-carrname, wa-connid.

    ENDLOOP.

    The output might look like this:

    The example links the columns CARRID, CARRNAME, and CONNID of the tables SCARR and SPFLI using the condition in the left outer join that the airline must fly from Frankfurt. All other airlines have a null value in the CONNID column in the selection.

    If the left outer join is replaced with an inner join, the list looks like this:

    Only lines that fulfill the ON condition are included in the selection.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 20, 2007 at 05:00 AM

    hi,

    anyway karthikeyan gave sufficient explanation i am trying to give example to you

    SELECT bzzuwy czzior

    bzzcofrom bzzcerto b~zzorigindt

    czzbussid czbusli

    d~insobez

    FROM zi_ext AS a INNER JOIN zil_ext AS b

    ON ( ainobject = binobject )

    INNER JOIN zio_ext AS c

    ON ( binobject = cinobject )

    INNER JOIN dimaiob AS d

    ON ( cinobject = dinobject )

    INTO CORRESPONDING FIELDS OF TABLE it_table

    WHERE a~zzcode = ztcatcode-zzcatcode.

    regards,

    pavan

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 20, 2007 at 05:01 AM

    hi

    REPORT ZINNERJOINS no standard page heading line-count 60(4).

    TABLES : LFA1,EKKO,EKPO.

    SELECT-OPTIONS : S_LIFNR FOR LFA1-LIFNR.

    DATA : BEGIN OF ITAB OCCURS 0,

    LIFNR LIKE LFA1-LIFNR,

    NAME1 LIKE LFA1-NAME1,

    EBELN LIKE EKKO-EBELN,

    AEDAT LIKE EKKO-AEDAT,

    EBELP LIKE EKPO-EBELP,

    MENGE LIKE EKPO-MENGE,

    END OF ITAB.

    SELECT LFA1LIFNR LFA1NAME1 EKKOEBELN EKKOAEDAT EKPOEBELP EKPOMENGE INTO TABLE ITAB FROM LFA1 INNER JOIN EKKO ON LFA1LIFNR = EKKOLIFNR

    INNER JOIN EKPO ON EKKOEBELN = EKPOEBELN WHERE LFA1~LIFNR IN S_LIFNR.

    LOOP AT ITAB.

    WRITE:/ ITAB-LIFNR,ITAB-NAME1,ITAB-EBELN,ITAB-AEDAT,ITAB-EBELP,ITAB-MENGE.

    ENDLOOP.

    top-of-page.

    write : / 'this is vendors report'

    regards

    Nagesh.Paruchuri

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 20, 2007 at 05:13 AM

    HI

    Inner Join and Outer Join

    The data that can be selected with a view depends primarily on whether the view implements an inner join or an outer join. With an inner join, you only get the records of the cross-product for which there is an entry in all tables used in the view. With an outer join, records are also selected for which there is no entry in some of the tables used in the view.

    The set of hits determined by an inner join can therefore be a subset of the hits determined with an outer join.

    Database views implement an inner join. The database therefore only provides those records for which there is an entry in all the tables used in the view. Help views and maintenance views, however, implement an outer join.

    http://help.sap.com/saphelp_46c/helpdata/en/cf/21ec77446011d189700000e8322d00/frameset.htm

    REPORT ZSK005.

    TABLES : LIKP.

    DATA : BEGIN OF I_LIKP_LIPS OCCURS 0,

    VBELN LIKE LIKP-VBELN,

    LFART LIKE LIKP-LFART,

    LFDAT LIKE LIKP-LFDAT,

    VGBEL LIKE LIPS-VGBEL,

    VGPOS LIKE LIPS-VGPOS,

    END OF I_LIKP_LIPS.

    SELECT-OPTIONS : S_VBELN FOR LIKP-VBELN OBLIGATORY MATCHCODE OBJECT VMVA

    .

    START-OF-SELECTION.

    SELECT LIKPVBELN LIKPLFART LIKPLFDAT LIPSVGBEL LIPS~VGPOS

    INTO TABLE I_LIKP_LIPS

    FROM LIKP INNER JOIN LIPS

    ON LIKPVBELN = LIPSVBELN

    WHERE LIKP~VBELN IN S_VBELN.

    IF SY-SUBRC <> 0.

    MESSAGE E001(ZX).

    ENDIF.

    END-OF-SELECTION.

    LOOP AT I_LIKP_LIPS.

    WRITE : / I_LIKP_LIPS-VBELN,I_LIKP_LIPS-LFART,I_LIKP_LIPS-LFDAT,

    I_LIKP_LIPS-VGBEL,I_LIKP_LIPS-VGPOS.

    ENDLOOP.

    <b>Reward if yusefull</b>

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.