OrdinaryPerson
OrdinaryPerson

Reputation: 67

Modify certain BSEG fields from customary structured table

I'm trying to use following:

update bseg from zbseg

where tables are not from same length (ZBSEG is reduced version of BSEG).

Whole idea is that BSEG is just an example, I have a loop where all cluster tables will be iterated, so everything should be dynamically.

Table data from cluster is reduced to only several fields and copied to transparent table (data dictionary in new transparent table has primary keys + only few of the field of cluster) and afterwards data in DB will be modified and copied back via UPDATE to the cluster.

update bseg from zbseg

this statement updates the field values from ZBSEG but for the rest will not keep old values but rather puts initial values.

I've tried even that:

SELECT *
FROM bseg
INTO TABLE gt_bseg.

SELECT mandt bukrs belnr gjahr buzei buzid augdt
FROM zbseg
INTO CORRESPONDING FIELDS OF TABLE gt_bseg.

but it still overlaps those fields that are not considered in zbseg.

Any statement that will update only certain range of fields extracted from ZBSEG not touching other BSEG fields?

IMAGE: Looping over original full length and short table

Upvotes: 0

Views: 2364

Answers (3)

Suncatcher
Suncatcher

Reputation: 10621

Here is piece of code you can use for your task. It is based on dynamic UPDATE statement which allows updating only certain fields:

DATA: handle      TYPE REF TO data,
      lref_struct TYPE REF TO cl_abap_structdescr,
      source      TYPE string,
      columns     TYPE string,
      keys        TYPE string,
      cond        TYPE string,
      sets        TYPE string.

SELECT tabname FROM dd02l INTO TABLE @DATA(clusters) WHERE tabclass = 'CLUSTER'.

LOOP AT clusters ASSIGNING FIELD-SYMBOL(<cluster>).
  lref_struct ?= cl_abap_structdescr=>describe_by_name( <cluster>-tabname ).
  source = 'Z' && <cluster>-tabname. " name of your ZBSEG-like table
* get key fields
  DATA(key_fields) = VALUE ddfields( FOR line IN lref_struct->get_ddic_field_list( ) 
                                     WHERE ( keyflag NE space ) ( line ) ).
  lref_struct ?= cl_abap_structdescr=>describe_by_name( source ).
* get all fields from source reduced table
  DATA(fields) = VALUE ddfields( FOR line IN lref_struct->get_ddic_field_list( ) ( line ) ).

* filling SELECT fields and SET clause
  LOOP AT fields ASSIGNING FIELD-SYMBOL(<field>).
    AT FIRST.
      columns = <field>-fieldname.
      CONTINUE.
    ENDAT.
    CONCATENATE columns <field>-fieldname INTO columns SEPARATED BY `, `.
    IF NOT line_exists( key_fields[ fieldname = <field>-fieldname ] ).
      IF sets IS INITIAL.
        sets = <field>-fieldname && ` = @<fsym_wa>-` && <field>-fieldname.
      ELSE.
        sets = sets && `, ` && <field>-fieldname && ` = @<fsym_wa>-` && <field>-fieldname.
      ENDIF.
    ENDIF.
  ENDLOOP.

* filling key fields and conditions
  LOOP AT key_fields ASSIGNING <field>.
    AT FIRST.
      keys = <field>-fieldname.
      CONTINUE.
    ENDAT.
    CONCATENATE keys <field>-fieldname INTO keys SEPARATED BY `, `.
    IF cond IS INITIAL.
      cond = <field>-fieldname && ` = @<fsym_wa>-` && <field>-fieldname.
    ELSE.
      cond = cond && ` AND ` && <field>-fieldname && ` = @<fsym_wa>-` && <field>-fieldname.
    ENDIF.
  ENDLOOP.

* constructing reduced table type
  lref_struct ?= cl_abap_typedescr=>describe_by_name( source ).
  CREATE DATA handle TYPE HANDLE lref_struct.
  ASSIGN handle->*  TO FIELD-SYMBOL(<fsym_wa>).

* updating result cluster table
  SELECT (columns)
    FROM (source)
    INTO @<fsym_wa>.
  UPDATE (<cluster>-tabname)
  SET (sets)
  WHERE (cond).
  ENDSELECT.

ENDLOOP.

This piece selects all cluster tables from DD02L and makes an assumption you have reduced DB table prefixed with Z for each target cluster table. E.g. ZBSEG for BSEG, ZBSET for BSET, ZKONV for KONV and so on.

Tables are updated by primary key which must be included in reduced table. The fields to be updated are taken from reduced table as all fields excluding key fields, because primary key is prohibited for update.

Upvotes: 0

AZn5ReD
AZn5ReD

Reputation: 1

You could try to use the MODIFY statement to update the tables.

An other way to do it would be to use the cl_abap_typedescr to get the fields of each table and compare them for the update.

Here is an example of how to get the fields.

DATA : ref_table_des TYPE REF TO cl_abap_structdescr,
       columns TYPE abap_compdescr_tab.

ref_table_des ?= cl_abap_typedescr=>describe_by_data( struc ).
columns = ref_table_des->components[].

Upvotes: -1

mkysoft
mkysoft

Reputation: 5758

I think you need get records from zbseg with limit because of there will be exists million records then get them from bseg one by one and update it, then remove or update flags of it from zbseg for performance.

tables: BSEG, ZBSEG.

data: GT_ZBSEG like ZBSEG occurs 1 with header line,
      GS_BSEG type BSEG.

select *
into table GT_ZBSEG up to 1000 rows
from ZBSEG.

check SY-SUBRC is initial.
check SY-DBCNT is not initial.

loop at GT_ZBSEG.
  select single * from BSEG into GS_BSEG
  where BSEG~MANDT = GT_ZBSEG-MANDT
    and BSEG~BUKRS = GT_ZBSEG-BUKRS
    and BSEG~BELNR = GT_ZBSEG-BELNR
    and BSEG~GJAHR = GT_ZBSEG-GJAHR
    and BSEG~BUZEI = GT_ZBSEG-BUZEI.
  if SY-SUBRC ne 0.
    message E208(00) with 'Record not found!'.
  endif.
  if GS_BSEG-BUZID ne GT_ZBSEG-BUZID
  or GS_BSEG-AUGDT ne GT_ZBSEG-AUGDT.
    move-corresponding GT_ZBSEG to GS_BSEG.
    update BSEG from GS_BSEG.
  endif.
  " delete same records and transfered
  delete ZBSEG from GT_ZBSEG.
endloop.

Upvotes: 2

Related Questions