Application Development and Automation 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: 
Read only

DELETE

Former Member
0 Likes
790

Hi,

I want to delete all records from a custom table based on one of the KEY field.

There are 4 key fields in the table and one among them is PLANT(WERKS).

I'm using the statement:

DELETE FROM zxxx WHERE werks = '1000'.

Here only the first satisfying condition record is deleted.

I want to delete all records where werks = '1000'.

Pls help me with the DELETE syntax

Thanks

Ram

8 REPLIES 8
Read only

Former Member
0 Likes
751

Hi prabha,

1. We can do like this also.

2. Data : ztab like ztab occurs 0 with header line.

select * from ztab

into table ztab

where werks = '1000'.

Loop at ztab.

DELETE ZTAB FROM ZTAB.

ENDLOOP.

regards,

amit m.

Read only

Former Member
0 Likes
751

Hi,

First fetch the data from Ztable into Internal Table for that plant.

sort itab.

Delete the records from that Itab where plant is 1000.

delete Itab where werks = '1000'

deletes all the plant records,

Now modify the Ztable with this ITAB.

reward if useful

regards,

Anji

Read only

Former Member
0 Likes
751

Try this way.

select * from zxxx into table itab where plant = '1000'.

delete zxxx from table itab.

Regards,

Ravi

Read only

Former Member
0 Likes
751

HI,

hi,

check this from help..

DELETE { {FROM target [WHERE sql_cond]}

| {target FROM source} }.

Effect

The statement DELETE deletes one or more rows from the database table specified in target. The rows that are to be deleted are specified either in a WHERE condition sql_cond or with data objects in source.

System Fields

The statement DELETE sets the values of the system fields sy-subrc and sy-dbcnt.

sy-subrc Meaning

0 A least one row was deleted.

4 At least one row could not be deleted, since it was not found in the database table.

The statement MODIFY sets sy-dbcnt to the number of deleted rows.

DELETE dbtab - cond

Syntax

... WHERE sql_cond.

Effect

The WHERE addition uses a logical expression sql_cond to specify which rows in the database table are deleted. The same applies to the logical expression sql_cond as for the WHERE condition of the SELECT statement. If there is no row in the database that satisfies the WHERE condition, no row is deleted and sy-subrc is set to 4. If no WHERE condition is specified, all rows are deleted.

Note

As of Release 6.10, specifying the WHERE condition is optional. Prior to Release 6.10, you had to specify the WHERE condition in this variant of the DELETE statement and you could not use dynamic logical expressions.

Example

In the following example, all an airline's flights for today in which no seats are occupied are deleted from the database table SFLIGHT (see also, example to dtab-source).

PARAMETERS p_carrid TYPE sflight-carrid.

DELETE FROM sflight

WHERE carrid = p_carrid AND

fldate = sy-datum AND

seatsocc = 0.

Syntax

... FROM wa|{TABLE itab}.

Alternatives:

1. ... FROM wa

2. ... FROM TABLE itab

Effect

After FROM, you can specifiy a data object wa that is not table-type or an internal table itab. The content of the data objects determines the row(s) to be deleted.

Alternative 1

... FROM wa

Effect

If you specify a work area wa that is not table-type, a row is searched for in the database table whose primary key content is the same as that of the corresponding initial part of the work area. The content of the work area is not converted and is interpreted according to the structure of the database table or view. This row is deleted. The work area must meet the prerequisites for use in Open SQL statements.

If there is no row in the database with the same content as the primary key, no row is deleted and sy-subrc is set to 4.

Notes

The work area wa should be declared wit reference to the database table or view in the ABAP Dictionary using the length of the primary key.

If the database table of view are specified statically, you do not have to specify the work area with FROM wa outside of classes if a table work area dbtab is declared for the relevant database table or view using the TABLES statement. The system then implicitly adds the FROM dbtab addition to the DELETE statement.

Alternative 2

... FROM TABLE itab

Effect

If an internal table itab is specified, the system processed all rows of the internal table according to the rules for the work area wa. The row type of the internal table must meet the prerequisites for use in Open SQLstatements.

If, for a row of the internal table, there is no row in the database with the same content as the primary key, the corresponding row is ignored and sy-subrc is set to 4. If the internal table is empty, sy-subrc is set to 0. The system field sy-dbcnt is always set to the number of rows actually deleted.

Example

In the following example, all today's flights of an airline in which no seats are occupied are deleted from the database table SFLIGHT. The client field must be in the row structure of the internal table sflight_key_tab. Otherwise it does not cover the primary key of the database table and incorrect key values will be accepted as a result. This example has the same function as that for dtab-cond, but it requires two database accesses. The deleted rows are recorded in the internal table.

PARAMETERS p_carrid TYPE sflight-carrid.

TYPES: BEGIN OF sflight_key,

mandt TYPE sflight-mandt,

carrid TYPE sflight-carrid,

connid TYPE sflight-connid,

fldate TYPE sflight-fldate,

END OF sflight_key.

DATA sflight_key_tab TYPE TABLE OF sflight_key.

SELECT carrid connid fldate

FROM sflight

INTO CORRESPONDING FIELDS OF TABLE sflight_key_tab

WHERE carrid = p_carrid AND

fldate = sy-datum AND

seatsocc = 0.

DELETE sflight FROM TABLE sflight_key_tab.

do reward if it helps,

Kishi.

Read only

Former Member
0 Likes
751

Hi,

tables:<db table>.

select * from <db table> into wa where bukrs = '1000'..

delete <db table> from wa.

endselect.

rgds,

bharat.

Read only

Former Member
0 Likes
751

Hi Prabha,

Use the DELETE statement within LOOP and ENDLOOP commands.

Reward me if helpful.

Thanks,

Viji..

Read only

Former Member
0 Likes
751

try this...

DELETE FROM <b>TABLE</b> zxxx WHERE werks = '1000'.

Read only

Former Member
0 Likes
751

HI,

DELETE zxxx WHERE werks = '1000'.

Regards

Sudheer