从子查询中选择字段

时间:2019-06-06 21:14:05

标签: sap abap

我们可以在选择查询中包含子查询中的字段吗?喜欢

select sp~carrid, sp~connid,
       carrname = ( select CARRNAME from scarr as sc )
into TABLE @data(it_flight)
FROM spfli as sp.

1 个答案:

答案 0 :(得分:2)

您没有提议简单的ABAP SQL语法,但是有以下变通办法。

从7.51开始,您可以使用Common Table Expressions

WITH
  +carrname_af AS (
      SELECT carrname FROM scarr WHERE carrid = 'AF' ),
  +carrname_lh AS (
      SELECT carrname FROM scarr WHERE carrid = 'LH' )
  SELECT sp~carrid, sp~connid, af~carrname AS carrname_af,
        lh~carrname AS carrname_lh
      FROM spfli AS sp
        CROSS JOIN +carrname_af AS af
        CROSS JOIN +carrname_lh AS lh
      INTO TABLE @data(it_flight).

在7.51之前,您需要使用多个语句(这里使用的简短语法仅在7.50中有效(由于两个Host Expressions),但是您可以很轻松地将代码修改为较早的版本):

SELECT sp~carrid, sp~connid, @( VALUE s_carrname( ) ) AS carrname_af,
      @( VALUE s_carrname( ) ) AS carrname_lh
    FROM spfli AS sp
    INTO TABLE @data(it_flight).

" Now initialize carrname_af and carrname_lh of IT_FLIGHT 
SELECT SINGLE carrname FROM scarr INTO @data(carrname_af)
    WHERE carrid = 'AF'.
SELECT SINGLE carrname FROM scarr INTO @data(carrname_lh)
    WHERE carrid = 'LH'.

DATA ls_flight LIKE LINE OF it_flight.
ls_flight = VALUE #( carrname_af = carrname_af carrname_lh = carrname_lh ).
MODIFY it_flight FROM ls_flight TRANSPORTING carrname_af carrname_lh
    WHERE carrid CP '*'.