oracle – 如果不是函数PLSQL中的EXISTS
发布时间:2021-03-06 08:31:48 所属栏目:百科 来源:网络整理
导读:我有一个函数,它在打开游标之前有三个If / Then语句. If / Then语句在打开游标之前检查有效性. 我想再添加一个If / Then有效性检查,但是,它比其他更复杂一点.下面是一个示例,我有块评论我想添加的内容: begin if not procedure.validation_function (vari
|
我有一个函数,它在打开游标之前有三个If / Then语句. If / Then语句在打开游标之前检查有效性. 我想再添加一个If / Then有效性检查,但是,它比其他更复杂一点.下面是一个示例,我有块评论我想添加的内容: begin
if not procedure.validation_function (<variable>,<condition>=TRUE) then
return variable2;
end if;
/* if not exists
(
SELECT 'x' FROM table1
WHERE table1_id = variable1_id
AND trunc(sysdate) < trunc(table1_date + 60)
) then
return variable2;
end if; */
open cursor(<argument>);
fetch cursor into <variable>;
close cursor;
return <variable>;
end;
我的问题是我来自一个T-SQL世界,而我在PL / SQL中发现if not exists命令不起作用.有没有办法,我可以在函数中有一个If NO_DATA_FOUND语句,我使用SELECT? 有没有办法在其中嵌套另一个函数,所以我可以: begin
SELECT ....
FROM ....
WHERE ....
if NO_DATA_FOUND then
return variable2;
end if;
end;
解决方法Exists条件只能在SQL语句中使用,不能直接在PL / SQL中使用.有几种选择:>在select语句中使用带有exists条件的case表达式: SQL> declare
2 l_exists number(1);
3 begin
4 select case
5 when exists(select 1
6 from employees
7 where department_id = 1)
8 then 1
9 else 0
10 end into l_exists
11 from dual;
12
13 if (l_exists = 1)
14 then
15 dbms_output.put_line('exists');
16 else
17 dbms_output.put_line(q'[doesn't exist]');
18 end if;
19 end;
20 /
doesn't exist
PL/SQL procedure successfully completed
>或者(如果有多条记录符合匹配条件,则需要rownum来保证只返回一条记录): SQL> declare
2 l_exists number;
3 begin
4
5 select 1
6 into l_exists
7 from employees
8 where department_id = 100
9 and rownum = 1;
10
11 dbms_output.put_line('exists');
12
13 exception
14 when no_data_found
15 then dbms_output.put_line(q'[doesn't exist]');
16 end;
17 /
exists
PL/SQL procedure successfully completed
(编辑:南平站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

