[英]Conditional Procedure oracle Pl/sql
我想创建一个具有以下条件的过程
$$
select 'Y'
from dual
where exists
(select 'Y'
from pa_expenditure_items_all paei, pa_expenditures_all pae
where paei.expenditure_id = pae.expenditure_id
and pae.incurred_by_person_id =&person_id
and paei.cost_distributed_flag = 'N')
$$
如果条件返回 Null 那么它应该显示一条错误消息,然后是一个错误。 对于错误消息,我正在使用 dbms_output.put_line,对于错误,我正在使用异常,那么任何人都可以帮助我解决这些问题
我正在尝试这些
create or replace procedure validate_terminate
declare
l_yes out varchar2 (1);
null_found exception;
is
begin
select 'Y'
into l_yes
from dual
where exists
(select 'Y'
from pa_expenditure_items_all paei, pa_expenditures_all pae
where paei.expenditure_id = pae.expenditure_id
and pae.incurred_by_person_id =&person_id
and paei.cost_distributed_flag = 'N');
if l_yes is null
then
dbms_output_put_line ('Condition is not met');
raise null_found;
end if;
exception
when null_found then
dbms_output_put_line('CANNOT PROCESS TERMINATE');
end;
声明过程时使用IS
而不是DECLARE
- 不要同时使用两者。
您无需检查l_yes
是否为 null - 如果您的 select 返回 0 行,它将自动引发NO_DATA_FOUND
异常。 抓住那个。
您不需要第二次 select(来自双重)。 第一个 select 工作正常。
声明中的l_yes
之后不要out
。 这只是l_yes varchar2(1);
.
它是dbms_output.put_line
。
使用绑定变量:person_id
而不是替换变量&person_id
set define off
create or replace procedure validate_terminate
is
l_yes varchar2(1);
begin
select 'Y'
into l_yes
from dual
where exists
(select 'Y'
from pa_expenditure_items_all paei, pa_expenditures_all pae
where paei.expenditure_id = pae.expenditure_id
and pae.incurred_by_person_id = :person_id
and paei.cost_distributed_flag = 'N');
exception
when NO_DATA_FOUND then
dbms_output.put_line ('Condition is not met');
dbms_output.put_line('CANNOT PROCESS TERMINATE');
raise;
end;
/
您的变量声明和异常处理不正确。
试试下面的代码:
create or replace procedure validate_terminate
declare
l_yes varchar2(1);
--null_found exception;
is
begin
select 'Y'
into l_yes
from pa_expenditure_items_all paei, pa_expenditures_all pae
where paei.expenditure_id = pae.expenditure_id
and pae.incurred_by_person_id =&person_id
and paei.cost_distributed_flag = 'N';
exception
when no_data_found then
dbms_output_put_line ('Condition is not met');
dbms_output_put_line('CANNOT PROCESS TERMINATE');
end;
/
干杯!!
PL/SQL 不是交互式编程语言。 是实现SQL及相关代码实现其他客户端调用的工具。 是那些处理用户输入和其他 UI 事物的客户端。
因此,编写程序的惯用方式是将 PERSON_ID 作为参数传递....
create or replace procedure validate_terminate
(p_person_id in pa_expenditure_items_all.incurred_by_person_id%type )
is
l_yes out varchar2 (1);
null_found exception;
is
begin
select 'Y'
into l_yes
from dual
where exists
(select 'Y'
from pa_expenditure_items_all paei
,pa_expenditures_all pae
where paei.expenditure_id = pae.expenditure_id
and pae.incurred_by_person_id =p_person_id
and paei.cost_distributed_flag = 'N');
if l_yes is null
then
dbms_output_put_line ('Condition is not met');
raise null_found;
end if;
exception
when null_found then
dbms_output_put_line('CANNOT PROCESS TERMINATE');
end;
然后你像这样调用这个过程(假设你想使用 SQL*Plus,正如你选择的符号所暗示的那样):
exec validate_terminate(&person_id)
注意我假设 PERSON_ID 是数字; 如果它是一个字符串,你需要引用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.