Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Erstellen Sie eine verschachtelte Tabelle und fügen Sie Daten nur in die innere Tabelle ein

Sie können COALESCE( salary_history, salary_list() ) MULTISET UNION ALL salary_list( :your_new_value ) verwenden um den neuen Wert an die alte Liste anzuhängen (oder eine neue Liste zu erstellen, falls sie nicht existiert).

Oracle 11g R2-Schema-Setup :

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 )
/

Abfrage 1 :

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

SELECT * FROM employees

Ergebnisse :

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

Abfrage 2 :

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

SELECT * FROM employees

Ergebnisse :

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

Abfrage 3 :

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

SELECT * FROM employees

Ergebnisse :

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