Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Hi everyone

Former Member
0 Kudos

i need to delete a record from an internal table comparing multiple fields .

Please let me know the syntax.

any help would be greatly appreciated.

Thanks in advance

suchitra

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Thanks for ur time .

But my problem is that i have a record from database table which i need to compare with record in internal table and delete it.

mind u all the fields in internal table are not available in database so i am not able to use

delete itab key field because -


i need to use this statement but iam not sure

delete itab where f1= db-f1

f2 = db-f2..

i dont whether the syntax is right or wrong because in comparision can we use multiple fields..

please let me know

suchitra

15 REPLIES 15

Former Member
0 Kudos

loop at itab where fld1 = <value> and fld2 = <value>.

endloop.

Former Member
0 Kudos

HI SUCHITRA,

DELETE itab.

... COMPARING f1 f2 ...

Effect

Two lines of the internal table itab are regarded as duplicates if they have identical contents in the fields f1, f2, ...

... COMPARING ALL FIELDS

Effect

Two lines of the internal table are regarded as duplicates if all of their field contents are identical.

Notes

The DELETE ADJACENT DUPLICATES statement works particularly well if you have sorted the internal table itab according to the fields that you want to compare when looking for duplicates. In this case, deleting adjacent duplicates is the same as deleting all duplicates. The direction of the sort is irrelevant.

If you do not know a comparison expression until runtime, you can specify it dynamically as the contents of the field name in the expression COMPARING ... (name) .... If name is empty at runtime, the comparison expression is ignored. If name contains an invalid component name, a runtime error occurs.

Note

Performance:

When you delete a line of an internal table, index maintenance costs are incurred. These depend on the index of the table. The runtime is independent of the width of the table line.

Deleting a line from the middle of an internal table with 200 entries usually requires around 10 msn (standardized microseconds).

When you delete a set of entries using "DELETE itab FROM idx1 TO idx2." or "DELETE itab WHERE ...", index maintenance costs are only incurred once. This means that it is considerably more efficient than deleting entries in a LOOP.

If you want to delete adjacent duplicate entries from an internal table, use the variant " DELETE ADJACENT DUPLICATES FROM itab." instead of a LOOP construction.

HOPE THIS HELPS,

DO REWARD IF IT HELPS,

PRIYA.

Former Member
0 Kudos

DELETE - Deleting from an Internal Table

Variants:

1. DELETE itab.

2. DELETE TABLE itab WITH TABLE KEY k1 = v1 ... kn = vn.

3. DELETE TABLE itab [FROM wa].

4. DELETE itab INDEX idx.

5. DELETE itab FROM idx1 TO idx2.

6. DELETE itab WHERE logexp.

7. DELETE ADJACENT DUPLICATES FROM itab.

See Short forms of line operations not allowed.

Effect

Deletes one or more lines from an internal table.

Note

If you delete lines within a LOOP ... ENDLOOP block, the deletion affects subsequent loop passes.

Variant 1

DELETE itab.

Effect

Deletes the current entry from an internal table in a LOOP. You can only use this variant with index tables (standard or sorted tables).

Return Code is set to 0.

Note

Once you have deleted the current entry of an internal table in a LOOP, the effect of subsequent changes to the current entry without specifying the INDEX cannot be guaranteed, and the behavior may change in future releases.

Variant 2

DELETE TABLE itab WITH TABLE KEY k1 = v1 ... kn = vn.

Effect

Deletes generically from an internal table. You can use this variant for any tables. The variant is a single command that deletes the table line with the key values k1 = v1 ....kn = vn. If there is more than one line with the specified key, only the first one is deleted. The table key must be specified fully. If the type of a value and the type of the associated key are not compatible, the system converts the value into the type of the component before the statement is executed, using MOVE logic. If a component is not known until runtime, you can specify it dynamically as the contents of the field fi in the expression WITH TABLE KEY ... (fi) = vi ....

The costs for deleting one entry depend on the table type with which the table is defined:

STANDARD TABLE:

The system starts searching for the entry at the start of the table. This means that generic deletion has linear costs.

SORTED TABLE:

The system searches for the entry using the table key. Generic deletion for tables of this type has a logarithmic cost.

HASHED TABLE:

