Oracle:集合类型列值存在性检查及关联查询ORA-01722报错解决
Let's break down why your query is failing and how to fix it.
Why You're Getting ORA-01722
Your original query tries to compare a single numeric value (a.PK) against a collection type (b.Table_A_PK) directly in the IN clause. Oracle can't implicitly convert a collection into a list of individual numbers, so it throws the "invalid number" error—this is a type mismatch issue.
Correct Query Approaches
Since Table_A_PK is a collection (like a nested table or VARRAY), you need to unnest it to get individual values that can be compared to a.PK. Here are two reliable ways to do this:
1. Use TABLE() to Unnest the Collection
This method expands the collection into rows of values, which you can then use in your IN clause:
SELECT a."SIZE" FROM A a WHERE a.PK IN ( SELECT column_value FROM B b, TABLE(b.Table_A_PK) );
The TABLE() function converts the collection into a relational table with a single column column_value (the individual PKs stored in the collection).
2. Use MEMBER OF for Direct Collection Membership Check
Oracle provides the MEMBER OF operator specifically to check if a value exists within a collection:
SELECT DISTINCT a."SIZE" FROM A a JOIN B b ON a.PK MEMBER OF b.Table_A_PK;
The DISTINCT keyword ensures you don't get duplicate values (though in your sample data, this isn't an issue, it's a safe practice for broader cases).
Expected Result
Both queries will return the desired values:
5 3 8
Note: SIZE is a reserved word in Oracle, so wrapping the column name in double quotes ("SIZE") avoids any potential syntax conflicts.
内容的提问来源于stack exchange,提问作者Junaid

