CREATE TABLE table_update ( id1 NUMBER(2,0) , desc1 VARCHAR(2) , desc2 VARCHAR(2) );
INSERT INTO table_update (id1, desc1, desc2) VALUES (1, 'A', 'X'); INSERT INTO table_update (id1, desc1, desc2) VALUES (2, 'B', 'Y'); INSERT INTO table_update (id1, desc1, desc2) VALUES (3, 'C', 'Z'); COMMIT;This creates a simple table with three columns: a numeric identifier and two character description fields. The sample data provides a foundation for demonstrating various UPDATE scenarios.
UPDATE table_update SET id1 = 11 , desc1 = 'V' WHERE id1 = 1;This statement updates the row where id1 equals 1, changing the id1 value to 11 and the desc1 value to 'V'. The WHERE clause ensures only the specified row is modified.
table_update
":SELECT id1, desc1 FROM table_update;Query result:
id1 desc1 ----- ----- 11 V 2 B 3 C
UPDATE table_update t1 SET id1 = id1 * 10 , t1.desc1 = ( SELECT t2.desc1 || t1.desc1 FROM table_update t2 WHERE t2.id1 = t1.id1 ) WHERE t1.id1 = 1;In this example, the subquery concatenates the existing desc1 value with itself by joining the table to itself. The correlation occurs through the WHERE clause matching t2.id1 = t1.id1, ensuring the subquery processes the same row being updated.
table_update
":SELECT id1, desc1 FROM table_update;Query result:
id1 desc1 ----- ----- 10 AA 2 B 3 C
UPDATE table_update t1 SET (t1.desc1, t1.desc2) = ( SELECT (t2.desc1 || t1.desc1), (t2.desc2 || t1.desc2) FROM table_update t2 WHERE t2.id1 = t1.id1 );This statement updates both desc1 and desc2 columns by concatenating each column with itself. The subquery returns two values that correspond to the two target columns specified in the SET clause.
table_update
":SELECT id1, desc1 FROM table_update;Query result:
id1 desc1 desc2 ----- ----- ----- 1 AA XX 2 BB YY 3 CC ZZ
UPDATE table_update t1 SET (t1.desc1, t1.desc2) = ( SELECT (t2.desc1 || t1.desc1), (t2.desc2 || t1.desc2) FROM table_update t2 );Query result:
Error starting at line 1 in command: UPDATE table_update t1 SET (t1.desc1, t1.desc2) = ( SELECT (t2.desc1 || t1.desc1), (t2.desc2 || t1.desc2) FROM table_update t2 ) Error report: SQL Error: ORA-01427: single-row subquery returns more than one row 01427.00000 - "single-row subquery returns more than one row" *Cause: *Action:The error occurs because the subquery lacks a WHERE clause to correlate it with the outer query, causing it to return all rows from table_update for each row being updated.
UPDATE table_update t1 SET (t1.desc1, t1.desc2) = ( SELECT (t2.desc1 || t1.desc1), (t2.desc2 || t1.desc2) FROM table_update t2 WHERE t2.id1 = t1.id1 AND t2.id1 < 3 );Select the data from the table "
table_update
":SELECT id1, desc1 FROM table_update;Query result:
id1 desc1 desc2 ----- ----- ----- 1 AA XX 2 BB YY 3Since the third row had no match in the subquery, the columns were initialized to NULL.
UPDATE table_update t1 SET (t1.desc1, t1.desc2) = ( SELECT (t2.desc1 || t1.desc1), (t2.desc2 || t1.desc2) FROM table_update t2 WHERE t2.id1 = t1.id1 AND t2.id1 < 3 ) WHERE t1.id1 < 3;Select the data from the table "
table_update
":SELECT id1, desc1 FROM table_update;Query result:
id1 desc1 desc2 ----- ----- ----- 1 AA XX 2 BB YY 3 CC ZZBy adding the same condition (t1.id1 < 3) to the main UPDATE WHERE clause, we prevent the unwanted NULL assignment to the third row.
UPDATE ( SELECT desc1, desc2 FROM table_update t2 WHERE t2.id1 = 3 ) SET desc1 = desc1 || desc1 , desc2 = desc2 || desc2;This approach updates only the columns specified in the inline view SELECT list. The WHERE clause in the inline view determines which rows are eligible for update, effectively replacing the need for a WHERE clause in the main UPDATE statement.
table_update
":SELECT id1, desc1 FROM table_update;Query result:
id1 desc1 desc2 ----- ----- ----- 1 A X 2 B Y 3 CC ZZ