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 ??
小弟不胜感激!
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 ??
小弟不胜感激!
解决方案 »
- 11g R2 RAC安装,推荐使用asm磁盘组,具体怎么使用能有人介绍下吗
- Oracle 独立Ado驱动发布的问题
- Oracle Developer 2000 怎样屏蔽报表列印功能?
- 【高手帮忙】我想知道学习数据库的步骤为dba做准备的学要怎么来学?
- 进入oracle9i企业管理器(oracle enterprise manager console),打开本地数据库连接时,提示'关闭的连接'
- 无法插入数据
- 在執行SQL查詢的時候,出現以下錯誤,求助。
- 如何快速复制一个blob字段内容
- 如何用JDBC导出和导入Oracle数据库
- 看遍以前的帖子,我的问题依旧没有解决,求助!
- 哪位大虾帮我看看数据库的参数设置是否合理?
- linux安装Oracle9设置环境变量在哪设置?
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;
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;
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;
2楼的朋友的方法很简单,也达到了效果。
小弟认为:唯一一点不足的,就是如果city_id和province_id都存在的情况下,需要做一次update操作。如果city_id和province_id同时存在的记录非常多的话,就需要做太多没用的update,这样就会对sql执行效率产生影响。
希望高手可以解决下我这个疑问。
谢谢!!!
如果Test_city表中有1000条记录,有500条重复记录是不需要update的。
我进行了1000次if判断 和 500次 update 的效率 和 直接500次update 相比,确实直接update 500次的效率还更加高。结帖 给分 。 非常感谢!!