vinod
vinod

Reputation: 1202

Dynamic delete in Oracle

I have Some static data for that i am creating select statement with the help of union all and i am comparing those data with DB table (departments).. with the help of minus i will get extra records which are in DB table and i need to delete those record......Need to sync up existing records with static data i hope this will clear the requirement..

Trying to delete a resultSet of select query in below query i am not sure where is my mistake.. getting SQL Error: ORA-01732: data manipulation operation not legal on this view 01732. 00000 - "data manipulation operation not legal on this view"

Here i am trying to delete all the records from departments which are not matched with 66,77

  delete from (select department_id, department_name, manager_id,location_id from departments
     minus
      (select 66,'Administration',200,1700 from dual
    union all
    select 77,'Marketing',201,1800 from dual));

Upvotes: 0

Views: 681

Answers (2)

David דודו Markovitz
David דודו Markovitz

Reputation: 44951

  delete departments

  where  department_id in
         (
            select department_id

            from  (    select  department_id, department_name, manager_id,location_id 
                       from    departments

                       minus

                       (          select 66,'Administration',200,1700 from dual
                       union all  select 77,'Marketing'     ,201,1800 from dual
                       )
                   )
         )

or

  delete departments

  where  (department_id, department_name, manager_id,location_id) not in
         (          select 66,'Administration',200,1700 from dual
         union all  select 77,'Marketing'     ,201,1800 from dual
         )

But make sure you don't have select null,null,null,null from dual among your UNION ALL records or nothing will be deleted

Upvotes: 1

Mureinik
Mureinik

Reputation: 311723

deletes work on tables, not results of select statements. Here, you just want to delete all the records besides those with IDs 66 and 77:

DELETE FROM departments WHERE id NOT IN (66, 77)

Upvotes: 1

Related Questions