简体   繁体   中英

Oracle - using nested table column in where clause

I have a staging table (table_B) with columns using nested table data types:

CREATE OR REPLACE TYPE nested_column_type AS OBJECT
          (
            abc_1            VARCHAR2(100),
            abc_2            VARCHAR2(100),
            col_id           VARCHAR2(100),
            tbl_id           NUMBER
          );

CREATE OR REPLACE TYPE nested_column_tab AS TABLE OF nested_column_type;

CREATE TABLE table_B
(col_id NUMBER,
 nested_column NESTED_COLUMN_TAB)
NESTED TABLE nested_column STORE AS column_nested);

I want to use nested_column in the where clause of a delete statement like this:

DELETE FROM table_A a
WHERE tbl_id = v_tbl_id
AND NOT EXISTS (SELECT col_id 
                  FROM TABLE(SELECT b.nested_column 
                               FROM table_B b 
                              WHERE tbl_id = v_tbl_id) 
                 WHERE col_id = a.col_id);

Table_A is my target table. My goal is to delete records from table_A where table_A.col_id NOT EXISTS in table_B.nested_column.col_id and tbl_id = v_tbl_id.

Adding more on what @Ted mentioned, you need to understand object name resolution steps and must use a table alias. This is mentioned here .

To avoid inner capture and similar problems resolving references, Oracle Database requires you to use a table alias to qualify any dot-notational reference to subprograms or attributes of objects.

In your case the query becomes:

DELETE FROM table_A a
      WHERE     tbl_id = v_tbl_id
            AND a.col_id NOT IN (SELECT b.col_id
                                   FROM table_B b
                                  WHERE (SELECT tb.tbl_id
                                           FROM TABLE (b.nested_column) tb) =a.tbl_id);

I think the below will put you in the right path:

select t.primary_id, nt.*
from table_b t, table (t.nested_column) nt

For any further clarifications please don't hesitate to contact me again here.

Ted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM