oracle - 用于检查不确定值的 PL/SQL 构造
问题描述
我正在使用以下查询来查找 PL/SQL 过程中年月两个日期的差异:-
...
years number;
months number ;
yrs_string varchar2;
months_string varchar2;
begin
...
select trunc(months_between(sysdate,date_of_birth)/12) into years,
months_between(sysdate,date_of_birth) -
12*trunc(months_between(sysdate,date_of_birth)/12) into months from
tbl_student where student id = incoming_id;
---test 1
if years = 0 then
yrs_string := 'N/A or less than a yr";
else
yrs_string := years || 'years';
end if;
--- test 2
if years = null then
....
但是,如果 date_of_birth 未在表中输入,则测试 1 和测试 2 均失败。如果用户未输入 date_of_birth,如何检查年份值或 PL/SQL 返回的年份?
解决方案
可能最好的方法是将出生日期选择为局部变量,然后对此进行测试:
...
l_dob date;
years number;
months number ;
yrs_string varchar2;
months_string varchar2;
begin
...
select date_of_birth into l_dob
from tbl_student
where student id = incoming_id;
-- new test
if l_dob is null then
yrs_string := 'Date of birth is null';
else
years := trunc(months_between(sysdate, l_dob)/12);
months := months_between(sysdate, l_dob) - 12*trunc(months_between(sysdate, l_dob)/12);
---test 1
if years = 0 then
yrs_string := 'N/A or less than a yr";
else
yrs_string := years || 'years';
end if;
--- test 2
if years is null then
....
推荐阅读
- javascript - 如何使用此 discord.js 代码检查提到的用户邀请?
- symfony - Symfony 4.3 中的 FOSUserBundle 注册表单
- c - C:参数传递与返回值的优点?
- android - OnResume() 在 MainActivity 中导致无限循环
- css - R中的CSS选择器
- json - 将 JSON 对象解码为 Swift Dictionary 保留键
- ios - Swift - 关闭模态 ViewController 后重新加载 TableView
- django - 在管理视图中显示内联项的计数
- python - Pygame 延迟参数
- python-3.x - 排序列表的二进制搜索,在列表中找到与用户提供的目标值最接近的值 Python 3