Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> RE: PL/SQL Question:Eliminate duplicate rows

RE: PL/SQL Question:Eliminate duplicate rows

From: Igor Neyman <ineyman_at_perceptron.com>
Date: Fri, 19 Sep 2003 12:49:48 -0800
Message-ID: <F001.005D0863.20030919124948@fatcity.com>


Check SQL Reference for "exception_clause" when creating Primary Key. Could help to do what you need just using SQL (no PL/SQL).

Igor Neyman, OCP DBA
ineyman_at_perceptron.com

-----Original Message-----
Mercadante, Thomas F
Sent: Friday, September 19, 2003 2:55 PM To: Multiple recipients of list ORACLE-L

Johann,

how about the following. what this does is, using the inner begin/end block, catches when an insert would fail because of the PK failure and ignores the error.

This is very quick and dirty - it will work fine if you are not working with
a huge amount of data.

declare

cursor c1 is
  select col1, col2
   from some_table;
begin
 for c1_rec in c1 loop
  begin
    insert into new_table(col1, col2)
      values (c1_rec.col1, c1_rec.col2);     exception

      when too_many_rows then
           null;

  end;
 end loop;
end;
/

If you are talking about lots and lots of data, you could easily query the
table you are inserting into, testing for the existence of the value you are
attempting to insert. If you find it, skip the insert. Like this:

declare
rec_count number;
cursor c1 is
  select col1, col2
   from some_table;
begin
 for c1_rec in c1 loop
  begin
    select count(*)

     into rec_count
     from new_table
     where col1 = c1_rec.col1;  -- this assumes that col1 is the pk!
     if rec_count = 0 then
       insert into new_table(col1, col2)
        values (c1_rec.col1, c1_rec.col2);
     end if;
    exception
      when too_many_rows then
           null;

  end;
 end loop;
end;
/

Good Luck!

Tom Mercadante
Oracle Certified Professional

-----Original Message-----
Sent: Friday, September 19, 2003 3:05 PM To: Multiple recipients of list ORACLE-L

Looking for an a sample cursor routine to load a PK enabled table to eliminate
any dupes from the load table.



Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com
--

Please see the official ORACLE-L FAQ: http://www.orafaq.net
--

Author: Johan Muller
  INET: bad_dba_at_yahoo.com
Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
--

Please see the official ORACLE-L FAQ: http://www.orafaq.net
--

Author: Mercadante, Thomas F
  INET: NDATFM_at_labor.state.ny.us
Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).

--

Please see the official ORACLE-L FAQ: http://www.orafaq.net
--

Author: Igor Neyman
  INET: ineyman_at_perceptron.com

Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). Received on Fri Sep 19 2003 - 15:49:48 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US