继续我们的数据库操作语句讲解:From
2. from详解:
* From dbtab :从数据库表或者视图中选取数据。数据库表名称直接在abap中使用,必须能够被abap字典识别。
* From dbtab [as xxx]: 定义数据表的别名,选择数据。
* From “table1” join “table2” on “condition”
= From “table1” inner join “table2” on “condition” : table1和table2做内联,大家可以根据下面的例子了解什么是内联接:
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 |
|----|----|----|----|----|----|----|----|----|
程序实例:
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.
* From “table1” left join “table2” on “condition”
= From “table1” left outer join “table2” on “condition” :table1和table2做外联,大家可以根据下面的例子了解什么是外联接:
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 |
|----|----|----|----|----|----|----|----|----|
* From (sorce_text):动态编程时候使用,同select 的(source_text)使用。
CONSTANTS: flight_tab_name(30) VALUE 'SPFLI'.
DATA: from_clause TYPE STRING.
DATA: BEGIN OF wa,
name(20) TYPE C,
connid TYPE spfli-connid,
END OF wa.
CONCATENATE flight_tab_name ' AS t1'
' JOIN scarr AS t2 ON t1~carrid = t2~carrid'
INTO from_clause.
SELECT t1~connid t2~carrname AS name
FROM (from_clause)
INTO CORRESPONDING FIELDS OF wa.
WRITE: / wa-name, wa-connid.
ENDSELECT.
几个附加选项:
…client specified:不考虑集团,把数据选取出来。
…bypassing buffer:直接从数据库选取数据,不使用sap buffer。
…up to “n” rows:选择的数据限制在n行。