sql - 消除 PL/SQL 函数中的重复代码片段
问题描述
我需要关于优化我的功能的帮助/建议。如何避免重复这段代码:
select 1 from order_attr oa
where oa.order_id = o.order_id and ea.order_attr_name = 'administrationId'
下面是我的 PL/SQL 函数(这是一个示例,在实际代码中,该片段重复了很多次):
begin
select count (distinct o.order_id)
into :result
from orders o
where o.ext_order_id = :ExtOrderId and o.service_id not like 'tech#%' and (:Include = 1 and exists
(
select 1 from order_attr oa
where oa.order_id = o.order_id and ea.order_attr_name = 'administrationId'
)
or :Include = 0 and not exists
(
select 1 from order_attr oa
where oa.order_id = o.order_id and ea.order_attr_name = 'administrationId'
));
exception
when others then
:result := 0;
end;
解决方案
左连接将在这里为您提供帮助
from orders o
left join order_attr oa
on oa.order_id = o.order_id and ea.order_attr_name = 'administrationId'
where o.ext_order_id = :ExtOrderId and o.service_id not like 'tech#%'
and (:Include = 1 and oa.order_id is not null -- exists
or :Include = 0 and oa.order_is is null; -- not exists
我没有要测试的数据,但我希望你能明白
推荐阅读
- email - Redmine 问题 | 错误 - :MailHandler:验证失败:开始时间不能为空
- spring-boot - Spring Boot 连接到 IBM MQ Cloud
- html - 我无法在 Angular/html 脚本中打印对象的值
- c# - 如何区分 Cosmos DB 更改源中的插入和更新
- optimization - LLVM IR(中间表示)中的“inttoptr”指令是否无效?
- discord - 如何向服务器的所有成员发送直接消息?
- matrix-multiplication - Matrix 矩阵产品操作 OpenMDAO
- node.js - 如何从 Scival Api 检索作者指标?
- python - 程序立即跳转到我的代码中的 else 子句
- python - 使用python在双引号文件夹中提取rar文件