Home » SQL & PL/SQL » SQL & PL/SQL » comparing collections
comparing collections [message #617848] Fri, 04 July 2014 01:16 Go to next message
purnima1
Messages: 79
Registered: June 2014
Member
declare

cursor c_lri_data is select c1,c2 from tab1 order by c1;
type tab_lri_data is table of c_lri_data%rowtype index by binary_integer;
t_lri_data tab_lri_data;--lri rec

cursor c_ce_data is select c1,c2 from tab1 order by c1 ;
type tab_ce_data is table of c_ce_data%rowtype index by binary_integer;
t_ce_data tab_ce_data;---ce rec
begin
open c_lri_data;
fetch c_lri_data bulk collect into t_lri_data;
close c_lri_data;

open c_ce_data;
fetch c_ce_data bulk collect into t_ce_data;
close c_ce_data;


IF t_lri_data = t_ce_data THEN
DBMS_OUTPUT.PUT_LINE (
'both collections are equal');


END IF;
end;
/


getting and error while compilation
wrong number or types of arguments in call to '='

can't we compare collections like this



[Edit MC: fix title typo]

[Updated on: Fri, 04 July 2014 10:05] by Moderator

Report message to a moderator

Re: coparing collections [message #617853 is a reply to message #617848] Fri, 04 July 2014 01:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

There are many ways but you can't use = on a collection (this type of collection).

PL/SQL User's Guide and Reference

[Updated on: Fri, 04 July 2014 01:28]

Report message to a moderator

Re: coparing collections [message #617860 is a reply to message #617853] Fri, 04 July 2014 01:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

More, I didn't realize, you cannot compare 2 different types.

icon7.gif  Re: coparing collections [message #617881 is a reply to message #617860] Fri, 04 July 2014 03:40 Go to previous messageGo to next message
purnima1
Messages: 79
Registered: June 2014
Member
Thanks Michel for your help
Re: coparing collections [message #617883 is a reply to message #617881] Fri, 04 July 2014 03:42 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So have you the solution?
How do you do it?

Re: coparing collections [message #618048 is a reply to message #617883] Mon, 07 July 2014 05:03 Go to previous messageGo to next message
purnima1
Messages: 79
Registered: June 2014
Member
in that case i need to compare field by field ..
Re: coparing collections [message #618050 is a reply to message #618048] Mon, 07 July 2014 05:16 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You can use the same type for both queries.

Re: coparing collections [message #618060 is a reply to message #618050] Mon, 07 July 2014 07:27 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
For instance:
SQL> create table emp2 as select * from emp where ename != 'KING';

Table created.

SQL> update emp2 set sal=sal+100 where mod(empno,2)=0;

10 rows updated.

SQL> commit;

Commit complete.

SQL> create or replace type employee is object (
  2      ename varchar2(10),
  3      sal   number(7,2)
  4    )
  5  /

Type created.

SQL> create or replace type employees is table of employee
  2  /

Type created.

SQL> declare
  2    empl  employees;
  3    empl2 employees;
  4    nb    pls_integer := 0;
  5  begin
  6    select employee(ename,sal) bulk collect into empl from emp;
  7    select employee(ename,sal) bulk collect into empl2 from emp2;
  8    dbms_output.new_line;
  9    select 1 into nb
 10    from ( select * from table(empl)
 11           minus
 12           select * from table(empl2) )
 13    where rownum = 1;
 14    dbms_output.put_line ('There are differences');
 15  exception
 16    when no_data_found then
 17      dbms_output.put_line ('No differences');
 18  end;
 19  /

There are differences

PL/SQL procedure successfully completed.

SQL> truncate table emp2;

Table truncated.

SQL> insert into emp2 select * from emp;

14 rows created.

SQL> declare
  2    empl  employees;
  3    empl2 employees;
  4    nb    pls_integer := 0;
  5  begin
  6    select employee(ename,sal) bulk collect into empl from emp;
  7    select employee(ename,sal) bulk collect into empl2 from emp2;
  8    dbms_output.new_line;
  9    select 1 into nb
 10    from ( select * from table(empl)
 11           minus
 12           select * from table(empl2) )
 13    where rownum = 1;
 14    dbms_output.put_line ('There are differences');
 15  exception
 16    when no_data_found then
 17      dbms_output.put_line ('No differences');
 18  end;
 19  /

No differences

PL/SQL procedure successfully completed.

Of course you can do it in a single query and have not to use collections to do this:
SQL> declare
  2    nb    pls_integer := 0;
  3  begin
  4    dbms_output.new_line;
  5    select 1 into nb
  6    from ( select * from emp
  7           minus
  8           select * from emp2)
  9    where rownum = 1;
 10    dbms_output.put_line ('There are differences');
 11  exception
 12    when no_data_found then
 13      dbms_output.put_line ('No differences');
 14  end;
 15  /

No differences

PL/SQL procedure successfully completed.

Re: coparing collections [message #618063 is a reply to message #618060] Mon, 07 July 2014 07:34 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
The above will work correctly only if tables have PK.

SY.
Re: coparing collections [message #618172 is a reply to message #618063] Wed, 09 July 2014 01:28 Go to previous message
purnima1
Messages: 79
Registered: June 2014
Member
ohh that is great solution thanks
Previous Topic: What is Analytical function?
Next Topic: Packages
Goto Forum:
  


Current Time: Wed Apr 24 20:56:12 CDT 2024