简体   繁体   English

从另一个 itab 填充 itab 值

[英]Filling itab value from another itab

I have an internal table itab which has some rows that one component of the rows matnr is empty.我有一个内部表itab ,它有一些行,其中行matnr一个组成部分是空的。 I want to check the palet column of the internal table and get matnr according to palet from the zlldet table, and then change the matnr column of internal table to new matnr value.我要检查的palet内部表的列,并得到matnr根据paletzlldet表,然后将更改matnr内部表的列到新matnr值。 Here is a summary;这是一个总结;

itab table: itab表:

+-------+-------+-----------+
| palet | matnr | something |
+-------+-------+-----------+
|  1234 | null  | abc       |
|  1235 | null  | saa       |
|  1236 | null  | ssd       |
+-------+-------+-----------+

So, I will check the palet column from the zlldet table and find the matnr value of that row.因此,我将检查zlldet表中的palet列并找到该行的matnr值。 The new itab values should be:新的itab值应该是:

+-------+--------+-----------+
| palet | matnr  | something |
+-------+--------+-----------+
|  1234 | 543213 | abc       |
|  1235 | 988876 | saa       |
|  1236 | 344545 | ssd       |
+-------+--------+-----------+

What I've tried:我试过的:

LOOP AT itab.
  SELECT SINGLE matnr INTO itab-matnr
    FROM zlldet WHERE palet = itab-palet.
  ENDIF.
ENDLOOP.

I am trying to change the value in the second line.我正在尝试更改第二行中的值。 I know it is wrong, I should use some MODIFY statement but I'm not sure how.我知道这是错误的,我应该使用一些MODIFY语句,但我不确定如何使用。

Edit:编辑:

Here is the complete code:这是完整的代码:

REPORT zwps0510 MESSAGE-ID zc LINE-SIZE 255 NO STANDARD PAGE HEADING.
TABLES: zzllog, zzldet, marc, makt.
DATA: BEGIN OF itab OCCURS 0.
    INCLUDE STRUCTURE zzllog.
DATA: END OF itab.

DATA: adet TYPE p.
*/ paket numaraları
SELECT-OPTIONS: spalet FOR zzllog-palet.      "/paket no
SELECT-OPTIONS: spaleta FOR zzllog-paleta.    "/paketnoa
SELECT-OPTIONS: spaletb FOR zzllog-paletb.    "/paketnob
SELECTION-SCREEN SKIP 1.
*/ paketle ilgili bilgiler
SELECT-OPTIONS: sdispo FOR marc-dispo.
SELECT-OPTIONS: smatnr FOR zzllog-matnr.
SELECT-OPTIONS: sharkod FOR zzllog-harkod.    "/logtaki işlem kodu
*SELECT-OPTIONS: stelf1 FOR zzllog-telf1.
SELECT-OPTIONS: starih FOR zzllog-tarih.
SELECT-OPTIONS: susr FOR zzllog-usr.
SELECT-OPTIONS: saufnr FOR zzllog-aufnr.
SELECTION-SCREEN SKIP 1.
*/ depo adres bilgileri
SELECT-OPTIONS: sflgnum FOR zzllog-flgnum.    "/kaynak depo no
SELECT-OPTIONS: sflgtyp FOR zzllog-flgtyp.    "/kaynak depo tipi
SELECT-OPTIONS: sflgpla FOR zzllog-flgpla.    "/kaynak depo numarası
SELECT-OPTIONS: stlgnum FOR zzllog-tlgnum.    "/hedef depo no
SELECT-OPTIONS: stlgtyp FOR zzllog-tlgtyp.    "/hedef depo tipi
SELECT-OPTIONS: stlgpla FOR zzllog-tlgpla.    "/hedef depo numarası
SELECTION-SCREEN SKIP 1.
*/ Çıktıda gelecek kolonların seçimi
SELECTION-SCREEN BEGIN OF BLOCK uc WITH FRAME TITLE text-001.
PARAMETERS : tarih AS CHECKBOX DEFAULT 'X',
         saat AS CHECKBOX DEFAULT 'X',
         usr AS CHECKBOX DEFAULT 'X',
         palet AS CHECKBOX DEFAULT 'X',
         paleta AS CHECKBOX DEFAULT 'X',
         paletb AS CHECKBOX DEFAULT 'X',
         harkod AS CHECKBOX DEFAULT 'X',  "/ hareket kodu
         matnr AS CHECKBOX DEFAULT 'X',
         menge AS CHECKBOX DEFAULT 'X',
         sebep AS CHECKBOX DEFAULT 'X',  "/ sipariş/red
         aufnr AS CHECKBOX DEFAULT 'X',  "/sm.siparişi
         ref2 AS CHECKBOX DEFAULT 'X',
         paufnr AS CHECKBOX DEFAULT 'X', "/enj.siparişi
         flgnum AS CHECKBOX DEFAULT 'X', "/from depo...
         flgtyp AS CHECKBOX DEFAULT 'X',
         flgpla AS CHECKBOX DEFAULT 'X',
         tlgnum AS CHECKBOX DEFAULT 'X', "/ to depo...
         tlgtyp AS CHECKBOX DEFAULT 'X',
         tlgpla AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN END OF BLOCK uc.
