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 TableGkfq_rec (slidChar( A) parimaryKey, Blztvarchar2( -), WJBTvarchar2( -) not NULL,........);Create TableOa2_ftask (Fi_instChar( A) parimaryKey, Fi_stateint  not NULL, Ft_lstateint  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)

Updateset blzt=(Selectfromwhere a.slid=  b.fi_inst)whereexists(Select1from  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 as ft_lstatefromwhere a.slid=  set blzt=ft_lstate;

(3) Merge Update method (faster when associating fields with non-primary keys)
Grammar:

MERGE intoTABLE_NAME Alias1USING (Table|View|Sub_query) Alias2 on(Joincondition) whenMatched ThenUPDATE SETCol1=Col_val1, col2=Col_val2 when  notMatched ThenINSERT(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.

 into Gkfq_rec ausing oa2_ftask b  on (A.slid=b.fi_inst)  when  Then Update set a.blzt=b.ft_lstate;

(4) Fast cursor Update method (High efficiency when complex logic)
Grammar:

begin  for inch (query statement) Loop  -- loop updateset ...   -- UPDATE 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  ft_lstate  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.