update update multiple rows of data (oracle)

Transfer from: http: //blog.itpub.net/25322446/viewspace-767505

Description : Notes summarizes the range encountered in the work of several update methods and various methods applicable.

1. Single table update
scheme: using the standard syntax to update, and to perform a stable high efficiency
update Table
SET (column1, Column2, ...) =
VALUE1, value2, ...
;

Table 2. Multi-related update
Example: Update the table ft_lstate gkfq_rec slid all the rows with the same table oa2_ftask fi_inst, blzt field value = oa2_ftask table.

create table gkfq_rec (

slid char(12) parimary key,

blzt varchar2(50),

wjbt varchar2(100) not null,

........

);

create table oa2_ftask (

fi_inst char (12) parimary key,

fi_state int not null,

ft_lstate int not null,

...

);

 

 The method described

 Scope

 operation efficiency

 Traditional solutions  Generally applicable  High efficiency and stable single-table updates, the slower the efficiency of multi-table
 inline view update method  Associated with the primary key field  Faster
 merge update method  Non-key associated field for two associated table  Associated with non-primary key table updates, faster
 Act fast cursor updates  Logic more complex cases  When the complex logic high efficiency

 

(1)传统方案(速度可能最慢)
update gkfq_rec a 
set blzt=
(select b.ft_lstate from oa2_ftask b where a.slid=b.fi_inst)
where exists
(select 1 from oa2_ftask b where a.slid=b.fi_inst)
;

// sub-query returns multiple rows value, filtered through a progressive condition where exists, to achieve one matched set of unique values

(2) inline view update method (associated with the primary key field, faster)
Program: to update the view of establishing a temporary. B requirements table's primary key field must be in the condition where, and is updated to associate = number table, or may be given: ORA-01779: can not be modified and the non-key storage table corresponding to the column. When B is the primary key column is a combination of a plurality of rows, this error may also occur. Update (AS SELECT a.blzt blzt, b.ft_lstate AS ft_lstate
from gkfq_rec A, B WHERE a.slid oa2_ftask = b.fi_inst) 
SET blzt = ft_lstate
;

(3) merge update method (associated field when the non-primary key, faster)
Syntax:
the MERGE the INTO table_name Alias. 1
the USING (Table | View | sub_query) Alias 2
the ON (the Join for condition Condition)
the WHEN MATCHED THEN
the UPDATE 
the SET col1 = col_val1,
    col2 = col_val2
the WHEN the NOT MATCHED THEN
the INSERT (the column_list) the VALUES (column_values);

Protocol: alias2 out a select data, each related alias1 ON (join condition) compare, if match, the update is performed does not match the insert operation. Returns the number of rows does not affect the merge, and a maximum of only two tables associated connecting field conditions are not suitable for the primary key.
gkfq_rec INTO A Merge
the using oa2_ftask B
ON (= a.slid b.fi_inst)
When the then Matched
Update SET a.blzt = b.ft_lstate;

(4) fast cursor update method (when complex logic, high efficiency)
syntax:
the begin
for cr in (query) loop - loop
update table_name set ... - update statements (out of a collection based on a query result)
End Loop ; - end of the cycle
end;

方案:配合oracle独有的内置ROWID物理字段,使用快速游标,不需要定义,直接把游标写到for循环中,快速定位并执行更新。它可以支持复杂逻辑的查询语句,更新准确,无论数据多大更新效率依然很高。但执行后不返回影响行数。
begin
for aa in (select a.rowid as rowid,b.ft_lstate as ft_lstate from gkfq_rec a,oa2_ftask b
where a.slid=b.fi_inst ) loop
update gkfq_rec set blzt=aa.ft_lstate
where rowid=aa.rowid;
end loop;
end;

转自:http://blog.itpub.net/25322446/viewspace-767505

说明:笔记总结了在工作中遇到过的几种update方法和各种方法适用的范围。

1.单表更新
方案:使用标准update语法即可,执行稳定且效率较高
update table
set (column1,column2,...)=
value1,value2,...
;

2.多表关联更新
举例:更新gkfq_rec表中所有slid与oa2_ftask表fi_inst相同的行,blzt字段值=oa2_ftask表的ft_lstate。

create table gkfq_rec (

slid char(12) parimary key,

blzt varchar2(50),

wjbt varchar2(100) not null,

........

);

create table oa2_ftask (

fi_inst char(12) parimary key,

fi_state int not null,

ft_lstate int not null,

...

);

 

 方法描述

 适用范围

 运行效率

 传统方案  一般情况适用  单表更新效率高且稳定,多表时效率较慢
 inline view更新法  关联字段为主键  速度较快
 merge更新法  关联字段非主键,适用于两表关联  非主键关联表更新,速度较快
 快速游标更新法  逻辑较复杂的情况  复杂逻辑时效率很高

 

(1)传统方案(速度可能最慢)
update gkfq_rec a 
set blzt=
(select b.ft_lstate from oa2_ftask b where a.slid=b.fi_inst)
where exists
(select 1 from oa2_ftask b where a.slid=b.fi_inst)
;

//子查询返回多行值时,通过where exists条件逐行过滤,一一匹配实现set唯一值

(2)inline view更新法(关联主键字段,速度较快)
方案:更新一个临时建立的视图。要求B表的主键字段必须在where条件中,并且是以=号来关联被更新表,否则可能报错:ORA-01779:无法修改与非键值保存表对应的列。当B表主键字段为多列组合时,也有可能出现这一报错。update (select a.blzt as blzt,b.ft_lstate as ft_lstate
from gkfq_rec a,oa2_ftask b where a.slid=b.fi_inst) 
set blzt=ft_lstate
;

(3)merge更新法(关联字段非主键时,速度较快)
语法:
MERGE INTO table_name alias 1
USING (table|view|sub_query) alias 2
ON (join condition)
WHEN MATCHED THEN
UPDATE 
SET col1=col_val1,
    col2=col_val2
WHEN NOT MATCHED THEN
INSERT (column_list) VALUES (column_values);

方案:在alias2中select出来的数据,每一条都跟alias1进行ON (join condition)比较,若匹配,就进行更新操作,不匹配,执行插入操作。merge不会返回影响行数,且最多只能两表关联,适用于连接条件不是主键的字段。
merge into gkfq_rec a
using oa2_ftask b
on (a.slid=b.fi_inst)
when matched then
update set a.blzt=b.ft_lstate;

(4)快速游标更新法(复杂逻辑时,效率很高)
语法:
begin
for cr in (查询语句) loop  --循环
update table_name set ...   --更新语句(根据查询出来的结果集合)
end loop;  --结束循环
end;

方案:配合oracle独有的内置ROWID物理字段,使用快速游标,不需要定义,直接把游标写到for循环中,快速定位并执行更新。它可以支持复杂逻辑的查询语句,更新准确,无论数据多大更新效率依然很高。但执行后不返回影响行数。
begin
for aa in (select a.rowid as rowid,b.ft_lstate as ft_lstate from gkfq_rec a,oa2_ftask b
where a.slid=b.fi_inst ) loop
update gkfq_rec set blzt=aa.ft_lstate
where rowid=aa.rowid;
end loop;
end;

Guess you like

Origin www.cnblogs.com/cppfans140812/p/12217451.html