PL/SQL check if query returns empty

前端 未结 4 1715
闹比i
闹比i 2021-01-03 19:58

I\'m writing a procedure, and i need to check whether my select query returned an empty record or not. (In this example whether there is no x,y shelf)

How can i do t

4条回答
  •  情话喂你
    2021-01-03 20:40

    catch first not wanted condition and use count(1) because count(*) actually trying to count something and add rownum=1, you need only one first not matching condition. i use this statement.

           declare
          v_check number;
        begin
          select count(1) into v_check 
    from table
     where condition(something not wanted) AND rownum=1;
    
          if v_check = 0 then 
               --do something else
          elsif v_check = 1 --dont want theat
             rise some error or more..
          end if;
        end;
    

    For you just

    select count(1) into v_check from dual where exists (select count(1) 
        from table
         where condition AND rownum=1);
    
    if v_check = 0 then --nothing found
                     something...
              elsif v_check = 1 --found something
                something...
              end if;
            end;
    

提交回复
热议问题