简体   繁体   English

对为 4 个内部连接外部表创建的内部表使用字段符号

[英]Using Field-Symbols for the internal table that was created for 4 inner joined external tables

Allegedly use of field-symbols might result in better performance, however I have very scarce experiences with pointers and don't quite understand the concept of pointers.据称使用字段符号可能会导致更好的性能,但是我对指针的经验非常少,并且不太了解指针的概念。

I have this program which selects carrid , connid , fldate , bookid and travel agency's name from the table created by INNER JOIN of tables spfli , sflight , sbook and travelag , based on 2 user inputs ( parameters ):我有这个程序,它根据 2 个用户输入( parameters )从表spflisflightsbooktravelagINNER JOIN创建的表中选择carridconnidfldatebookid和旅行社的name

  • cityFrom
  • cityTo

Basically, I want the user to insert from where he wants to take off and in which city he wants to land.基本上,我希望用户从他想要起飞的地方和他想要降落的城市插入。 After that the program displays all flights that are possible to book.之后,程序会显示所有可以预订的航班。

Here is my code (works fine), that I would like to refactor by using FIELD-SYMBOLS:这是我的代码(工作正常),我想使用 FIELD-SYMBOLS 重构它:

TABLES spfli.

DATA: BEGIN OF wa,
        carrid TYPE spfli-carrid,
        connid TYPE spfli-connid,
        fldate TYPE sflight-fldate,
        bookid TYPE sbook-bookid,
        name TYPE stravelag-name,
      END OF wa,
      itab LIKE SORTED TABLE OF wa
                WITH UNIQUE KEY carrid connid fldate bookid.

PARAMETERS: fromLoc TYPE spfli-cityfrom,
            toLoc TYPE spfli-cityto.

SELECT  p~carrid p~connid f~fldate b~bookid a~name
  INTO  CORRESPONDING FIELDS OF TABLE itab
  FROM  ( ( (
          spfli AS p
          INNER JOIN sflight AS f ON p~carrid = f~carrid AND
                                     p~connid = f~connid
        )
          INNER JOIN sbook AS b ON b~carrid = f~carrid AND
                                   b~connid = f~connid AND
                                   b~fldate = f~fldate
        ) 
          INNER JOIN stravelag AS a ON b~agencynum = a~agencynum
        )
  WHERE p~cityfrom = fromLoc AND
        p~cityto   = toLoc  AND
        f~seatsmax > f~seatsocc.

LOOP AT itab INTO wa.
  AT NEW fldate.
    ULINE.
    WRITE: / .
    WRITE: 'FLIGHT' COLOR 4.
    ULINE.
    WRITE: / wa-carrid COLOR 3, wa-connid COLOR 3, wa-fldate COLOR 3.
    WRITE: / .
    WRITE: 'Booking ID' COLOR 3, 30 'Carrier ID' COLOR 3, 60 'Air Connection ID' COLOR 3, 90 'Travel Agency' COLOR 3.
    ULINE.
ENDAT.
  WRITE: / wa-bookid, 30 wa-carrid, 60 wa-connid, 90 wa-name.
ENDLOOP.

I started by declaration of field-symbols I thought I will need and then I assigned them to tables.我首先声明了我认为需要的字段符号,然后将它们分配给表。 After that i tried to declare PARAMETERS and specify their type based on newly created field-symbols, but it did not work - program says: "Type <FS_SPFLI>" is unknown.之后,我尝试声明PARAMETERS并根据新创建的字段符号指定它们的类型,但它不起作用 - 程序说: "Type <FS_SPFLI>" is unknown.

DATA: BEGIN OF wa,
        carrid TYPE spfli-carrid,
        connid TYPE spfli-connid,
        fldate TYPE sflight-fldate,
        bookid TYPE sbook-bookid,
        name TYPE stravelag-name,
      END OF wa,
      itab LIKE SORTED TABLE OF wa
                WITH UNIQUE KEY carrid connid fldate bookid.

FIELD-SYMBOLS: <fs_spfli> TYPE any,
               <fs_wa> TYPE any.

ASSIGN: spfli TO <fs_spfli>,
        wa TO <fs_wa>.

PARAMETERS: fromLoc TYPE <fs_spfli>-cityfrom,
            toLoc TYPE <fs_spfli>-cityto.

I would appreciate a guidance trough how to make my program work with field-symbols我很感激如何使我的程序与字段符号一起工作的指导

A field symbol is not a type, it's a kind of alias to the data object (variable) assigned at runtime (it's a kind of reference variable). 字段符号不是类型,它是在运行时分配的数据对象(变量)的一种别名(它是一种引用变量)。

So, you can't write:所以,你不能写:

PARAMETERS: fromLoc TYPE <fs_spfli>-cityfrom,
            toLoc TYPE <fs_spfli>-cityto.

Instead, use the actual type (SPFLI is a type defined in the ABAP Dictionary, via the transaction code SE11 ) :相反,使用实际类型(SPFLI 是在 ABAP 字典中定义的类型,通过事务代码SE11 ):

