Oracle
 sql >> Database >  >> RDS >> Oracle

Crea una tabella nidificata e inserisci i dati solo nella tabella interna

Puoi usare COALESCE( salary_history, salary_list() ) MULTISET UNION ALL salary_list( :your_new_value ) per aggiungere il nuovo valore alla vecchia lista (o creare una nuova lista se non esiste).

Impostazione dello schema Oracle 11g R2 :

CREATE OR REPLACE TYPE salary_list AS TABLE OF NUMBER;
/

CREATE TABLE employees(
  id             NUMBER,
  salary_history salary_list
) NESTED TABLE salary_history STORE AS salary_history_tab
/

INSERT INTO employees VALUES ( 1, NULL )
/

Query 1 :

UPDATE employees
SET   salary_history = COALESCE( salary_history, salary_list() )
                       MULTISET UNION ALL salary_list( 500 )
WHERE id = 1

SELECT * FROM employees

Risultati :

| ID | SALARY_HISTORY |
|----|----------------|
|  1 |            500 |

Query 2 :

UPDATE employees
SET   salary_history = COALESCE( salary_history, salary_list() )
                       MULTISET UNION ALL salary_list( 700 )
WHERE id = 1

SELECT * FROM employees

Risultati :

| ID | SALARY_HISTORY |
|----|----------------|
|  1 |        500,700 |

Query 3 :

UPDATE employees
SET   salary_history = COALESCE( salary_history, salary_list() )
                       MULTISET UNION ALL salary_list( 500 )
WHERE id = 1

SELECT * FROM employees

Risultati :

| ID | SALARY_HISTORY |
|----|----------------|
|  1 |    500,700,500 |