Home » SQL & PL/SQL » SQL & PL/SQL » Multi Set operator (MULTISET UNION DISTINCT) in PL/SQL (11g)
Multi Set operator (MULTISET UNION DISTINCT) in PL/SQL [message #649438] |
Fri, 25 March 2016 13:15  |
vikram_2050
Messages: 10 Registered: June 2005 Location: bangalore
|
Junior Member |
|
|
Hi,
Having an issue with multi set operators in pl/sql.
MULTISET UNION working fine.
set serveroutput on
declare
TYPE rec IS RECORD (
name varchar2(1000)
);
TYPE ttbl IS TABLE OF rec;
p ttbl:=ttbl();
t ttbl:=ttbl();
x ttbl:=ttbl();
begin
for i in 1..10
loop
p.extend;
p(i).name:=i;
end loop;
for j in 1..15
loop
t.extend;
t(j).name:=j;
end loop;
x:= t MULTISET UNION p;
for k in x.first..x.last
loop
dbms_output.put_line(x(k).name);
end loop;
end;
Output:
PL/SQL procedure successfully completed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
MULTISET UNION DISTINCT throwing an error in below code
set serveroutput on
declare
TYPE rec IS RECORD (
name varchar2(1000)
);
TYPE ttbl IS TABLE OF rec;
p ttbl:=ttbl();
t ttbl:=ttbl();
x ttbl:=ttbl();
begin
for i in 1..10
loop
p.extend;
p(i).name:=i;
end loop;
for j in 1..15
loop
t.extend;
t(j).name:=j;
end loop;
x:= t MULTISET UNION DISTINCT p;
for k in x.first..x.last
loop
dbms_output.put_line(x(k).name);
end loop;
end;
error Message:
Error report -
ORA-06550: line 22, column 5:
PLS-00306: wrong number or types of arguments in call to 'MULTISET_UNION_DISTINCT'
ORA-06550: line 22, column 1:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
I am not able to understand why it is throwing this error message.
Any help really appreciated.
thanks in advance
|
|
|
|
|
|
|
|
Re: Multi Set operator (MULTISET UNION DISTINCT) in PL/SQL [message #649445 is a reply to message #649444] |
Fri, 25 March 2016 14:45   |
 |
Barbara Boehmer
Messages: 9105 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
I get the same in 12c. I don't know that I would label this a bug. It might be considered expected behavior. The MULTISET UNION DISTINCT works fine with scalar data types. I believe the issue with non-scalar data types, is that, in order to perform a distinct, a comparison must be made, and in order to compare non-scalar types, there must be a map function, which requires a SQL type, as demonstrated below.
SCOTT@orcl> CREATE OR REPLACE TYPE rec AS OBJECT
2 (name VARCHAR2(1000),
3 MAP MEMBER FUNCTION sort_key RETURN VARCHAR2);
4 /
Type created.
SCOTT@orcl> SHOW ERRORS
No errors.
SCOTT@orcl> CREATE OR REPLACE TYPE BODY rec
2 AS
3 MAP MEMBER FUNCTION sort_key RETURN VARCHAR2
4 IS
5 BEGIN
6 RETURN name;
7 END;
8 END;
9 /
Type body created.
SCOTT@orcl> SHOW ERRORS
No errors.
SCOTT@orcl> CREATE OR REPLACE TYPE ttbl AS TABLE OF rec;
2 /
Type created.
SCOTT@orcl> SHOW ERRORS
No errors.
SCOTT@orcl> declare
2 p ttbl:=ttbl();
3 t ttbl:=ttbl();
4 x ttbl:=ttbl();
5 begin
6 for i in 1..10
7 loop
8 p.extend;
9 p(i) := rec(i);
10 end loop;
11
12 for j in 1..15
13 loop
14 t.extend;
15 t(j) := rec(j);
16 end loop;
17
18 x:= t MULTISET UNION DISTINCT p;
19
20 for k in x.first..x.last
21 loop
22 dbms_output.put_line(x(k).name);
23 end loop;
24 end;
25 /
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PL/SQL procedure successfully completed.
|
|
|
Re: Multi Set operator (MULTISET UNION DISTINCT) in PL/SQL [message #649446 is a reply to message #649444] |
Fri, 25 March 2016 14:49   |
Solomon Yakobson
Messages: 3305 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
No, it is not a bug. Type ttbl is nested table of composite type - record. DISTINCT requires comparing two records for equality and Oracle doesn't support record comparison:
SQL> declare
2 r1 emp%rowtype;
3 r2 emp%rowtype;
4 begin
5 if r1 = r2
6 then
7 dbms_output.put_line('Equal');
8 else
9 dbms_output.put_line('Not equal or null');
10 end if;
11 end;
12 /
if r1 = r2
*
ERROR at line 5:
ORA-06550: line 5, column 11:
PLS-00306: wrong number or types of arguments in call to '='
ORA-06550: line 5, column 5:
PL/SQL: Statement ignored
SQL>
SY.
|
|
|
|
Goto Forum:
Current Time: Mon May 19 21:31:41 CDT 2025
|