Oracle中的动态删除

时间:2016-12-11 15:11:40

标签: sql oracle plsql sql-delete

我有一些静态数据,我在union all的帮助下创建select语句,我将这些数据与DB表(部门)进行比较..在减号的帮助下,我将获得DB表中的额外记录我需要删除那些记录......需要将现有记录与静态数据同步,我希望这将明确要求..

尝试在下面的查询中删除select查询的resultSet我不知道我的错误在哪里..获取SQL错误:ORA-01732:数据操作操作在此视图上不合法 01732. 00000 - “数据操作操作在此视图中不合法”

这里我试图删除部门中与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));

2 个答案:

答案 0 :(得分:1)

delete处理表,而不是select语句的结果。在这里,您只想删除ID为66和77的所有记录:

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

答案 1 :(得分:1)

  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
                       )
                   )
         )

  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
         )

但请确保您的UNION ALL记录中没有select null,null,null,null from dual,否则不会删除任何内容