Você está na página 1de 8

hi all,

my requirement is to select fields from 4 tables, where dpr_projects(table-


cprojects) being the main table, the selection is based on status of last chagned
on.

i'll select all the records that got changed after a particular date. that is fine
till here.
but i have to select from another 3 ztables say zpstr_proj_app; zpstr_proj_prac;
zpstr_proj_coe

in all the tables i have guid as the key(common) filed. so i have to select from
all tables the records that satisfies the condition of (last changed on, from main
table dpr_project).

please guid me this.

thanks in advance.

rgds,
chem

hai.

check this.

joins are used to fetch data fast from database tables:


tables are joined with the proper key fields to fetch the data properly.
if there are no proper key fields between tables don't use joins;
important thing is that don't use joins for cluster tableslike bseg and konv.
only use for transparenmt tables.
you can also use joins for the database views to fetch the data.

joins

... from tabref1 inner join tabref2 on cond

effect
the data is to be selected from transparent database tables and/or views
determined by tabref1 and tabref2. tabref1 and tabref2 each have the same form as
in variant 1 or are themselves join expressions. the keyword inner does not have
to be specified. the database tables or views determined by tabref1 and tabref2
must be recognized by the abap dictionary.

in a relational data structure, it is quite normal for data that belongs together
to be split up across several tables to help the process of standardization (see
relational databases). to regroup this information into a database query, you can
link tables using the join command. this formulates conditions for the columns in
the tables involved. the inner join contains all combinations of lines from the
database table determined by tabref1 with lines from the table determined by
tabref2, whose values together meet the logical condition (join condition)
specified using on>cond.

inner join between table 1 and table 2, where column d in both tables in the join
condition is set the same:
table 1 table 2
---- ---- ---- ---- ---- ---- ---- ---- ----
a b c d d e f g h
---- ---- ---- ---- ---- ---- ---- ---- ----
a1 b1 c1 1 1 e1 f1 g1 h1
a2 b2 c2 1 3 e2 f2 g2 h2
a3 b3 c3 2 4 e3 f3 g3 h3
a4 b4 c4 3 ---- ---- ---- ---- ----
---- ---- ---- ----

\ /
\ /
\ /
\ /
\/
inner join
---- ---- ---- ---- ---- ---- ---- ---- ----
a b c d d e f g h
---- ---- ---- ---- ---- ---- ---- ---- ----
a1 b1 c1 1 1 e1 f1 g1 h1
a2 b2 c2 1 1 e1 f1 g1 h1
a4 b4 c4 3 3 e2 f2 g2 h2
---- ---- ---- ---- ---- ---- ---- ---- ----

example
output a list of all flights from frankfurt to new york between september 10th and
20th, 2001 that are not sold out:

data: date like sflight-fldate,


carrid like sflight-carrid,
connid like sflight-connid.

select f~carrid f~connid f~fldate


into (carrid, connid, date)
from sflight as f inner join spfli as p
on f~carrid = p~carrid and
f~connid = p~connid
where p~cityfrom = 'frankfurt'
and p~cityto = 'new york'
and f~fldate between '20010910' and '20010920'
and f~seatsocc < f~seatsmax.
write: / date, carrid, connid.
endselect.

if there are columns with the same name in both tables, you must distinguish
between them by prefixing the field descriptor with the table name or a table
alias.

note
in order to determine the result of a select command where the from clause
contains a join, the database system first creates a temporary table containing
the lines that meet the on condition. the where condition is then applied to the
temporary table. it does not matter in an inner join whether the condition is in
the on or whereclause. the following example returns the same solution as the
previous one.

example
output of a list of all flights from frankfurt to new york between september 10th
and 20th, 2001 that are not sold out:

data: date like sflight-fldate,


carrid like sflight-carrid,
connid like sflight-connid.

select f~carrid f~connid f~fldate


into (carrid, connid, date)
from sflight as f inner join spfli as p
on f~carrid = p~carrid
where f~connid = p~connid
and p~cityfrom = 'frankfurt'
and p~cityto = 'new york'
and f~fldate between '20010910' and '20010920'
and f~seatsocc < f~seatsmax.
write: / date, carrid, connid.
endselect.

note
since not all of the database systems supported by sap use the standard syntax for
on conditions, the syntax has been restricted. it only allows those joins that
produce the same results on all of the supported database systems:

only a table or view may appear to the right of the join operator, not another
join expression.

