Link to home
Start Free TrialLog in
Avatar of claghorn
claghorn

asked on

oracle sql exists clause

Is there a more efficient/faster or more elegant way to do this statement with just the exists clause? I use these in triggers all over the place so I'd like to improve it if possible.
SQL Developer SQL Tuning Advisor says "no recomendation to improve it"
set serveroutput on;
declare l_count number;
begin
SELECT COUNT(*)
    INTO l_count
    FROM dual
    WHERE EXISTS
      (SELECT employee_id FROM hr.employees WHERE manager_id = 114);

if l_count > 0 then
 dbms_output.put_line ('found some');
end if;
    
end;

Open in new window

Avatar of ajexpert
ajexpert
Flag of United States of America image

How about this?
set serveroutput on;

DECLARE
   l_count   NUMBER :=0;
BEGIN
   SELECT   COUNT (employee_id)
     INTO   l_count
     FROM   hr.employees
    WHERE   manager_id = 114;

   IF l_count > 0 THEN
      DBMS_OUTPUT.put_line ('found some');
   END IF;
END;

Open in new window

I agree with the solution by ajexpert.

I would also ensure that the hr.employees table has an index on the column manager_id.
ASKER CERTIFIED SOLUTION
Avatar of ob2s
ob2s
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> efficient/faster way?

You should check explain plan

I  am sure involving dual table has more cost...