*
TOP-OF-PAGE.
   PERFORM baslik.
*
START-OF-SELECTION.
  PERFORM prepare_itab.
  PERFORM write_itab.
**
FORM prepare_itab.
*/ Paketle ilgili tüm geçmiş hareketler log tablosundan okunur
  SELECT * INTO CORRESPONDING FIELDS OF TABLE itab
      FROM zzllog WHERE palet IN spalet
                    AND paleta IN spaleta
                    AND paletb IN spaletb
                    AND matnr IN smatnr
                    AND harkod IN sharkod
                    AND tarih IN starih
                    AND flgnum IN sflgnum
                    AND flgtyp IN sflgtyp
                    AND flgpla IN sflgpla
                    AND tlgnum IN stlgnum
                    AND tlgtyp IN stlgtyp
                    AND tlgpla IN stlgpla
                    AND aufnr IN saufnr
                    AND usr IN susr.
  IF sdispo[] IS NOT INITIAL.
    LOOP AT itab.
      SELECT SINGLE dispo INTO marc-dispo
                FROM marc WHERE matnr = itab-matnr
                            AND werks = '3001'
                            AND dispo IN sdispo.
      IF sy-subrc <> 0.
        DELETE itab.
      ENDIF.
    ENDLOOP.
  ENDIF.




  DESCRIBE TABLE itab LINES adet.
*/ tüm hareketler tarih ve saate göre sıralanır
 SORT itab BY tarih saat.
 WRITE:/ 'ADET: ', adet.
 ULINE.
   ENDFORM.   

So, what I want to do is exactly: if sharkod includes D , zzldet should be checked for the matnr .所以,我想要做的正是:如果sharkod包括Dzzldet应为被检查matnr

Instead of using MODIFY , use a field symbol:不要使用MODIFY ,而是使用字段符号:

DATA: lt_materials TYPE TABLE OF zzllog.
FIELD-SYMBOLS: <ls_line> TYPE zzllog.

* other code with strange comments :-)

LOOP AT lt_materials ASSIGNING <ls_line> WHERE matnr IS INITIAL.
  SELECT SINGLE MATNR
    FROM zlldet
    INTO <ls_line>-matnr
    WHERE palet = <ls_line>-palet.
ENDLOOP.

This is just an example for the correct syntax.这只是正确语法的示例。 It's not a good idea to do this in a real program because there's a fair chance thtat you'll be hitting the database with thousands of requests, and potentially draw quite a bit of performance.在实际程序中执行此操作并不是一个好主意,因为您很有可能会用数千个请求访问数据库,并可能会降低性能。 It'd be better to preselect the PALET numbers that have no MATNR into a separate table and then use FOR ALL ENTRIES IN to read all the MATNR s in a single query.最好将没有MATNRPALET数字预选到单独的表中,然后使用FOR ALL ENTRIES IN在单个查询中读取所有MATNR

