【问题标题】:Optional condition in Oracle 10gOracle 10g 中的可选条件
【发布时间】:2018-06-07 16:11:15
【问题描述】:

对于虚拟测试,我想在 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?

谢谢。

【问题讨论】:

    标签: sql oracle dynamic-sql case-when plsql-package


    【解决方案1】:
     SELECT firstname, lastname
     FROM employees
     WHERE 0 = :p_deptid
     OR dept_id = :p_deptid
    

    【讨论】:

    • 哇,这太俗气了。非常感谢。我记得我以前见过这个,但不知怎的,我无法弄清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多