首页 > 解决方案 > 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?

谢谢。

标签: sqloracledynamic-sqlcase-whenplsql-package

解决方案


 SELECT firstname, lastname
 FROM employees
 WHERE 0 = :p_deptid
 OR dept_id = :p_deptid

推荐阅读