想在 PL/SQL 中使用 no_data_found 两次
Want to use no_data_found twice in PL/SQL
我在一个过程中有两个 select 语句(客户、产品)。
我的代码是这样的,
begin
....begin
....select xxxxx from customer
....exception
....when no_data_found then
....raise_application....(xxx1,'no customer found');
....end
select xxxxx from product
exception
when no_data_found then
raise_application....(xxx2,'no product found')
when others then
raise_application....(xxx3,sqlerrm);
end
没有找到商品也没有问题,
但问题是当没有客户ID时,
因为它执行了两个异常 xxx1,xxx3 但我只希望它执行 xxx1 然后退出程序。由于某种原因我需要 xxx3 异常所以我想保留它。
你们有什么想法吗?
此代码完全按照您的指示执行。在第一个块中,您会得到一个 NO_DATA_FOUND
异常,并且 NO_DATA_FOUND
的异常处理程序会引发您的 xxx1
异常。然后触发外部异常块中的 WHEN OTHERS
处理程序,因为您没有针对异常 xxx1
的特定处理程序,并且此 WHEN OTHERS
处理程序会引发您的 xxx3
异常。
如果您希望程序在引发 xxx1
异常后退出,您需要在外部异常块中编写一个处理程序来执行此操作:
begin
....begin
....select xxxxx from customer
....exception
....when no_data_found then
....raise_application....(xxx1,'no customer found');
....end
select xxxxx from product
exception
when no_data_found then
raise_application....(xxx2,'no product found')
WHEN xxx1 THEN -- added
RETURN;
when others then
raise_application....(xxx3,sqlerrm);
end
如果你想重新引发 xxx1
异常以便它可以在过程之外被捕获,那么你需要在异常处理程序中使用 RAISE
而不是 return:
begin
....begin
....select xxxxx from customer
....exception
....when no_data_found then
....raise_application....(xxx1,'no customer found');
....end
select xxxxx from product
exception
when no_data_found then
raise_application....(xxx2,'no product found')
WHEN xxx1 THEN -- added
RAISE; -- will re-raise the xxx1 exception so it can be handled elsewhere
when others then
raise_application....(xxx3,sqlerrm);
end
分享和享受。
我在一个过程中有两个 select 语句(客户、产品)。
我的代码是这样的,
begin
....begin
....select xxxxx from customer
....exception
....when no_data_found then
....raise_application....(xxx1,'no customer found');
....end
select xxxxx from product
exception
when no_data_found then
raise_application....(xxx2,'no product found')
when others then
raise_application....(xxx3,sqlerrm);
end
没有找到商品也没有问题,
但问题是当没有客户ID时, 因为它执行了两个异常 xxx1,xxx3 但我只希望它执行 xxx1 然后退出程序。由于某种原因我需要 xxx3 异常所以我想保留它。
你们有什么想法吗?
此代码完全按照您的指示执行。在第一个块中,您会得到一个 NO_DATA_FOUND
异常,并且 NO_DATA_FOUND
的异常处理程序会引发您的 xxx1
异常。然后触发外部异常块中的 WHEN OTHERS
处理程序,因为您没有针对异常 xxx1
的特定处理程序,并且此 WHEN OTHERS
处理程序会引发您的 xxx3
异常。
如果您希望程序在引发 xxx1
异常后退出,您需要在外部异常块中编写一个处理程序来执行此操作:
begin
....begin
....select xxxxx from customer
....exception
....when no_data_found then
....raise_application....(xxx1,'no customer found');
....end
select xxxxx from product
exception
when no_data_found then
raise_application....(xxx2,'no product found')
WHEN xxx1 THEN -- added
RETURN;
when others then
raise_application....(xxx3,sqlerrm);
end
如果你想重新引发 xxx1
异常以便它可以在过程之外被捕获,那么你需要在异常处理程序中使用 RAISE
而不是 return:
begin
....begin
....select xxxxx from customer
....exception
....when no_data_found then
....raise_application....(xxx1,'no customer found');
....end
select xxxxx from product
exception
when no_data_found then
raise_application....(xxx2,'no product found')
WHEN xxx1 THEN -- added
RAISE; -- will re-raise the xxx1 exception so it can be handled elsewhere
when others then
raise_application....(xxx3,sqlerrm);
end
分享和享受。