What's the most efficient way to check if a record exists in Oracle?

前端 未结 10 1976
刺人心
刺人心 2020-12-23 03:10

A)

select decode(count(*), 0, \'N\', \'Y\') rec_exists
from (select \'X\'
      from dual
      where exists (select \'X\'
                    from sales
           


        
相关标签:
10条回答
  • 2020-12-23 03:58
    SELECT 'Y' REC_EXISTS             
    FROM SALES                       
    WHERE SALES_TYPE = 'Accessories'
    

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

    0 讨论(0)
  • 2020-12-23 04:02
    select decode(count(*), 0, 'N', 'Y') rec_exists 
          from sales 
          where sales_type = 'Accessories'; 
    
    0 讨论(0)
  • 2020-12-23 04:03

    Simply get a count of the record(s) you're looking for. If count > 0 then record(s) exist.

        DECLARE
        rec_count NUMBER := 0;
    
        BEGIN
    
        select count(*) 
        into rec_count  
        from EMPLOYEETABLE  
        WHERE employee_id = inEMPLOYEE_ID  
        AND department_nbr = inDEPARTMENT_NBR;  
    
    
        if rec_count > 0 then  
           {UPDATE EMPLOYEETABLE}
        else  
           {INSERT INTO EMPLOYEETABLE}  
        end if;  
    
        END;
    
    0 讨论(0)
  • 2020-12-23 04:08
    select case 
                when exists (select 1 
                             from sales 
                             where sales_type = 'Accessories') 
                then 'Y' 
                else 'N' 
            end as rec_exists
    from dual;
    
    0 讨论(0)
提交回复
热议问题