PL/SQL check if query returns empty

前端 未结 4 1707
闹比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:31

    Exception handling would be the first thing I think of too, but if you don't want to burden yourself with handling all the different cases, I tend to use a select count(*) from. The nice thing with count(*) is that it ALWAYS returns something (assuming your query is legal). In this case you could count to see if it returns 0 (no matches) or more (in which case you can do something.

    You could get something like this:

    declare
      v_count number := 0;
    begin
      select count(*) into v_count from table where condition;
    
      if v_count = 0 then
          --do something
      else
          --do something else
      end if;
    end;
    

提交回复
热议问题