Test 表:
create table Test
(city_id number(10) primary key,
 province_id number(10)
)  该表表示:City和Province的关系。
插入几条测试数据:
insert into Test values(100,1);
insert into Test values(101,1);需求: 
外部输入参数p_city和p_province值 ,在Test表中比较。 
如果p_city 在Test中不存在,(即:该city在Test不存在,需新增city),则insert into Test values(p_city,p_province)如果P_city在Test中存在,province也同时存在,(即:city和province的关系已经存在)则不做任何操作如果P_city在Test中存在, province也同时不存在,(即:city和province的关系不存在,修改city和province关系)则update Test set Test.province_id = p_province where Test.city_id = p_city下面是我写的存储过程:
create or replace procedure mergeTest(p_city in number,p_province in number)
AS
v_city number(10);
v_province number(10);
Begin
v_city := p_city;
v_province := p_province;
merge into Test t1
using (select v_city as c , v_province as p from dual ) t2
on (t1.city_id = t2.c)  
when not matched then
insert (t1.city_id,t1.province_id) values(t2.c,t2.p)
when  matched then
if t1.province_id := t2.p then
else
execute immediately "update t1 set t1.province_id := t2.p where t1.city_id := t2.c";
end if;
commit;
end mergeTest;
请教高手,在merge into 中是否可以使用if else ??
小弟不胜感激!

解决方案 »

  1.   

    你的要求跟我写的这样sql意思是一样的。create or replace procedure mergeTest(p_city in number,p_province in number) 
    AS 
        v_city number(10); 
        v_province number(10); 
    Begin 
        update Test set Test.province_id = p_province where Test.city_id = p_city; --如果city_id和province_id同时存在,更新一下也没有什么关系
        --不存在,就插入
        IF sql%notfound THEN
            insert (t1.city_id,t1.province_id) values(t2.c,t2.p);
        END IF; 
        COMMIT;
    END;
      

  2.   

    --测试了的
    create or replace procedure mergeTest(p_city in number,p_province in number) 
    AS 
        v_city number(10); 
        v_province number(10); 
    Begin 
        update Test set Test.province_id = p_province where Test.city_id = p_city;
        IF sql%rowcount <=0 THEN
            insert into test(city_id, province_id) values(p_city, p_province);
        END IF; 
        COMMIT;
    END;
      

  3.   

    --10g有新的语法,支持update语句里加where字句
    create or replace procedure mergeTest(p_city in number,p_province in number) 
    AS 
        v_city number(10); 
        v_province number(10); 
    Begin 
        v_city := p_city; 
        v_province := p_province; 
        merge into Test t1 
        using (select v_city as c , v_province as p from dual ) t2  on (t1.city_id = t2.c)  
        when not matched then 
            insert (city_id, province_id) values(t2.c,t2.p)
        when  matched then 
            update set t1.province_id = t2.p where t1.city_id = v_city;    commit; 
    end mergeTest; 
      

  4.   

    太谢谢了。
    2楼的朋友的方法很简单,也达到了效果。
    小弟认为:唯一一点不足的,就是如果city_id和province_id都存在的情况下,需要做一次update操作。如果city_id和province_id同时存在的记录非常多的话,就需要做太多没用的update,这样就会对sql执行效率产生影响。
    希望高手可以解决下我这个疑问。
    谢谢!!!
      

  5.   

    非常感谢2楼朋友的意见!!
    如果Test_city表中有1000条记录,有500条重复记录是不需要update的。
    我进行了1000次if判断 和 500次 update  的效率 和 直接500次update 相比,确实直接update 500次的效率还更加高。结帖  给分 。  非常感谢!!