sql - Oracle 10g 中的可选条件
问题描述
对于虚拟测试,我想在 Web 表单中显示员工列表。
Web 表单上有一个下拉列表,其中包含一个简短的部门列表,如下所示:
All Depts
Sales Dept
Marketing Dept
Communication Dept
HR Dept
Finance Dept
IT Dept
的下拉项的All Depts
值为 0。
以下小提琴向您展示了我正在尝试做的事情:
http://sqlfiddle.com/#!4/59d1f/2
我知道我可以这样做:
IF (deptid = 0) THEN
select firstname, lastname from employees;
ELSE
select firstname, lastname from employees where deptid = :p_deptid
END IF;
但我的实际情况有一个更复杂的选择查询,它涉及多个表的连接。所以,我不想用重复的代码弄乱我的脚本。
我可以使用 CASE WHEN 实现我的目标吗?还是我必须使用动态 SQL?
谢谢。
解决方案
SELECT firstname, lastname
FROM employees
WHERE 0 = :p_deptid
OR dept_id = :p_deptid
推荐阅读
- flutter - 尝试从博客获取帖子到颤动的移动应用程序时出错
- apache - .htaccess 仅在 GET 参数存在时重定向
- testing - Foundry 转换的 Python 单元测试?
- parent-child - 创建动态父子层次结构?
- google-cloud-platform - Dask Hub/JupyterHub - 无法启动 Python 内核
- firebase-realtime-database - 如何在 Firebase Recycler Adapter 中膨胀不同的布局
- python - 如何将黑白像素图像转换为文本?
- angular - Angular v6:如何根据选择值禁用 formArray 的特定 formControl?
- delphi - Delphi10.1 Berlin .. 在打开 dblookupcombobox 菜单的情况下关闭表单时出现错误
- javascript - 在 React 中插入内容后,防止关注 Expand More 按钮