大佬教程收集整理的这篇文章主要介绍了Oracle PL/SQL:如果列至少有“一个空值”或“未找到数据”,则返回 true,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果列至少有一个值为空或未找到任何外键列值的数据,如何返回 TRUE?
尝试在互联网上搜索答案,但找不到任何与此组合有关的答案。所以把它贴在这里。
以下是数据:表名 - @H_830_4@mY_EMP
+----+------------+-----------+---------------------------+--------+
| ID | first_name | last_name | email | fk_col |
+----+------------+-----------+---------------------------+--------+
| 1 | Hurleigh | Stove | | 1 |
| 2 | Joline | | | 1 |
| 3 | Martynne | Honatsch | mhonatsch2@infoseek.co.jp | 2 |
| 4 | Willyt | Fullylove | wfullylove3@hhs.gov | 2 |
| 5 | Joelly | | jferrIDay4@twitpic.com | 3 |
+----+------------+-----------+---------------------------+--------+
尝试编写函数并测试代码。这是进入函数的代码:
DECLARE
v_verify numbER(2);
BEGIN
SELECT disTinCT 1
INTO v_verify
FROM my_emp
WHERE ( last_name IS NulL
OR email IS NulL )
AND fk_col = :custom_fk;
DBMS_output.Put_line('Return True');
EXCEPTION
WHEN no_data_found THEN
DBMS_output.Put_line('Return false');
END;
当参数 :custom_fk = 1 时,值返回 TRUE(这是正确的)
当参数 :custom_fk = 2 时,值返回 falSE(这是正确的)
当参数 :custom_fk = 700 时,值返回 falSE(根据我的要求不正确)--这里我希望值返回 true 并且无法弄清楚如何调整代码以符合我的要求。
希望我能在这里得到一些帮助。
下面是创建表和插入数据的代码:
/* create table */
create table MY_EMP(
id number(5) NOT NulL,first_name varchar2(100) NOT NULL,last_name varchar2(100),email VARCHAR2(100),fk_col numbER(5),CONSTraiNT "MY_EMP_PK" PRIMARY KEY ("ID")
);
/*Insert data*/
INSERT INTO my_emp(ID,first_name,last_name,email,fk_col)
VALUES(1,'Hurleigh','Stove','',1);
INSERT INTO my_emp(ID,fk_col)
VALUES(2,'Joline',fk_col)
VALUES(3,'Martynne','Honatsch','mhonatsch2@infoseek.co.jp',2);
INSERT INTO my_emp(ID,fk_col)
VALUES(4,'Willyt','Fullylove','wfullylove3@hhs.gov',fk_col)
VALUES(5,'Joelly','jferrIDay4@twitpic.com',3);
谢谢,
理查
这行吗?
SQL> with temp as
2 (SELEct count(*) cnt
3 from (SELEct disTinct 1 as val
4 from my_emp
5 where fk_col = &&par_fk_col
6 and ( last_name is null
7 or email is null
8 )
9 union all
10 SELEct disTinct 2
11 from my_emp
12 where fk_col = &&par_fk_col
13 )
14 )
15 SELEct case when cnt = 1 then 'false'
16 else 'true'
17 end as result
18 from temp;
结果
Enter value for par_fk_col: 1
RESUL
-----
true
SQL> undefine par_fk_col
SQL> /
Enter value for par_fk_col: 2
RESUL
-----
false
SQL> undefine par_fk_col
SQL> /
Enter value for par_fk_col: 3
RESUL
-----
true
SQL> undefine par_fk_col
SQL> /
Enter value for par_fk_col: 700
RESUL
-----
true
SQL>
这一切都在一个 SELEct
语句中。如果您正在编写函数,检查 fk_col
是否存在于单独的查询中可能更简单;如果没有,立即返回 true
。如果是,则检查其他条件。
(顺便说一句,感谢您提供测试用例。这种情况太罕见了,我感到非常惊讶!)
[EDIT] 将该语句“转换”为 pl/sqL 并不困难。我建议使用 函数,而不是匿名 pl/sqL 块。
SQL> create or replace function f_test (par_fk_col in my_emp.fk_col%typE)
2 return varchar2
3 is
4 retval varchar2(10);
5 begin
6 with temp as
7 (SELEct count(*) cnt
8 from (SELEct disTinct 1 as val
9 from my_emp
10 where fk_col = par_fk_col
11 and ( last_name is null
12 or email is null
13 )
14 union all
15 SELEct disTinct 2
16 from my_emp
17 where fk_col = par_fk_col
18 )
19 )
20 SELEct case when cnt = 1 then 'false'
21 else 'true'
22 end
23 into retval
24 from temp;
25
26 return retval;
27 end;
28 /
Function created.
测试:
SQL> SELEct f_test(1) from dual;
F_TEST(1)
---------------------------------------
true
SQL> SELEct f_test(2) from dual;
F_TEST(2)
---------------------------------------
false
SQL> SELEct f_test(700) from dual;
F_TEST(700)
---------------------------------------
true
SQL>
,
SELECT case when count(*)=1 then 'true' else 'false' end ret_value
from dual
where not exists(
SELECT null
FROM my_emp
WHERE fk_col = :custom_fk
having count(*)>0
and count(*)=count(last_name)
and count(*)=count(email)
);
示例:
SQL> ho cat tests/2.sql
SELECT case when count(*)=1 then 'true' else 'false' end ret_value
from dual
where not exists(
SELECT null
FROM my_emp
WHERE fk_col = &1
having count(*)>0
and count(*)=count(last_name)
and count(*)=count(email)
);
SQL> @tests/2.sql 1
RET_V
-----
true
SQL> @tests/2.sql 2
RET_V
-----
false
SQL> @tests/2.sql 3
RET_V
-----
true
SQL> @tests/2.sql 700
RET_V
-----
true
以上是大佬教程为你收集整理的Oracle PL/SQL:如果列至少有“一个空值”或“未找到数据”,则返回 true全部内容,希望文章能够帮你解决Oracle PL/SQL:如果列至少有“一个空值”或“未找到数据”,则返回 true所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。