首页 > 解决方案 > no_data_found 时出现异常,我想将两个变量分配给 null

问题描述

我编写了一个选择查询来获取两个列值,以防出现异常,我想在 ORACLE STORED PROCEDURE 中将这些变量分配给 null。

例如

select column A, column B into l_a, l_b 
  from ......
exception 
  when no_data_found then 
    l_a: = null and l_b := null ;

标签: oracleexceptionplsql

解决方案


有一个错字。它必须:=代替: =. 您还需要用分号分隔语句:;。通常语句在单独的行中。

l_a := null;
l_b := null;

除此之外,您不需要分配 null。变量最初被分配为空,如果没有找到数据,它们将保持为空。

当然,如果变量在值保持之前被分配了一个值。所以你也可以这样做。

l_a := null;
l_b := null;
select column A, column B into l_a, l_b 
  from ......
-- do something with l_a and l_b here
exception 
  when no_data_found then 
    null; -- ignore and do nothing

但通往罗马的道路总是有很多


推荐阅读