postgresql - 函数不返回任何值且不引发异常
问题描述
下面的函数既不返回值也不引发异常。
create or replace function get_custid(p_customerNum varchar2)
RETURNS text AS $$
DECLARE
cust_id customer.customer_num%TYPE;
begin
raise notice '%', message_text;
select customer_num into cust_id
from customer
where customer_num = p_customerNum;
return cust_id;
exception
when OTHERS then
raise notice '%', message_text;
raise;
end $$ language plpgsql;
select get_custid('Ab12345') from dual;
-- 客户编号存在但未返回任何行。
select get_custid('DDDDDDD') from dual;
-- 客户号不存在但不会异常阻塞
解决方案
在PL/pgSQL中,如果指定,SELECT INTO
则仅在错误的行数上引发异常。STRICT
create or replace function get_custid(p_customerNum varchar)
RETURNS text AS $$
DECLARE
cust_id customer.customer_num%TYPE;
begin
raise notice '%', 'message_text';
select customer_num into strict cust_id
from customer
where customer_num = p_customerNum;
return cust_id;
exception
when OTHERS then
raise notice '%', 'message_text';
raise;
end $$ language plpgsql;
推荐阅读
- python - 使用 Pyinstaller 创建的二进制文件失败:“No module named ...”、“Failed to execute script ...”使用 Pipenv 时
- html - 是否可以将 Bootstrap 集成到 App Maker 中?
- sql - 选择 Max() 太慢了
- sql - MS Access 在数据表中显示 vba 选择查询
- javascript - AngularJS:从父母那里获取孩子的状态名称
- python - PyTorch RuntimeError 大小的参数 2 无效
- ansible - Ansible - Centos 机器,ansible_lsb 为空
- sql - 未知列数的 SQL Server 动态数据透视
- excel - VBA - 重命名模块
- javascript - 如何检索文本内容并在复选框输入上显示