only and is possible in the on condition as a logical operator.

each comparison in the on condition must contain a field from the right-hand
table.

if an outer join occurs in the from clause, all the on conditions must contain at
least one "real" join condition (a condition that contains a field from tabref1
amd a field from tabref2.

note
in some cases, '*' may be specified in the select clause, and an internal table or
work area is entered into the into clause (instead of a list of fields). if so,
the fields are written to the target area from left to right in the order in which
the tables appear in the from clause, according to the structure of each table
work area. there can then be gaps between table work areas if you use an alignment
request. for this reason, you should define the target work area with reference to
the types of the database tables, not simply by counting the total number of
fields. for an example, see below:

variant 3
... from tabref1 left outer join tabref2 on cond
effect
selects the data from the transparent database tables and/or views specified in
tabref1 and tabref2. tabref1 und tabref2 both have either the same form as in
variant 1 or are themselves join expressions. the keyword outer can be omitted.
the database tables or views specified in tabref1 and tabref2 must be recognized
by the abap-dictionary.

in order to determine the result of a select command where the from clause
contains a left outer join, the database system creates a temporary table
containing the lines that meet the on condition. the remaining fields from the
left-hand table (tabref1) are then added to this table, and their corresponding
fields from the right-hand table are filled with zero values. the system then
applies the where condition to the table.

left outer join between table 1 and table 2 where column d in both tables set the
join condition:

table 1 table 2
---- ---- ---- ---- ---- ---- ---- ---- ----
a b c d d e f g h
---- ---- ---- ---- ---- ---- ---- ---- ----
a1 b1 c1 1 1 e1 f1 g1 h1
a2 b2 c2 1 3 e2 f2 g2 h2
a3 b3 c3 2 4 e3 f3 g3 h3
a4 b4 c4 3 ---- ---- ---- ---- ----
---- ---- ---- ----

\ /
\ /
\ /
\ /
\/
left outer join
---- ---- ---- ---- ---- ---- ---- ---- ----
a b c d d e f g h
---- ---- ---- ---- ---- ---- ---- ---- ----
a1 b1 c1 1 1 e1 f1 g1 h1
a2 b2 c2 1 1 e1 f1 g1 h1
a3 b3 c3 2 null null null null null
a4 b4 c4 3 3 e2 f2 g2 h2
---- ---- ---- ---- ---- ---- ---- ---- ----

example
output a list of all custimers with their bookings for october 15th, 2001:

data: customer type scustom,


booking type sbook.

select scustom~name scustom~postcode scustom~city


sbook~fldate sbook~carrid sbook~connid sbook~bookid
into (customer-name, customer-postcode, customer-city,
booking-fldate, booking-carrid, booking-connid,
booking-bookid)
from scustom left outer join sbook
on scustom~id = sbook~customid and
sbook~fldate = '20011015'
order by scustom~name sbook~fldate.
write: / customer-name, customer-postcode, customer-city,
booking-fldate, booking-carrid, booking-connid,
booking-bookid.
endselect.

if there are columns with the same name in both tables, you must distinguish
between them by prefixing the field descriptor with the table name or using an
alias.

note
for the resulting set of a select command with a left outer join in the from
clause, it is generally of crucial importance whether a logical condition is in
the on or where condition. since not all of the database systems supported by sap
themselves support the standard syntax and semantics of the left outer join, the
syntax has been restricted to those cases that return the same solution in all
database systems:

only a table or view may come after the join operator, not another join statement.

the only logical operator allowed in the on condition is and.

each comparison in the on condition must contain a field from the right-hand
table.

comparisons in the where condition must not contain a field from the right-hand
table.

the on condition must contain at least one "real" join condition (a condition in
which a field from tabref1 as well as from tabref2 occurs).

note
in some cases, '*' may be specivied as the field list in the select clause, and an
internal table or work area is entered in the into clause (instead of a list of
fields). if so, the fields are written to the target area from left to right in
the order in which the tables appear in the llen in der from clause, according to
the structure of each table work area. there can be gaps between the table work
areas if you use an alignment request. for this reason, you should define the
target work area with reference to the types of the database tables, as in the
following example (not simply by counting the total number of fields).

example
example of a join with more than two tables: select all flights from frankfurt to
new york between september 10th and 20th, 2001 where there are available places,
and display the name of the airline.

data: begin of wa,


flight type sflight,
pfli type spfli,
carr type scarr,
end of wa.

select * into wa
from ( sflight as f inner join spfli as p
on f~carrid = p~carrid and
f~connid = p~connid )
inner join scarr as c
on f~carrid = c~carrid
where p~cityfrom = 'frankfurt'
and p~cityto = 'new york'
and f~fldate between '20010910' and '20010920'
and f~seatsocc < f~seatsmax.
write: / wa-carr-carrname, wa-flight-fldate, wa-flight-carrid,
wa-flight-connid.
endselect.

--------------------------------------------------------------------------------

syntax
... [(] {dbtab_left as tabalias_left} | join
{inner join}|{left outer join}
{dbtab_right as tabalias_right on join_cond} [)] ... .

effect
the join syntax represents a recursively nestable join expression. a join
expression consists of a left-hand and a right- hand side, which are joined either
by means of inner join or left outer join . depending on the type of join, a join
expression can be either an inner ( inner) or an outer (left outer) join. every
join expression can be enclosed in round brackets. if a join expression is used,
the select command circumvents sap buffering.

on the left-hand side, either a single database table, a view dbtab_left, or a


join expression join can be specified. on the right-hand side, a single database
table or a view dbtab_right as well as join conditions join_cond can be specified
after on. in this way, a maximum of 24 join expressions that join 25 database
tables or views with each other can be specified after from.

as can be used to specify an alternative table name tabalias for each of the
specified database table names or for every view. a database table or a view can
occur multiple times within a join expression and, in this case, have various
alternative names.

the syntax of the join conditions join_cond is the same as that of the sql_cond
conditions after the addition where, with the following differences:

at least one comparison must be specified after on.

individual comparisons may be joined using and only.

all comparisons must contain a column in the database table or the view
dbtab_right on the right-hand side as an operand.

the following language elements may not be used: between, like, in.

no sub-queries may be used.

for outer joins, only equality comparisons (=, eq) are possible.

if an outer join occurs after from, the join condition of every join expression
must contain at least one comparison between columns on the left-hand and the
right-hand side.

in outer joins, all comparisons that contain columns as operands in the database
table or the view dbtab_right on the right-hand side must be specified in the
corresponding join condition. in the where condition of the same select command,
these columns are not allowed as operands.

resulting set for inner join

the inner join joins the columns of every selected line on the left- hand side
with the columns of all lines on the right-hand side that jointly fulfil the
join_cond condition. a line in the resulting set is created for every such line on
the right-hand side. the content of the column on the left-hand side may be
duplicated in this case. if none of the lines on the right-hand side fulfils the
join_cond condition, no line is created in the resulting set.

resulting set for outer join

the outer join basically creates the same resulting set as the inner join, with
the difference that at least one line is created in the resulting set for every
selected line on the left-hand side, even if no line on the right-hand side
fulfils the join_cond condition. the columns on the right-hand side that do not
fulfil the join_cond condition are filled with null values.

example
join the columns carrname, connid, fldate of the database tables scarr, spfli and
sflight by means of two inner joins. a list is created of the flights from
p_cityfr to p_cityto. alternative names are used for every table.

parameters: p_cityfr type spfli-cityfrom,


p_cityto type spfli-cityto.

data: begin of wa,


fldate type sflight-fldate,
carrname type scarr-carrname,
connid type spfli-connid,
end of wa.

data itab like sorted table of wa


with unique key fldate carrname connid.

select c~carrname p~connid f~fldate


into corresponding fields of table itab
from ( ( scarr as c
inner join spfli as p on p~carrid = c~carrid
and p~cityfrom = p_cityfr
and p~cityto = p_cityto )
inner join sflight as f on f~carrid = p~carrid
and f~connid = p~connid ).

loop at itab into wa.


write: / wa-fldate, wa-carrname, wa-connid.
endloop.

example
join the columns carrid, carrname and connid of the database tables scarr and
spfli using an outer join. the column connid is set to the null value for all
flights that do not fly from p_cityfr. this null value is then converted to the
appropriate initial value when it is transferred to the assigned data object. the
loop returns all airlines that do not fly from p_cityfr.

parameters p_cityfr type spfli-cityfrom.

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 s~carrid s~carrname p~connid


into corresponding fields of table itab
from scarr as s
left outer join spfli as p on s~carrid = p~carrid
and p~cityfrom = p_cityfr.

loop at itab into wa.


if wa-connid = '0000'.
write: / wa-carrid, wa-carrname.
endif.
endloop.

Você também pode gostar