Oracle Database
Diversi modi per aggiornare i record
Ricerca…
Sintassi
- UPDATE table-Name [[AS] correlation-Name] SET column-Name = Value [, column-Name = Value}] * [WHERE clause]
- UPDATE tabella-Nome SET colonna-Nome = Valore [, colonna-Nome = Valore] * WHERE CORRENTE DI
Aggiorna la sintassi con l'esempio
Aggiornamento normale
UPDATE
TESTTABLE
SET
TEST_COLUMN= 'Testvalue',TEST_COLUMN2= 123
WHERE
EXISTS
(SELECT MASTERTABLE.TESTTABLE_ID
FROM MASTERTABLE
WHERE ID_NUMBER=11);
Aggiorna usando Vista in linea
Utilizzo della vista in linea (Se è considerato aggiornabile da Oracle)
Nota : se si affronta un errore di riga preservato non chiave, aggiungere un indice per risolvere lo stesso per renderlo aggiornabile
UPDATE
(SELECT
TESTTABLE.TEST_COLUMN AS OLD,
'Testvalue' AS NEW
FROM
TESTTABLE
INNER JOIN
MASTERTABLE
ON TESTTABLE.TESTTABLE_ID = MASTERTABLE.TESTTABLE_ID
WHERE ID_NUMBER=11) T
SET
T.OLD = T.NEW;
Aggiorna usando Unisci
Utilizzando l'unione
MERGE INTO
TESTTABLE
USING
(SELECT
T1.ROWID AS RID,
T2.TESTTABLE_ID
FROM
TESTTABLE T1
INNER JOIN
MASTERTABLE T2
ON TESTTABLE.TESTTABLE_ID = MASTERTABLE.TESTTABLE_ID
WHERE ID_NUMBER=11)
ON
( ROWID = RID )
WHEN MATCHED
THEN
UPDATE SET TEST_COLUMN= 'Testvalue';
Unisci con dati di esempio
drop table table01;
drop table table02;
create table table01 (
code int,
name varchar(50),
old int
);
create table table02 (
code int,
name varchar(50),
old int
);
truncate table table01;
insert into table01 values (1, 'A', 10);
insert into table01 values (9, 'B', 12);
insert into table01 values (3, 'C', 14);
insert into table01 values (4, 'D', 16);
insert into table01 values (5, 'E', 18);
truncate table table02;
insert into table02 values (1, 'AA', null);
insert into table02 values (2, 'BB', 123);
insert into table02 values (3, 'CC', null);
insert into table02 values (4, 'DD', null);
insert into table02 values (5, 'EE', null);
select * from table01 a order by 2;
select * from table02 a order by 2;
--
merge into table02 a using (
select b.code, b.old from table01 b
) c on (
a.code = c.code
)
when matched then update set a.old = c.old
;
--
select a.*, b.* from table01 a
inner join table02 b on a.code = b.codetable01;
select * from table01 a
where
exists
(
select 'x' from table02 b where a.code = b.codetable01
);
select * from table01 a where a.code in (select b.codetable01 from table02 b);
--
select * from table01 a
where
not exists
(
select 'x' from table02 b where a.code = b.codetable01
);
select * from table01 a where a.code not in (select b.codetable01 from table02 b);
Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow