Update updates multiple rows of data (Oracle)

Source: Internet
Author: User

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

Note: Notes summarize several update methods and the range of methods that have been encountered at work.

1. single-table update
Scenario: Use standard update syntax to perform stable and high efficiency
Update table
Set (Column1,column2,...) =
Value1,value2,...
;

2. Multi-table associated updates
Example: Update all slid in the Gkfq_rec table with the same row as Oa2_ftask table Fi_inst, blzt field value =oa2_ftask table Ft_lstate.

CREATE TABLE Gkfq_rec (

Slid char (parimary key),

Blzt VARCHAR2 (50),

WJBT VARCHAR2 (+) NOT NULL,

........

);

CREATE TABLE Oa2_ftask (

Fi_inst char (parimary key),

Fi_state int NOT NULL,

Ft_lstate int NOT NULL,

...

);

Method Description

Scope of application

Operational efficiency

Traditional Solutions General Conditions apply Single-table update is efficient and stable, and the efficiency of multiple tables is slow
Inline View Update method Associate fields as primary keys Faster than fast
Merge Update method Association field non-primary key for two-table association Non-primary Key association table update, faster
Fast Cursor Update method A more complex case of logic High efficiency when complex logic

(1) Traditional scheme (speed may be slowest)
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)
;

When a subquery returns a multiline value, it is filtered by row by where exists condition, and each match implements a set unique value

(2) Inline view Update method (associated primary key field, faster)
Scenario: Update a temporary built-in view. Requires that the primary key field of Table B must be in the where condition and is associated with the updated table with the = number, otherwise it might be an error: ORA-01779: Cannot modify the column corresponding to the non-key-value save table. This error can also occur when the B-table primary key Chiweido column combination. 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 Update method (faster when associating fields with non-primary keys)
Grammar:
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 isn't matched then
INSERT (column_list) VALUES (column_values);

Scenario: Select data in Alias2, each with the ALIAS1 on (join condition) comparison, if matched, update operation, do not match, perform the insert operation. The merge does not return a number of affected rows and can only be associated with at most two tables, for fields where the join condition is not a primary key.
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) Fast cursor Update method (High efficiency when complex logic)
Grammar:
Begin
For CR in (query statement) loop--loop
UPDATE table_name SET ...-Updated statement (set of results based on query)
End Loop; --End Loop
End

Scenario: With Oracle's unique built-in ROWID physical field, use a fast cursor, do not need to define, directly write the cursor to the for loop, quickly locate and perform the update. It can support complex logic query statements, update accurate, no matter how big the data update efficiency is still very high. However, the number of rows affected does not return after execution.
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

Update updates multiple rows of data (Oracle)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.