EDIT: Added type names.编辑:添加类型名称。 BTW, your way of declaring the internal table is somewhat outdated...顺便说一句,你声明内部表的方式有点过时了......

When you want to do it with modify and not with a field symbol, that would be the correct solution:当您想使用修改而不是使用字段符号进行操作时,这将是正确的解决方案:

LOOP AT itab.
  SELECT SINGLE matnr INTO itab-matnr
    FROM zlldet WHERE palet = itab-palet.
  MODIFY itab.
ENDLOOP.

You declared itab as a table with header line (which is deprecated, by the way).您将 itab 声明为带有标题行的表(顺便说一下,已弃用)。 That means you have a table itab and a structure itab.这意味着您有一个表 itab 和一个结构 itab。 Which one is used in which situation depends on context, and some commands, like LOOP AT and MODIFY, use both at the same time.在哪种情况下使用哪个取决于上下文,并且某些命令,例如 LOOP AT 和 MODIFY,同时使用这两个命令。 This would be the code with a work area instead of a header line.这将是带有工作区而不是标题行的代码。 It's more readable.它更具可读性。

DATA itab TYPE TABLE OF [something].
DATA wa TYPE [something].

LOOP AT itab INTO wa.  " copies each line into wa
  SELECT SINGLE matnr INTO wa-matnr
    FROM zlldet WHERE palet = itab-palet.
  MODIFY itab FROM wa.  " writes the changed line back to the table
ENDLOOP.

You can also use the solution with a field symbol vwegert described.您还可以使用带有描述的字段符号 vwegert 的解决方案。 A field symbol is a pointer to the line of the table.字段符号是指向表格行的指针。 Any changes to the field symbol will be automatically written back to the table, so you don't need MODIFY in this case.对字段符号的任何更改都将自动写回到表中,因此在这种情况下您不需要 MODIFY。 It is also faster, especially with very wide tables, because no data needs to be copied.它也更快,尤其是对于非常宽的表,因为不需要复制数据。

You can also avoid the select statement every time inside the loop.您还可以避免每次在循环内使用 select 语句。 Instead, you can try making use of Range tables and make use of select statement only once before the loop since it increases performance.相反,您可以尝试使用 Range 表并在循环之前仅使用一次 select 语句,因为它可以提高性能。

Define user defined structure and declare variables定义用户定义结构并声明变量

types: begin of ty_p_m,
         palet type zlldet-palet,
         matnr type zlldet-matnr,
       end of ty_p_m.

data: r_palet type range table of zlldet-palet,
      r_line_palet like line of r_palet,
      i_p_matnr type standard table of ty_p_m,
      wa_p_m type ty_p_m.


field-symbols: <fs> type wa_p_m.

Fill the range table which is to be used in the select query in the next step.填写将在下一步选择查询中使用的范围表。

r_line_palet-sign = 'I'.
r_line_palet-option = 'EQ'.

loop at itab into wa.
r_line_palet-low = wa-palet.
append  r_line_palet to r_palet.
endloop.

now use the select statement by referring the range table and get the data inside the declared internal table.现在通过引用范围表使用 select 语句并获取声明的内部表中的数据。

select palet matnr from zlldet into i_p_matnr
where palet in r_palet.

Now loop on declared internal table.现在循环声明的内部表。 Inside that read your internal table with incoming values and modify the table there itself using field symbols instead of modify statement.在里面读取带有传入值的内部表,并使用字段符号而不是修改语句修改表本身。

loop at i_p_matnr into wa_p_matnr.
read table itab with key palet = wa_p_matnr-palet assigning <fs>.
if sy-subrc = 0.
<fs>-matnr = wa_p_matnr-matnr.
endif.
clear wa_p_matnr.
endloop.

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

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