Oracle Update There must be an update that you don't know about.

Source: Internet
Author: User

Basic Update Statements
The Oracle UPDATE statement processes one or more rows in a table and sets one or more columns to the values of you specify.

Update All records
UPDATE <table_name>
SET <column_name> = <value>
CREATE TABLE Test AS
SELECT object_name, Object_type
From All_objs;

SELECT DISTINCT object_name
From Test;

UPDATE Test
SET object_name = ' OOPS ';

SELECT DISTINCT object_name
From Test;

ROLLBACK;

Update a specific record
UPDATE <table_name>
SET <column_name> = <value>
WHERE <column_name> = <value>
select DISTINCT object_name
from Test

update test
set object_name = ' LOAD '

COMMIT;

SELECT DISTINCT object_name
from test

Update based on a queried value
UPDATE <table_name>
SET <column_name> = (
SELECT <column_name>
From <table_name
WHERE <column_name> <condition> <value>)
WHERE <column_name> <condition> <value>;
CREATE TABLE Test AS
SELECT table_name,CAST ("as VARCHAR2 ()) as Lower_name
From User_tables;

DESC Test

SELECT *
From Test
WHERE table_name like '%a% ';

SELECT *
From Test
WHERE table_name not like '%a% ';

--This was not a good thing ...
UPDATE Test T
SET Lower_name =(
SELECT DISTINCT LOWER (table_name)
From User_tables u
WHERE U.table_name = T.table_name
and u.table_name like '%a% ');
-Look at the number of rows updated

SELECT * from Test;

--neither is this 
UPDATE Test T
SET lower_name =  (
  SELECT DISTINCT LOWER (table_name)
  from User_tables u
  WHERE u.table_name = T.table_name
  U.table_name not like '%a% ' ) ;

select * from Test;
update test t
set lower_name =  (
  Select DISTINCT LOWER (table_name)
  from User_tables u
  WHERE u.table_name = t.table_name
  and U.table_name Like '%a% ' WHERE t.table_name like '%a% ';

SELECT * from test;

Update based on a query returning multiple values
UPDATE <table_name> <alias>
SET (<column_name>,<column_name> ) = (
SELECT (<column_name>, <column_name>)
From <table_name>
WHERE <alias.column_name> = <alias.column_name>)
WHERE <column_name> <condition> <value>;
CREATE TABLE Test AS
SELECT T. table_name, T. tablespace_name, S.extent_management
From User_tables T, user_tablespaces s
WHERE T.tablespace_name = S. tablespace_name
and 1=2;

DESC Test

SELECT * from Test;

--Does not work
UPDATE Test
SET (table_name, tablespace_name) = (
SELECT table_name, Tablespace_name
from User_tables);

--Works
INSERT into Test
(TABLE_NAME, Tablespace_name)
SELECT table_name, Tablespace_name
From User_tables;

COMMIT;

SELECT *
From Test
WHERE table_name like '%a% ';

--Does not work
UPDATE Test T
SET tablespace_name, extent_management = (
SELECT Tablespace_name, Extent_management
From User_tables A, user_tablespaces u
WHERE T.table_name = A.table_name
and a.tablespace_name = U.tablespace_name
and t.table_name like '%a% ');

--Works but look at the number of rows updated
UPDATE Test T
SET(Tablespace_name, Extent_management)= (
SELECT DISTINCT U.tablespace_name, u.extent_management
From User_tables A, user_tablespaces u
WHERE T.table_name = A.table_name
and a.tablespace_name = U.tablespace_name
and t.table_name like '%a% ');

ROLLBACK;

--works properly
update test T
set  tablespace_name ,  extent_management )  =  (
  Select Distinct  ( u.tablespace_name,  U.extent_management
  from User_tables A, user_ tablespaces u
  WHERE t.table_name = a.table_name
  and a.tablespace_name = U.tablespace_name Span style= "color: #0000ff;" >)
WHERE t.table_name like '%a% ';

SELECT * from test;

Update The results of a SELECT statement
update (<select statement>)
SET <column_name> = <value>
WHERE <column_name> <condition> <value
SELECT *
From Test
WHERE table_name like '%a% ';

SELECT *
From Test
WHERE table_name not like '%a% ';

UPDATE (
SELECT *
From Test
WHERE table_name not LIKE '%a% ')
SET extent_management = ' Unknown '
WHERE table_name not like '%a% ';

SELECT * from Test;
Correlated Update

Single Column
update TABLE (<select statement>) < Alias>
SET <column_name> =  (
  SELECT <column_name>
  from <table_name> <alias>
  WHERE <alias.table_name> = <alias.table_name> ) ;
conn hr/hr

CREATE TABLE empnew as
SELECT * FROM Employees;

update empnew
set salary = Salary * 1.1;

update employees t1
set salary =  (
  SELECT salary
  from Empnew t2
  Where t1.employee_id =  t2.employee_id ) ;

drop table empnew;

Multi-column
update <table_name> <alias>
SET ( <column_name_list>) = (
  SELECT <column_name_list>
  from <table_name> <alias
  WHERE <alias.table_name> <condition> <alias.table_name>);
CREATE TABLE T1 AS
SELECT table_name, Tablespace_name
From User_tables
WHERE RowNum < 11;

CREATE TABLE T2 as
SELECT table_name,
TRANSLATE (tablespace_name, ' aeiou ', ' VWXYZ ') as Tablespace_name
From User_tables
WHERE RowNum < 11;

SELECT * from T1;

SELECT * from T2;

UPDATE T1 T1_alias
SET (table_name, tablespace_name) = (
SELECT table_name, Tablespace_name
From T2 T2_alias
WHERE t1_alias.table_name = t2_alias.table_name);

SELECT * from T1;
Nested Table Update
See Nested Tables page
Update with returning Clause

Returning Clause Demo
UPDATE (<select statement>)
SET ....
WHERE ....
Returning <values_list>
Into <variables_list>;
Conn HR/HR

var bnd1 number
var bnd2 VARCHAR2 (30)
var bnd3 number

UPDATE Employees
SET job_id = ' Sa_man ', salary = salary + 1000,
department_id = 140
WHERE last_name = ' Jones '
Returning salary*0.25, last_name, department_id
Into:bnd1,: Bnd2,: Bnd3;

Print Bnd1
Print Bnd2
Print Bnd3

Rollback
Conn HR/HR

Variable bnd1 number

UPDATE Employees
SET Salary = Salary * 1.1
WHERE department_id = 100
Returning SUM (Salary) into : bnd1;

Print Bnd1

Rollback
Update Object Table

Update a Table Object
UPDATE <table_name> <alias>
SET VALUE (<alias>) = (
<select statement>)
WHERE <column_name> <condition> <value>;
create TYPE People_typ as OBJECT (
last_name     VARCHAR2 (+),
department_id number (4),
salary         number (8,2));
/

CREATE TABLE people_demo1 of People_typ;

Desc people_demo1

CREATE TABLE people_demo2 of People_typ;

Desc PEOPLE_DEMO2

INSERT into People_demo1
VALUES (People_typ (' Morgan ', ten, 100000));

INSERT into People_demo2
VALUES (People_typ (' Morgan ', ten, 150000));

update people_demo1 p
set VALUE (P) =   (
  select value (q) from People_demo2 Q
  WHERE p.department_id = q.department_id )
where p.department_id = ten;

SELECT * from People_demo1;
Record Update

Update based on a record

Note:this construct updates every column so use with care. May cause increased redo, undo, and foreign key locking issues.

UPDATE <table_name>
SET ROW = <record_name>
WHERE <column_name> <condition> <value>;
CREATE TABLE T as
SELECT table_name, Tablespace_name
From All_tables;

SELECT DISTINCT Tablespace_name
From T;

DECLARE
TREC T%rowtype;
BEGIN
Trec.table_name: = ' DUAL ';
Trec.tablespace_name: = ' new_tbsp ';

UPDATE T
SET ROW = Trec
WHERE table_name = ' DUAL ';

COMMIT;
END;
/

SELECT DISTINCT Tablespace_name
From T;
Update partitioned Table

Update only records inch a single partition
UPDATE <table_name> PARTITION (<partition_name>)
SET <column_name> = <value>
WHERE <column_name> <condition> <value>;
Conn Sh/sh

UPDATE Sales PARTITION (sales_q1_2005) s
SET s.promo_id = 494
WHERE amount_sold > 9000;

(http://blog.csdn.net/zgying/article/details/6264161)

Oracle Update There must be an update that you don't know about.

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.