昨天我在写一些任务,突然发现我真的不知道正确且被接受的方式当我使用 PL/SQL 时检查表中是否存在行。
例如,让我们使用表:
PERSON (ID, Name);
显然我can't do(除非有一些秘密方法)比如:
BEGIN
IF EXISTS SELECT id FROM person WHERE ID = 10;
-- do things when exists
ELSE
-- do things when doesn't exist
END IF;
END;
所以我解决这个问题的标准方法是:
DECLARE
tmp NUMBER;
BEGIN
SELECT id INTO tmp FROM person WHERE id = 10;
--do things when record exists
EXCEPTION
WHEN no_data_found THEN
--do things when record doesn't exist
END;
但是我不知道这是否是公认的做法,或者是否有更好的检查方法,如果有人可以与我分享他们的智慧,我将非常感激。
我不会将常规代码推入异常块中。只需检查是否存在满足您条件的行,然后从那里继续:
declare
any_rows_found number;
begin
select count(*)
into any_rows_found
from my_table
where rownum = 1 and
... other conditions ...
if any_rows_found = 1 then
...
else
...
end if;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)