You Asked
Hi,Tom
If I have a table including a column which is a varry type,
such as
create table myarray as varray(5) of number(10);
create table mytest(col1 number,col2 myarray)
insert into table mytest(1,myarray(1,2,3,4,5));
I try to use the values of varray type column as condition in where_clause like
"select * from mytest where col2=myarray(1,2,3,4,5);",
but I failed . How I can do
and Tom said...
From the PLSQL reference:
<quote>
Comparing Whole Collections
Nested tables and varrays can be atomically null, so they can be tested for nullity, as the following example shows:
DECLARE
TYPE Staff IS TABLE OF Employee;
members Staff;
BEGIN
...
IF members IS NULL THEN ... -- condition yields TRUE;
END;
However, collections cannot be compared for equality or inequality. For instance, the following IF condition is illegal:
DECLARE
TYPE Clientele IS TABLE OF Customer;
group1 Clientele := Clientele(...);
group2 Clientele := Clientele(...);
BEGIN
...
IF group1 = group2 THEN -- causes compilation error
...
END IF;
END;
This restriction also applies to implicit comparisons. For example, collections cannot appear in a DISTINCT, GROUP BY, or ORDER BY list.
</quote>
So, you need to use an OBJECT type to do this and, much like you would in Java or C++, you must create an "operator" to do the comparision -- Just as in Java you have to use:
stringVariable.isEqual( "some other string" )
we must use
objectCol.isEqual( some other object instance )
Here is a small example:
ops$tkyte@8i> create type myarray as varray(5) of number(10);
2 /
Type created.
ops$tkyte@8i> create type myObject as object
2 ( array myArray,
3 member function isEqual( p_other in myArray default null)
return number
4 )
5 /
Type created.
ops$tkyte@8i>
ops$tkyte@8i> create type body myObject
2 as
3 member function isEqual( p_other in myArray )
return number
4 is
5 begin
6 if ( self.array.count <> p_other.count )
7 then
8 return 0;
9 end if;
10
11 for i in 1 .. self.array.count
12 loop
13 if ( self.array(i) <> p_other(i) )
14 then
15 return 0;
16 end if;
17 end loop;
18 return 1;
19 end;
20 end;
21 /
Type body created.
ops$tkyte@8i> create table mytest(col1 number,col2 myObject)
2 /
Table created.
ops$tkyte@8i> insert into mytest
values ( 1, myObject( myarray(1,2,3,4,5) ) )
2 /
1 row created.
ops$tkyte@8i> select t.col2.array Array from mytest T
2 /
ARRAY
-------------------------
MYARRAY(1, 2, 3, 4, 5)
ops$tkyte@8i> select t.col1, t.col2.array array
2 from mytest t
3 where t.col2 = myobject( myarray(1,2,3,4,5) )
4 /
no rows selected
No data found, object1 <> object2...
ops$tkyte@8i>
ops$tkyte@8i> select t.col1, t.col2.array array
2 from mytest T
3 where t.col2.isEqual( myArray(1,2,3,4,5) ) = 1
4 /
COL1 ARRAY
---------- -------------------------
1 MYARRAY(1, 2, 3, 4, 5)
But out compare works OK
Rating
(4 ratings)
Is this answer out of date? If it is, please let us know via a Comment