简体   繁体   中英

Select field from subquery

Can we have in a select query a field from a sub-query? Like

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

There is no simple ABAP SQL syntax as you propose, but there are the following workarounds.

Since 7.51, you may use 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).

Before 7.51, you need to use several statements (here I use a short syntax valid only in 7.50 (because of the two Host Expressions), but you may adapt the code rather easily for earlier versions):

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 '*'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM