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 from db table

Former Member
0 Likes
6,028

We have a requirement where the user wants to delete certain records from variuos tables at a time.The user will enter Table name,time created and date created as the input.These will be select-options.Upon executing this the program has to delete the records from the tables the user has entered.how can i do this?

12 REPLIES 12
Read only

Former Member
0 Likes
1,578

you mean to say you need to delete the records of the database table right?

Then you can use the select option in your where condition as delete the data from the internal table

using the statement delete itab.

Now thats the simple program you need to write.

Hope it helps

regards

Bhanu

Read only

0 Likes
1,578

The user can enter several tables like MARA,MARC etc ata time.The records should be selected first for these tables and then deleted.So how do we specify the tables dynamically in the select statment?

Read only

0 Likes
1,578

Hi,

try this code.

select-options s_matnr like mara-matnr.

delete from MARA where matnr in s_matnr.

delete from MARC where matnr in s_matnr.

regards.

sriram.

Read only

0 Likes
1,578

>

> The user can enter several tables like MARA,MARC etc ata time.The records should be selected first for these tables and then deleted.So how do we specify the tables dynamically in the select statment?

Isn't anyone going to make the obvious comment that you should NEVER do direct table updates on main SAP tables such as MARC and MARA? And, even worse, you are letting the end user decide which table to delete from thus losing even more control over what will happen. When you say 'they can enter several tables like MARA, MARC etc' are you going to impose any restrictions at all on which table they can delete from - or are you intending to let them delete from any SAP table they choose?

Read only

0 Likes
1,578

only Z tables not SAP tables..i just gave an example

Read only

0 Likes
1,578

Dangerous example. There are some people who could read that and judge from it that it is ok to do a direct DELETE from MARA.

Personally, if I had to do something like this for a small number of tables, I'd try to keep control by making it very specific. I'd give them a list of tables they can delete from on the selection screen with checkboxes and run a separate bit of delete code for each table where the checkbox has been ticked. I would not feel comfortable allowing them to enter any table name they liked - even with validation to restrict them to Z tables or to a list of certain tables - and then generate a bit of code to do the deletion.

Read only

0 Likes
1,578

Hi.

I would like to support Christine's opinion. Offering a report that allows a user to enter a table name and some parameters and then execute a delete statements seems to be a very risky thing. If you allow users to directly delete from SAP tables everyone who executes that report can crash your system. And even if you restrict it to Z-tables: Most likely that tables contain data that is of high importance for your business.

If you insist on having such a report you should at least implement a good security concept and think carefully about authorization checks!

And even if you are sure that there never will be a malicious user of that report you have to think about some user just mixing up two tables or having a bad day or whatever. People make errors. With such a report this errors might be really bad.

Best regards,

Jan Stallkamp

Read only

Former Member
0 Likes
1,578

Hi,

if the user requiremt is to delete all the records, then do the follwoing:

1) Fill all the select options table names in a range( fill only range-low values so that table names are stored there)

2) Loop at range, then call the delete statement...

loop at r_range

DELETE FROM DATABASE r_range-low.

endloop.

Formore info check the syntax of the delete statement..in sap help

Reward if useful..

Regards

Shiva

Read only

0 Likes
1,578

Not all the records should be deleted..

Table name:

date created :

time created :

Read only

0 Likes
1,578

Hi,

then try this code.

data: itab like MARA occurs 0 with header line,

jtab like MARC occurs 0 with header line.

select-options: s_matnr like mara-matnr.

select erdat erzet into table itab from mara where matnr in s_matnr.

if sy-subrc = 0.

delete MARA from itab where matnr in s_matnr.

endif.

regards.

\

sriram.

Read only

Former Member
0 Likes
1,578

Hi,

the simplest way will be to get the records into an internal table and then delete the records from the database using this internal table. since ur table name is dynamic, u can use a dynamic select query to fetch the records.

since ur selection is based on table name and "Creation date and time", u can write the select as

select

<key fields>

from (tablename)

into *******

do revert for any clarfications.

regards,

Anoop

Read only

Former Member
1,578

Hello Rakesh,

To specify the table name dynamically,

DELETE [FROM] (<table>) WHERE <cond>.

For example:

REPORT  yh_sample1.
tables: dd03l,
mara.
parameters: table type dd03l-tabname.
select-options:
                ersda for mara-ersda.

delete from (table) where ersda in ersda.
write: sy-subrc.

Hope this helps you

Indu.