我有一个 pl sql 代码,它按顺序执行三个查询以确定匹配级别并执行一些逻辑
问题是 - 当第一个查询没有结果(完全有效的场景)时,我得到 ORA-01403 找不到数据。
我知道我需要在 NO_DATA_FOUND 时加入 [Exception 子句]-但是如何添加它并继续下一个查询?
PL/SQL Code
SELECT A into PARAM A FROM SAMPLE WHERE SOME CONDITION;
-- GOT ORA-01403 No data found HERE
MATCH_LEVEL =1;
if A is null then
do some logic;
end if
SELECT A INTO PARAM_B FROM SAMPLE WHERE SOME OTHER CONDITION
MATCH_LEVEL =2
if A is null then
do some logic 2;
end if
SELECT A INTO PARAM_B FROM SAMPLE WHERE SOME OTHER CONDITION
MATCH_LEVEL =3
if A is null then
do some logic 3;
end if
END PL/SQL Code
Run Code Online (Sandbox Code Playgroud)
Declare
--your declarations
begin
SELECT A into PARAM A FROM SAMPLE WHERE SOME CONDITION;
-- GOT ORA-01403 No data found HERE
Begin
MATCH_LEVEL =1;
if A is null then
do some logic;
end if;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line ('Error...');
END;
--- and son on for other blocks
end;
Run Code Online (Sandbox Code Playgroud)
只要你的周围SELECT INTO用begin-end;
begin
-- your faulty statement here
Exception
When NO_DATA_FOUND Then
-- Do what you want or nothing
WHEN TOO_MANY_ROWS THEN
-- what if you get more then one row? and need specific handler for this
When OTHERS Then
-- do something here or nothing (optional - may happen if you have more than your SELECT INTO between 'begin' and 'Exception')
end;
Run Code Online (Sandbox Code Playgroud)
这就像try块PL/Sql
使用此技术,您可以记录语句失败的原因。
| 归档时间: |
|
| 查看次数: |
14054 次 |
| 最近记录: |