检查行是否存在的最快方法

BnJ*_*BnJ 2 sql oracle plsql

在a之前select SOMETHING into v_something,我想知道我的查询是否返回一行.

这是一个很好的方法,但select如果行存在,则成本为2 :

select count(1) into isRowExists from PERSON where CONDITION='Something';

if (isRowExists > 0) then
    select NAME into v_name from PERSON where CONDITION='Something';
else
    raise name_not_found;
end if;

select count(1) into isRowExists from CAR where CONDITION='Something';

if (isRowExists > 0) then
    select MODEL into v_model from CAR where CONDITION='Something';
else
    raise model_not_found;
end if;
Run Code Online (Sandbox Code Playgroud)

或类似的东西 :

select NAME into v_name from PERSON where CONDITION='Something';

select MODEL into v_model from CAR where CONDITION='Something';

exception
    when no_data_found then
        --do_something
Run Code Online (Sandbox Code Playgroud)

但是用这种方法,我不知道问题是来自PERSON还是CAR......

还有其他解决方案吗?就像发送一个参数exception

6to*_*ton 6

你可以这样做:

BEGIN
    BEGIN
        select NAME into v_name from PERSON where CONDITION='Something';
    exception
        when no_data_found then
        --do_something
    END;

    BEGIN
        select MODEL into v_model from CAR where CONDITION='Something';
    exception
        when no_data_found then
            --do_something
    END;
END;
/
Run Code Online (Sandbox Code Playgroud)