tags:

views:

189

answers:

4

A)

select decode(count(*), 0, 'N', 'Y') rec_exists
from (select 'X'
      from dual
      where exists (select 'X'
                    from sales
                    where sales_type = 'Accessories'));

B)

select decode(count(*), 0, 'N', 'Y') rec_exists
from (select 'X'
      from sales
      where sales_type = 'Accessories'); 

C) Something else (specify)

EDIT: It was hard to pick the "correct" answer, as the best approach depends on what you want to do after checking if the value exists, as pointed out by APC. I ended up picking the answer by RedFilter, since I had originally envisioned this check as a function by itself.

+3  A: 
select case 
            when exists (select 1 
                         from sales 
                         where sales_type = 'Accessories') 
            then 'Y' 
            else 'N' 
        end as rec_exists
from dual;
RedFilter
Looks pretty good. Thanks!
HappyCoder4U
You're missing a `FROM dual`.
Jeffrey Kemp
Thanks, I have fixed that.
RedFilter
A: 
select decode(count(*), 0, 'N', 'Y') rec_exists 
      from sales 
      where sales_type = 'Accessories'; 
Randy
A: 
SELECT 'Y' REC_EXISTS             
FROM SALES                       
WHERE SALES_TYPE = 'Accessories'

The result will either be 'Y' or NULL. Simply test against 'Y'

EvilTeach
The result will be 'Y' if one record exists. If multiple records exist it will be a TOO_MANY_ROWS exception. If no records exist it will be a NO_DATA_FOUND exception.
APC
@APC: Not unless you SELECT INTO a single variable. The query as written is pure SQL and does not raise any exceptions.
ObiWanKenobi
@ObiWanKenobi - how do you test for a value unless you select it into a variable?
APC
@APC: Maybe you put the SQL statement in a cursor and loop through it, or maybe you run the SQL from a client program and iterate through a record set (yes, I know the question is about PL/SQL). Or maybe you add PL/SQL exception handlers. Not the most efficient implementation, to be sure, but it's not wrong per se.
ObiWanKenobi
+3  A: 

What is the underlying logic you want to implement? If, for instance, you want to test for the existence of a record to determine to insert or update then a better choice would be to use MERGE instead.

If you expect the record to exist most of the time, this is probably the most efficient way of doing things (although the CASE WHEN EXISTS solution is likely to be just as efficient):

begin
    select null into dummy
    from sales
    where sales_type = 'Accessories'
    and rownum = 1;

    --  do things here when record exists
    ....        

exception
    when no_data_found then
        -- do things here when record doesn't exists
        .....
end;

You only need the ROWNUM line if SALES_TYPE is not unique. There's no point in doing a count when all you want to know is whether at least one record exists.

APC
+1 Nice answer. I use this check frequently in different circumstances... that's why I was wondering what was the best way to do it. Sometimes I just want to return a flag to the front end. Other times, I do something different, like return a value if it exists, and if not, return the next value of a sequence. I guess you're right - the best way might depend on the situation.
HappyCoder4U