Quickest query to check for the existence of a row in Oracle?

后端 未结 4 1985
北荒
北荒 2021-02-05 22:31

I\'m using Oracle, and I have a very large table. I need to check for the existence of any row meeting some simple criteria. What\'s the best way to go about this using simple

4条回答
  •  温柔的废话
    2021-02-05 23:05

    Using COUNT(*) is OK if you also use rownum=1:

    declare
       l_cnt integer;
    begin
       select count(*)
       into   l_cnt
       from   x
       where  x.col_a = value_a 
       and    x.col_b = value_b
       and    rownum = 1;
    end;
    

    This will always return a row, so no need to handle any NO_DATA_FOUND exception. The value of l_cnt will be 0 (no rows) or 1 (at least 1 row exists).

提交回复
热议问题