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

Home -> Community -> Usenet -> c.d.o.server -> Re: count(*) vs. a cursor when determining the existing record on primary key

Re: count(*) vs. a cursor when determining the existing record on primary key

From: Connor McDonald <connor_mcdonald_at_yahoo.com>
Date: Wed, 18 Jun 2003 18:02:36 +0800
Message-ID: <3EF038BC.3D42@yahoo.com>


jgk wrote:
>
> If you have a table with a primary key and you want to determine if a
> particular primary key exists what is the fastest way to accomplish
> this?
>
> In Steve Feuerstein's great book "Oracle PL/SQL Best Practices"
> SQL-08: Use COUNT only when the actual number of occurrences is
> needed...he gives some case(s) where a cursor should be used instead
> of the count(*) function.
>
> If you have a table. The table has a primary key field. If you want
> to test for existence of a matching primary key is it better to use
> the COUNT function and see if there is a match > 0 (it would always be
> 1 or zero)...or should a cursor be used?
>
> Thanks,
>
> JGK
select count(*)
into v
from dual
where exists (
  select null
  from big_table
  where col = ... )

or

select null
into v
from big_table
where col = ...
and rownum = 1

hth
connor

-- 
=========================
Connor McDonald
http://www.oracledba.co.uk

"Some days you're the pigeon, some days you're the statue"
Received on Wed Jun 18 2003 - 05:02:36 CDT

Original text of this message

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