PARAMETERS: fromLoc TYPE spfli-cityfrom,
            toLoc TYPE spfli-cityto.

A field symbol should be typed the most precisely possible rather than generically.字段符号应该尽可能精确地输入,而不是通用的。

In your case, instead of using the generic ANY type, you should write (here, I use LIKE variable , because there's no "standalone type" explicitly defined):在您的情况下,您应该编写(在这里,我使用LIKE variable ,因为没有明确定义的“独立类型”),而不是使用泛型 ANY 类型:

FIELD-SYMBOLS: <fs_spfli> LIKE wa.

NB: no need to prefix the field symbols with fs , they are identified clearly thanks to the angle brackets ( <spfli> ).注意:不需要用fs前缀字段符号,由于尖括号( <spfli> )可以清楚地识别它们。

A field symbol may be assigned by using one of these statements:可以使用以下语句之一分配字段符号:

  • ASSIGN ... TO ... 分配……给……
  • LOOP AT ... ASSIGNING ... 循环在...分配...
  • READ TABLE ... ASSIGNING ...阅读表格...分配...
  • APPEND ... TO ... ASSIGNING附加......到......分配
  • INSERT ... INTO ... ASSIGNING INSERT ... INTO ... 分配
  • MODIFY ... FROM ... ASSIGNING修改...从...分配

A field symbol may be assigned by using this statement:可以使用以下语句分配字段符号:

  • UNASSIGN取消签名

You declared your field symbols as type ANY , this is like void * in C++ (pointing into the void, the compiler expects that someting will begin there but has no knowledge what it is exactly).您将字段符号声明为ANY类型,这就像 C++ 中的void * (指向 void,编译器期望某些内容将从那里开始,但不知道它究竟是什么)。 There is absolutely no way for the compiler to know that those field symbols have in fact type spfli or the one of wa until the run time.编译器绝对无法知道这些字段符号实际上是spfli类型还是wa类型,直到运行时为止。

This one would compile.这个会编译。

FIELD-SYMBOLS: <fs_spfli> TYPE spfli,
               <fs_wa> LIKE wa.

ASSIGN: spfli TO <fs_spfli>,
        wa TO <fs_wa>.

PARAMETERS: fromLoc LIKE <fs_spfli>-cityfrom,
            toLoc LIKE <fs_spfli>-cityto.

And here the full compilable program.这里是完整的可编译程序。

REPORT ZZZ.

TABLES spfli.

DATA: BEGIN OF wa,
        carrid TYPE spfli-carrid,
        connid TYPE spfli-connid,
        fldate TYPE sflight-fldate,
        bookid TYPE sbook-bookid,
        name TYPE stravelag-name,
      END OF wa,
      itab LIKE SORTED TABLE OF wa
                WITH UNIQUE KEY carrid connid fldate bookid.

FIELD-SYMBOLS: <fs_spfli> TYPE spfli,
               <fs_wa> LIKE wa.

ASSIGN: spfli TO <fs_spfli>,
        wa TO <fs_wa>.

PARAMETERS: fromLoc LIKE <fs_spfli>-cityfrom,
            toLoc LIKE <fs_spfli>-cityto.

SELECT  p~carrid p~connid f~fldate b~bookid a~name
  INTO  CORRESPONDING FIELDS OF TABLE itab
  FROM  ( ( (
          spfli AS p
          INNER JOIN sflight AS f ON p~carrid = f~carrid AND
                                     p~connid = f~connid
        )
          INNER JOIN sbook AS b ON b~carrid = f~carrid AND
                                   b~connid = f~connid AND
                                   b~fldate = f~fldate
        )
          INNER JOIN stravelag AS a ON b~agencynum = a~agencynum
        )
  WHERE p~cityfrom = fromLoc AND
        p~cityto   = toLoc  AND
        f~seatsmax > f~seatsocc.

LOOP AT itab INTO wa.
  AT NEW fldate.
    ULINE.
    WRITE: / .
    WRITE: 'FLIGHT' COLOR 4.
    ULINE.
    WRITE: / wa-carrid COLOR 3, wa-connid COLOR 3, wa-fldate COLOR 3.
    WRITE: / .
    WRITE: 'Booking ID' COLOR 3, 30 'Carrier ID' COLOR 3, 60 'Air Connection ID' COLOR 3, 90 'Travel Agency' COLOR 3.
    ULINE.
ENDAT.
  WRITE: / wa-bookid, 30 wa-carrid, 60 wa-connid, 90 wa-name.
ENDLOOP.

If you are on 4.70 or higher you can write.如果您使用的是 4.70 或更高版本,则可以编写。

LOOP AT itab ASSIGNING FIELD-SYMBOL(<wa>).
  WRITE: / <wa>-bookid, 30 <wa>-carrid, 60 <wa>-connid, 90 <wa>-name.
ENDLOOP.

Then ABAP will take care of proper typing on its own.然后 ABAP 将自行处理正确的输入。 If you do not understand pointers then you may pretend that a field-symbol is just an alias for the structure it is pointing at.如果您不理解指针,那么您可能会假装字段符号只是它指向的结构的别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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