The system uses the hash algorithm and the table key to locate the entry. The time required is constant. Tables with deleted entries require a preceding/following administration. The first deletion operation has to set this up, and therefore has linear costs. After the deletion, the space required for each table entry increases by 8 bytes.

The Return Code is set as follows:

SY-SUBRC = 0:

Entry deleted.

SY-SUBRC = 4:

Entry did not exist.

Variant 3

DELETE TABLE itab [FROM wa].

Effect

Behaves similarly to variant 2. The values for the table key are taken from the corresponding components of the (structured) field wa. The field must be compatible with the table line of itab. This method allows you to delete from a table without the table key having to be known in advance. If the internal table has a header line, you can leave out the FROM wa addition; the system then takes the key values from the header line.

Variant 4

DELETE itab INDEX idx.

Effect

Deletes the idx-th entry of the internal table itab. This variant is only allowed with index tables (standard or sorted tables).

The Return Code is set as follows:

SY-SUBRC = 0:

Entry deleted.

SY-SUBRC = 4:

Entry did not exist.

Variant 5

DELETE itab FROM idx1 TO idx2.

Effect

Deletes the range of lines from index idx1 to idx2 from the internal table itab. You can only use this variant with index tables (standard or sorted tables). You must specify at least one of the parameters FROM idx1 or TO idx2. If you omit the FROM parameter, the system deletes the lines from the start of the table to idx2. If you omit the TO parameter, the system deletes from line idx1 to the end of the table. The starting line idx1 must be greater than 0.

The Return Code is set as follows:

SY-SUBRC = 0:

At least one entry deleted.

SY-SUBRC = 4:

No entries deleted.

Variant 6

DELETE itab WHERE logexp.

Extras:

1. ... FROM idx1

2. ... TO idx2

Effect

Deletes all of the entries from the internal table itab that satisfy the condition logexp. The condition logexp can be almost any logical expression. The only restriction is that the first field in each comparison must be a component of the line structure of the internal table itab.

The Return Code is set as follows:

SY-SUBRC = 0:

At least one entry deleted.

SY-SUBRC = 4:

No entries deleted.

Notes

When you use the WHERE condition with a STANDARD or HASHED table, a full table scan is always required.

If you are working with a SORTED TABLE, the partial sequential processing can be optimized so that only the entries that actually satisfy the WHERE condition are processed. This optimization requires that each sub-condition is linked with AND; that no parentheses are used; that at least one sub-condition is in the form k = v; and that the conditions in the form k1 = v1 ... kn = vn cover at least the first part of the table key.

The starting point for the loop is specified using a binary search with the sub-conditions that cover the table key (partially or completely). This optimization is similar to the optimization in the READ statement. The loop is only processed from the starting point to the point where these sub-conditions are still met. (See also Optimized Key Operations in Internal Tables).

Each operand in the WHERE condition that is not a component of the corresponding internal table itab can be replaced by a functional method call. Functional methods are methods in ABAP Objects with any number of IMPORTING parameters and a single RETURNING parameter.

If the line type of the internal table contains object reference variables, or the entire line type is itself a reference variable, you can use the attributes of the object to which the reference is pointing as comparison values in the WHERE condition (see Attributes of objects as the key of an internal table).

You can only use the additions ... FROM idx1 and ... TO idx2 with index tables (standard or sorted tables).

Addition 1

... FROM idx1

Effect

Restricts the area searched to the set of lines beginning at index idx1. If you omit the addition FROM idx1, the system searches from the start of the table.

The FROM addition must occur before the WHERE condition.

Addition 2

... TO idx2

Effect

Restricts the area searched to the set of lines up to index idx2. If you omit the addition TO idx2, the system searches to the end of the table.

The TO addition must occur befor the WHERE condition.

Example

Example to delete all of the lines between 5 and 36 in a table of names where the entry begins with one of the letters 'A' to 'C'.

TYPES: BEGIN OF NAMETAB_TYPE,

NAME(30) TYPE C,

END OF NAMETAB_TYPE.

DATA: NAMETAB TYPE STANDARD TABLE OF NAMETAB_TYPE WITH

NON-UNIQUE DEFAULT KEY INITIAL SIZE 100.

...

DELETE NAMETAB FROM 5 TO 36 WHERE NAME CA 'ABC'.

