Oracle 具有异常处理的PL/SQL函数
我是PL/SQL的初学者。我需要编写一个包含以下详细信息的函数: 创建一个名为“查找事务类型”的函数,该函数将接受事务类型id作为输入。基于此输入,函数必须返回varchar类型的事务类型名称 功能名称:查找交易类型 输入参数:int中的事务类型id 设计规则: 1) 如果作为输入传递的事务类型id(即事务类型id)与事务表中的id匹配,则返回给定事务类型id的类型 2) 如果作为输入传递的事务类型id与事务表中的id不匹配,则会引发“no_data_found”异常,并将其显示为“no this type” 注意:使用变量来打印异常,而不是“dbms\u output.put\u line” ie:裁判员姓名:=‘没有这样的裁判员’ 我的解决办法是:Oracle 具有异常处理的PL/SQL函数,oracle,plsql,Oracle,Plsql,我是PL/SQL的初学者。我需要编写一个包含以下详细信息的函数: 创建一个名为“查找事务类型”的函数,该函数将接受事务类型id作为输入。基于此输入,函数必须返回varchar类型的事务类型名称 功能名称:查找交易类型 输入参数:int中的事务类型id 设计规则: 1) 如果作为输入传递的事务类型id(即事务类型id)与事务表中的id匹配,则返回给定事务类型id的类型 2) 如果作为输入传递的事务类型id与事务表中的id不匹配,则会引发“no_data_found”异常,并将其显示为“no thi
create or replace function find_transaction_type(transaction_type_id in integer) return varchar is
transaction_type_name varchar(255);
type_id integer;
error_msg varchar(255);
begin
error_msg := 'No such Type';
select id
into type_id
from transaction_type;
if type_id = transaction_type_id
then
select type
into transaction_type_name
from transaction_type
where id = transaction_type_id;
return(transaction_type_name);
else
raise no_data_found;
end if;
exception
when no_data_found then
raise_application_error(-10403, error_msg);
end;
/
我的代码怎么了
您不需要第一个select语句,也不需要if语句。只需让查询引发no_data_found异常。按表中各自的类型引用类型
create or replace function find_transaction_type (
transaction_type_id in transaction_type.transaction_type_id%type
)
return transaction_type.type%type is
transaction_type_name transaction_type.type%type;
begin
select type -- not a good column name
into transaction_type_name -- this should be the column name also
from transaction_type
where id = transaction_type_id;
return transaction_type_name;
exception
when no_data_found then
if transaction_type_id is null then
raise_application_error(-10403, "type argument is null");
else
raise_application_error(-10403, "type '" || transaction_type_id || "' not found");
end if;
end;
除了没有任何缩进使其可读之外?:-)好的,您的第一个
select
没有where子句,因此将返回transaction\u type
中的所有行,而不仅仅是您想要的行。