两个表A,B
A中字段noticenum主键 。
B中字段noticenum主键,某个属性flag现在要做如下查询 
如果A中noticenum与B的noticenum相等
那么将B的这条flag设置为0;如果A的noticenum在B中不存在
那么将这条记录插入到B中,并且将flag设置为1然后两个表连接查询,要求得到B中noticeno和flag  flag可能是1也可能是0这样的情况一条sql怎么写啊?分不多了,希望高手能帮帮我!不胜感激~~

解决方案 »

  1.   

    一条语句应该做不到。你的需求是有两个动作,一是数据合并(A合并到B),二是查询B。
      

  2.   

    使用存储过程 或函数返回您想要的内容
    update b set flag=0 where b.noticenum in (select noticenum from a);
    insert into b select noticenum,1 from a,b where a.noticenum =b.noticenum(+) and b.noticenum is null ;
    open refcursor for select * from b ;
      

  3.   

    select noticenum,
    case  when (select count(*) from a where a.noticenum=b.noticenum)>1 then 0 else 1 end  from b
    union  all 
    select noticenum1, 1 from a where a.noticenum not in (select noticenum from b)
      

  4.   

    为什么非要弄成一个Sql捏!!Left Join ,Nvl,case 组合一下就OK