Variant 7

DELETE ADJACENT DUPLICATES FROM itab.

Extras:

1. ... COMPARING f1 f2 ...

2. ... COMPARING ALL FIELDS

Effect

Deletes adjacent duplicate entries from the internal table itab. If there are n duplicate entries in succession, the first entry is retained, and the following n-1 entries are deleted.

Two lines are regarded as duplicates if their keys are identical.

The Return Code is set as follows:

SY-SUBRC = 0:

At least one duplicate was found, and at least one entry was deleted.

SY-SUBRC = 4:

No duplicates found, no entries deleted.

Addition 1

... COMPARING f1 f2 ...

Effect

Two lines of the internal table itab are regarded as duplicates if they have identical contents in the fields f1, f2, ...

Addition 2

... COMPARING ALL FIELDS

Effect

Two lines of the internal table are regarded as duplicates if all of their field contents are identical.

Notes

The DELETE ADJACENT DUPLICATES statement works particularly well if you have sorted the internal table itab according to the fields that you want to compare when looking for duplicates. In this case, deleting adjacent duplicates is the same as deleting all duplicates. The direction of the sort is irrelevant.

If you do not know a comparison expression until runtime, you can specify it dynamically as the contents of the field name in the expression COMPARING ... (name) .... If name is empty at runtime, the comparison expression is ignored. If name contains an invalid component name, a runtime error occurs.

You can further restrict comparison expressions - both static and dynamic - by specifying offset and length.

Note

Performance:

When you delete a line of an internal table, index maintenance costs are incurred. These depend on the index of the table. The runtime is independent of the width of the table line.

Deleting a line from the middle of an internal table with 200 entries usually requires around 10 msn (standardized microseconds).

When you delete a set of entries using "DELETE itab FROM idx1 TO idx2." or "DELETE itab WHERE ...", index maintenance costs are only incurred once. This means that it is considerably more efficient than deleting entries in a LOOP.

If you want to delete adjacent duplicate entries from an internal table, use the variant " DELETE ADJACENT DUPLICATES FROM itab." instead of a LOOP construction.

Related

INSERT itab, MODIFY itab

Additional help

Deleting Table Lines

Deleting Table Lines Using the Index

Former Member
0 Kudos

1.

Deleting Several Lines Using a Condition:

To delete more than one line using a condition, use the following statement:

DELETE <itab> WHERE <cond>.

2.

DELETE TABLE <itab> WITH TABLE KEY <k1> = <f 1> ... <k n> = <f n>.

regards

srikanth

Message was edited by: Srikanth Kidambi

Former Member
0 Kudos

hii

DELETE itable WHERE fd1

Former Member
0 Kudos

Hi Suchitra,

Plz check it once.

Delete from an internal table

- DELETE itab.

- DELETE TABLE itab WITH TABLE KEY k1 = v1 ... kn = vn.

- DELETE TABLE itab [FROM wa].

- DELETE itab INDEX idx.

- DELETE itab FROM idx1 TO idx2.

- DELETE itab WHERE logexp.

- DELETE ADJACENT DUPLICATES FROM itab.

Regards

Laxmi.

Former Member
0 Kudos

Hi suchitra,

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab> [COMPARING <comp>].

The system deletes all adjacent duplicate entries from the internal table <itab>.

Entries are duplicate if they fulfill one of the following compare criteria:

If you do not use the COMPARING addition, the contents of the standard key fields must be identical (see Operations on Internal Tables).

With the COMPARING option

.... COMPARING <f1> <f 2>...,

the contents of the specified fields <f 1 > <f 2 >... must be the same. You can also specify the field names at runtime in parentheses by writing (<name>) instead of <f 1>. The field <name> contains the name of the sort key field. If <name> is empty at runtime, the system ignores it. If it contains an invalid component name, a runtime error occurs.

With the COMPARING option

.... COMPARING ALL FIELDS,

the contents of all fields must be the same.

If the system finds and deletes at least one duplicate entry, SY-SUBRC is set to 0. Otherwise, it is set to 4.

You can use this statement to delete all duplicate entries from an internal table if the table is sorted by the specified compare criterion.

award points if helpful.

regards,

keerthi.

Former Member
0 Kudos

Hi,

