case 语句的“ORA-00905:缺少关键字”

Posted

技术标签:

【中文标题】case 语句的“ORA-00905:缺少关键字”【英文标题】:"ORA-00905: missing keyword" for case statement 【发布时间】:2015-03-22 02:26:11 【问题描述】:
DECLARE
var_employee_number VARCHAR2(30);
var_employee_name VARCHAR2(30);
var_salary VARCHAR2(30);
BEGIN
SELECT employee_number,employee_name,
CASE var_salary
    WHEN salary <= 10000 THEN salary:= salary* .02 /*I am getting an error over here*/
    WHEN salary BETWEEN 10000 AND 15000 THEN salary := salary * .02
    WHEN salary BETWEEN 15000 AND 20000 THEN salary := salary * .025
    END AS salary INTO var_employee_number,var_employee_name,var_salary
FROM EMPLOYEES
WHERE employee_number := var_employee_number;
dbms_output.put_line('The salary is '||var_salary);
END;

如果您的薪水在特定范围内,我正在尝试写一个案例陈述,根据薪水范围,薪水应乘以 20% 或 25%。

【问题讨论】:

为什么要将数字放入VARCHAR2 变量中?而var_employee_number的价值又从何而来? 【参考方案1】:

您的CASE 语句有几个问题:

CASE var_salary -- you don't need var_salary here
    WHEN salary <= 10000 THEN salary:= salary* .02 /*I am getting an error over here*/ -- you don't need "salary:=" here
    WHEN salary BETWEEN 10000 AND 15000 THEN salary := salary * .02
    WHEN salary BETWEEN 15000 AND 20000 THEN salary := salary * .025
END AS salary

所以纠正这些问题应该只是:

CASE WHEN salary <= 10000 THEN salary * 0.2
  WHEN salary BETWEEN 10000 AND 15000 THEN salary * 0.2
  WHEN SALARY BETWEEN 15000 AND 20000 THEN salary * 0.25
END AS salary

您可能还注意到&lt;= 10000BETWEEN 10000 AND 15000 的情况相同,因此可以合并。

还请记住,BETWEEN ... AND 包含在内,因此值 15000 将返回 15000 * 0.2 (3000),而不是 * 0.25

另外,请注意我更正了你的数字...... 20% 是 0.2,而不是 .02

【讨论】:

感谢您的帮助!!

以上是关于case 语句的“ORA-00905:缺少关键字”的主要内容,如果未能解决你的问题,请参考以下文章

甲骨文。使用 case 语句时缺少关键字。错误 00905

ORA-00905: 缺少关键字(约束外键)

Oracle 错误:ORA-00905:缺少关键字

ORA-00905: 缺少关键字错误 oracle

ORA-00905: 缺少关键字

ORA-00905 缺少关键字