表A表B的PK均有字段ID。现在想做个存储过程:将表B中ID 未在表A中出现过 的记录删除。
其实过程很简单:
第一步,select id form a;
第二步,delete from b where id not in (第一步查询的结果);我现在不知道在PL/SQL中怎么把第一步查询出来的数据填入第二步的括号中。请大家帮我想想,最好能提供代码,谢谢了。

解决方案 »

  1.   

    用exists 执行效率更高
    SQL> select * from test_1;        ID NAME       PART
    ---------- ---------- ----------
             1 a          aa
             2 b          aa
             3 b          bb
             4 c          cc
             2 A          AA
             5 d          dd6 rows selectedSQL> select * from test_2;        ID VAL
    ---------- ----------
             1 abc
             2 abc
    SQL> delete from test_1 b where  not exists (select 1 from test_2 a where b.id=a.id);3 rows deleted
      

  2.   

    delete from b where id not in (select id form a and a.id is not null)
      

  3.   

    delete b where id not in(select id from a)
    --or
    delete b where not exists(select 1 from a where id=b.id)
      

  4.   

    楼上的,如果A表中有一个ID为NULL.
    你的第一种写法
    将会删除整张表的记录!