‎2006 Dec 15 5:41 AM
could you all send me some sample code in ineer join and outer join....
for learning purpose only.......
thanks & regards...
sam.....
‎2006 Dec 15 5:45 AM
‎2006 Dec 15 5:45 AM
‎2006 Dec 15 5:46 AM
‎2006 Dec 15 5:48 AM
‎2006 Dec 15 5:49 AM
hi,
<b>Specifying Two or More Database Tables as an Inner Join</b>
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>.
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.
<b>Specifying Two or More Database Tables as a Left Outer Join</b>
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>.
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'.
Regards,
Vara
‎2006 Dec 15 5:49 AM
Hi Sam
Check in the SAP Documentation. You will find a lot of usefull stuff.
Check in transaction ABAPDOCU for sample code
Thanks
Elan
‎2006 Dec 15 5:50 AM
Hi,
sample code for inner join is:
SELECT MKPFBUDAT MKPFCPUTM
MSEGMATNR MSEGWERKS MSEG~EBELN
MSEGEBELP MSEGERFMG
INTO CORRESPONDING FIELDS OF TABLE
W_DTL_INVOICE
FROM MKPF INNER JOIN MSEG
ON MKPFMBLNR = MSEGMBLNR
AND MKPFMJAHR = MSEGMJAHR
WHERE MKPF~BUDAT > '20040721' AND
MSEG~BWART = '101' AND
MSEG~BUKRS = '1733'.
for outer join : same code, replace the INNER JOIN with OUTER JOIN.
Please see the below link for more help:
<a href="http://help.sap.com/saphelp_erp2004/helpdata/en/67/7e4b3eaf72561ee10000000a114084/content.htm">http://help.sap.com/saphelp_erp2004/helpdata/en/67/7e4b3eaf72561ee10000000a114084/content.htm</a>
Thanks
Shiva
‎2006 Dec 15 6:05 AM
Inner join:it ill work like intersection (it will take the common records based on the 'on condition').
Outer join : ill work as a union.
select fields
into table iternal table
from dbtabname innerjoin dbtabname1
on condition
where condition (depending upon the requirement)
select vbakvbeln vbakernam vbakerdat kna1name1 into table itab
from vbak inner join kna1 on vbakkunnr = kna1kunnr where vbak~erdat
ge date-low ."and vbak~erdat le date-high.
select mbewbwkey maramatnr maktmaktx marmlaeng marmhoehe marmvolum
marmvoleh marmbrgew marm~gewei
into CORRESPONDING FIELDS OF table itab
from mbew
inner join mara on mbewmatnr = maramatnr
inner join makt on maramatnr = maktmatnr
inner join marm on maktmatnr = marmmatnr
where mbew~bwkey = p_key and spras = 'EN'.