DELETE ADJACENT DUPLICATES FROM itab.

Additions:

1. ... COMPARING f1 f2 ...

2. ... COMPARING ALL FIELDS

Effect

Deletes adjacent duplicate entries from the internal table itab. If there are n duplicate entries in succession, the first entry is retained, and the following n-1 entries are deleted.

Two lines are regarded as duplicates if their keys are identical.

The return code is set as follows:

SY-SUBRC = 0:

At least one duplicate was found, and at least one entry was deleted.

SY-SUBRC = 4:

No duplicates found, no entries deleted.

Addition 1

... COMPARING f1 f2 ...

Effect

Two lines of the internal table itab are regarded as duplicates if they have identical contents in the fields f1, f2, ...

Addition 2

... COMPARING ALL FIELDS

Effect

Two lines of the internal table are regarded as duplicates if all of their field contents are identical.

Notes

The DELETE ADJACENT DUPLICATES statement works particularly well if you have sorted the internal table itab according to the fields that you want to compare when looking for duplicates. In this case, deleting adjacent duplicates is the same as deleting all duplicates. The direction of the sort is irrelevant.

If you do not know a comparison expression until runtime, you can specify it dynamically as the contents of the field name in the expression COMPARING ... (name) .... If name is empty at runtime, the comparison expression is ignored. If name contains an invalid component name, a runtime error occurs.

You can further restrict comparison expressions - both static and dynamic - by specifying offset and length.

Note

Performance:

When you delete a line of an internal table, index maintenance costs are incurred. These depend on the index of the table. The runtime is independent of the width of the table line.

Deleting a line from the middle of an internal table with 200 entries usually requires around 10 msn (standardized microseconds).

When you delete a set of entries using "DELETE itab FROM idx1 TO idx2." or "DELETE itab WHERE ...", index maintenance costs are only incurred once. This means that it is considerably more efficient than deleting entries in a LOOP.

If you want to delete adjacent duplicate entries from an internal table, use the variant " DELETE ADJACENT DUPLICATES FROM itab." instead of a LOOP construction.

rgds,

latheesh

0 Kudos


loop at itab.

  if itab-fld1 = itab-fld2.
     delete itab.
  endif.

endloop.



Regards,

Rich Heilman

Former Member
0 Kudos

Thanks for ur time .

But my problem is that i have a record from database table which i need to compare with record in internal table and delete it.

mind u all the fields in internal table are not available in database so i am not able to use

delete itab key field because -


i need to use this statement but iam not sure

delete itab where f1= db-f1

f2 = db-f2..

i dont whether the syntax is right or wrong because in comparision can we use multiple fields..

please let me know

suchitra

0 Kudos

just use

delete itab where f1= db-f1 and

f2 = db-f2 and

f3 = db-f3.

Regards,

Suresh Datti

0 Kudos

Hi suchitra,

u can use

loop at itab.

delete itab where fd1 = db-fd1 and

fd2 = db-fd2 and

...fdn = db-fdn.

endloop.

award points if helpful.

regards,

keerthi.

0 Kudos

Yep, you can do that. Here is a short sample program.



report zrich_0001 .

data: begin of itab occurs 0,
      matnr type mara-matnr,
      mtart type mara-mtart,
      mbrsh type mara-mbrsh,
      end of itab.
data: xt134 type t134.
data: xt137 type t137.

* Get some data for the ITAB
select matnr mtart mbrsh into table itab
       from mara up to 1000 rows.


* Get the value from the db that you want do filter on
select single * into xt134 from t134
       where mtart = 'ZROH'.

* Get the value from the db that you want do filter on
select single * into xt137 from t137
       where mbrsh = 'M'.



* Now delete the records accordingly.
delete itab where mtart = xt134-mtart
              and mbrsh = xt137-mbrsh.


* Of course you can do this too.
*delete itab where mtart = 'ZROH'
*              and mbrsh = 'M'.


check sy-subrc  = 0.


Regards,

Rich Heilman

Former Member
0 Kudos

hii

loop at itab where field_1 = xyz and field_2 = abc.

delete itab .

endloop.

This will work

Regards

Naresh

Former Member
0 Kudos

I would like to thank each and everyone individually for ur replies.

Have anice day and weekend.

suchitra