Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Crie uma tabela aninhada e insira dados apenas na tabela interna


Você pode usar COALESCE( salary_history, salary_list() ) MULTISET UNION ALL salary_list( :your_new_value ) para anexar o novo valor à lista antiga (ou criar uma nova lista se ela não existir).

Configuração do esquema 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 )
/

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

SELECT * FROM employees

Resultados :
| ID | SALARY_HISTORY |
|----|----------------|
|  1 |            500 |

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

SELECT * FROM employees

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

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

SELECT * FROM employees

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