<span style="font-size:14px;">Oracle9i引入了MERGE命令,你能够在一个SQL语句中对一个表同时执行inserts和updates操作. MERGE命令从一个或多个数据源中选择行来updating或inserting到一个或多个表.
Oracle 10g中MERGE有如下一些改进:
1、UPDATE或INSERT子句是可选的
2、UPDATE和INSERT子句可以加WHERE子句
3、ON条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表
4、UPDATE子句后面可以跟DELETE子句来去除一些不需要的行
首先创建示例表:
create table PRODUCTS
(
PRODUCT_ID INTEGER,
PRODUCT_NAME VARCHAR2(60),
CATEGORY VARCHAR2(60)
);
insert into PRODUCTS values (1501, 'VIVITAR 35MM', 'ELECTRNCS');
insert into PRODUCTS values (1502, 'OLYMPUS IS50', 'ELECTRNCS');
insert into PRODUCTS values (1600, 'PLAY GYM', 'TOYS');
insert into PRODUCTS values (1601, 'LAMAZE', 'TOYS');
insert into PRODUCTS values (1666, 'HARRY POTTER', 'DVD');
commit;
create table NEWPRODUCTS
(
PRODUCT_ID INTEGER,
PRODUCT_NAME VARCHAR2(60),
CATEGORY VARCHAR2(60)
);
insert into NEWPRODUCTS values (1502, 'OLYMPUS CAMERA', 'ELECTRNCS');
insert into NEWPRODUCTS values (1601, 'LAMAZE', 'TOYS');
insert into NEWPRODUCTS values (1666, 'HARRY POTTER', 'TOYS');
insert into NEWPRODUCTS values (1700, 'WAIT INTERFACE', 'BOOKS');
commit;
1、可省略的UPDATE或INSERT子句
在Oracle 9i, MERGE语句要求你必须同时指定INSERT和UPDATE子句.而在Oracle 10g, 你可以省略UPDATE或INSERT子句中的一个. 下面的例子根据表NEWPRODUCTS的PRODUCT_ID字段是否匹配来updates表PRODUCTS的信息:
SQL> MERGE INTO products p
2 USING newproducts np
3 ON (p.product_id = np.product_id)
4 WHEN MATCHED THEN
5 UPDATE
6 SET p.product_name = np.product_name,
7 p.category = np.category;
3 rows merged.
SQL> SELECT * FROM products;
PRODUCT_ID PRODUCT_NAME CATEGORY
---------- -------------------- ----------
1501 VIVITAR 35MM ELECTRNCS
1502 OLYMPUS CAMERA ELECTRNCS
1600 PLAY GYM TOYS
1601 LAMAZE TOYS
1666 HARRY POTTER TOYS
SQL>
SQL> ROLLBACK;
Rollback complete.
SQL>
在上面例子中, MERGE语句影响到是产品id为1502, 1601和1666的行. 它们的产品名字和种 类被更新为表newproducts中的值. 下面例子省略UPDATE子句, 把表NEWPRODUCTS中新的PRODUCT_ID插入到表PRODUCTS中, 对于在两个表中能够匹配上PRODUCT_ID的数据不作任何处理. 从这个例子你能看到PRODUCT_ID=1700的行被插入到表PRODUCTS中.
SQL> MERGE INTO products p
2 USING newproducts np
3 ON (p.product_id = np.product_id)
4 WHEN NOT MATCHED THEN
5 INSERT
6 VALUES (np.product_id, np.product_name,
7 np.category);
1 row merged.
SQL> SELECT * FROM products;
PRODUCT_ID PRODUCT_NAME CATEGORY
---------- -------------------- ----------
1501 VIVITAR 35MM ELECTRNCS
1502 OLYMPUS IS50 ELECTRNCS
1600 PLAY GYM TOYS
1601 LAMAZE TOYS
1666 HARRY POTTER DVD
1700 WAIT INTERFACE BOOKS
2、带条件的Updates和Inserts子句
你能够添加WHERE子句到UPDATE或INSERT子句中去, 来跳过update或insert操作对某些行的处理. 下面例子根据表NEWPRODUCTS来更新表PRODUCTS数据, 但必须字段CATEGORY也得同时匹配上:
SQL> MERGE INTO products p
2 USING newproducts np
3 ON (p.product_id = np.product_id)
4 WHEN MATCHED THEN
5 UPDATE
6 SET p.product_name = np.product_name
7 WHERE p.category = np.category;
2 rows merged.
SQL> SELECT * FROM products;
PRODUCT_ID PRODUCT_NAME CATEGORY
---------- -------------------- ----------
1501 VIVITAR 35MM ELECTRNCS
1502 OLYMPUS CAMERA ELECTRNCS
1600 PLAY GYM TOYS
1601 LAMAZE TOYS
1666 HARRY POTTER DVD
SQL>
SQL> rollback;
在这个例子中, 产品ID为1502,1601和1666匹配ON条件但是1666的category不匹配. 因此MERGE命令只更新两行数据. 下面例子展示了在Updates和Inserts子句都使用WHERE子句:
SQL> MERGE INTO products p
2 USING newproducts np
3 ON (p.product_id = np.product_id)
4 WHEN MATCHED THEN
5 UPDATE
6 SET p.product_name = np.product_name,
7 p.category = np.category
8 WHERE p.category = 'DVD'
9 WHEN NOT MATCHED THEN
10 INSERT
11 VALUES (np.product_id, np.product_name, np.category)
12 WHERE np.category != 'BOOKS'
SQL> /
1 row merged.
SQL> SELECT * FROM products;
PRODUCT_ID PRODUCT_NAME CATEGORY
---------- -------------------- ----------
1501 VIVITAR 35MM ELECTRNCS
1502 OLYMPUS IS50 ELECTRNCS
1600 PLAY GYM TOYS
1601 LAMAZE TOYS
1666 HARRY POTTER TOYS
SQL>
注意由于有WHERE子句INSERT没有插入所有不匹配ON条件的行到表PRODUCTS.
3、无条件的Inserts
你能够不用连接源表和目标表就把源表的数据插入到目标表中. 这对于你想插入所有行到目标表时是非常有用的. Oracle 10g现在支持在ON条件中使用常量过滤谓词. 举个常量过滤谓词例子ON (1=0). 下面例子从源表插入行到表PRODUCTS, 不检查这些行是否在表PRODUCTS中存在:
SQL> MERGE INTO products p
2 USING newproducts np
3 ON (1=0)
4 WHEN NOT MATCHED THEN
5 INSERT
6 VALUES (np.product_id, np.product_name, np.category)
7 WHERE np.category = 'BOOKS'
SQL> /
1 row merged.
SQL> SELECT * FROM products;
PRODUCT_ID PRODUCT_NAME CATEGORY
---------- -------------------- ----------
1501 VIVITAR 35MM ELECTRNCS
1502 OLYMPUS IS50 ELECTRNCS
1600 PLAY GYM TOYS
1601 LAMAZE TOYS
1666 HARRY POTTER DVD
1700 WAIT INTERFACE BOOKS
6 rows selected.
SQL>
4、新增加的DELETE子句
Oracle 10g中的MERGE提供了在执行数据操作时清除行的选项. 你能够在WHEN MATCHED THEN UPDATE子句中包含DELETE子句. DELETE子句必须有一个WHERE条件来删除匹配某些条件的行.匹配DELETE WHERE条件但不匹配ON条件的行不会被从表中删除.
下面例子验证DELETE子句. 我们从表NEWPRODUCTS中合并行到表PRODUCTS中, 但删除category为ELECTRNCS的行.
SQL> MERGE INTO products p
2 USING newproducts np
3 ON (p.product_id = np.product_id)
4 WHEN MATCHED THEN
5 UPDATE
6 SET p.product_name = np.product_name,
7 p.category = np.category
8 DELETE WHERE (p.category = 'ELECTRNCS')
9 WHEN NOT MATCHED THEN
10 INSERT
11 VALUES (np.product_id, np.product_name, np.category)
SQL> /
4 rows merged.
SQL> SELECT * FROM products;
PRODUCT_ID PRODUCT_NAME CATEGORY
---------- -------------------- ----------
1501 VIVITAR 35MM ELECTRNCS
1600 PLAY GYM TOYS
1601 LAMAZE TOYS
1666 HARRY POTTER TOYS
1700 WAIT INTERFACE BOOKS
SQL>
产品ID为1502的行从表PRODUCTS中被删除, 因为它同时匹配ON条件和DELETE WHERE条件. 产品ID为1501的行匹配DELETE WHERE条件但不匹配ON条件, 所以它没有被删除. 产品ID为1700 的行不匹配ON条件, 所以被插入表PRODUCTS. 产品ID为1601和1666的行匹配ON条件但不匹配DELETE WHERE条件, 所以被更新为表NEWPRODUCTS中的值.
**************************************************************************************************
定义游标类型
CREATE OR REPLACE PACKAGE 包名 IS
创建作者:
创建日期:
功能描述:定义ref cursor变量类型
实现逻辑:
TYPE TYPE_CURSOR IS REF CURSOR; --定义游标变量
包体:
CREATE OR REPLACE PACKAGE 包名 IS
procedure 存过名(
.........
) as
CURSOR_MODULEID TYPE_CURSOR; --模块游标
begin
OPEN CURSOR_MODULEID FOR
SELECT DISTINCT (MODULEID)
FROM SYS_USERROLE A, SYS_ROLEPERMIT B
WHERE USERID = I_USER
AND A.ROLEID = B.ROLEID;
......
end;
或
CURSOR cur_prod is
select branch,remainprinamt
from prod_daily_prin_branch
where prodid=is_prodid
and cdate = is_cdate;
begin
open cur_prod;
loop
fetch cur_prod into vs_brach,vn_prinamt;
exit when cur_prod%notfound;
--销售费
vn_sale := vn_prinamt*vn_syield/vn_basis;
merge into bm_yieldassign a
using
(select is_cdate as cdate
,vs_assetpool as assetpool
,vs_brach as branch
,0 as assetofamt --资产提供额
,0 as hostamt --托管费
,0 as assetamt --资产推介费
,decode(vs_mktdeptno,'L',vn_prinamt,0) as reprinamt --零售销售额
,decode(vs_mktdeptno,'L',vn_sale,0) as resamt --零售销售费
,decode(vs_mktdeptno,'T',vn_prinamt,0) as typrinamt --同业销售额
,decode(vs_mktdeptno,'T',vn_sale,0) as tysamt --同业销售费
,decode(vs_mktdeptno,'G',vn_prinamt,0) as cwprinamt --公司销售额
,decode(vs_mktdeptno,'G',vn_sale,0) as cwsamt --公司销售费
from dual) b
on (a.cdate = b.cdate
and a.assetpool=b.assetpool
and a.branch = b.branch)
when matched then
update set a.reprinamt = nvl(a.reprinamt,0)+b.reprinamt,
a.resamt = nvl(a.resamt,0) + b.resamt,
a.typrinamt = nvl(a.typrinamt,0) + b.typrinamt,
a.tysamt = nvl(a.tysamt,0) + b.typrinamt,
a.cwprinamt = nvl(a.cwprinamt,0) + b.cwprinamt,
a.cwsamt = nvl(a.cwsamt,0) + b.cwsamt
when not matched then
insert(cdate,assetpool,branch
,assetofamt,hostamt,assetamt
,reprinamt,resamt
,typrinamt,tysamt
,cwprinamt,cwsamt
,lstmntuser,lstmntdate)
values(b.cdate,b.assetpool,b.branch
,b.assetofamt,b.hostamt,b.assetamt
,b.reprinamt,b.resamt
,b.typrinamt,b.tysamt
,b.cwprinamt,b.cwsamt
,is_operator,sysdate);
end loop;
close cur_prod;
</span>
原作者: