一个函数
create or replace function get_tax_1(p_salary number)
return number as
tax_salary number;
V_NUMBER NUMBER;
begin
tax_salary := p_salary-FLOOR(p_salary);
IF tax_salary <= 0.3 THEN
V_NUMBER :=FLOOR(p_salary);
RETURN V_NUMBER;
end if;
IF tax_salary >= 0.4 AND tax_salary <= 0.7 THEN
SELECT (CEIL(p_salary) + FLOOR(p_salary)) / 2 INTO V_NUMBER FROM DUAL;
return V_NUMBER;
end if;
IF tax_salary >= 0.8 THEN
V_NUMBER :=CEIL(p_salary);
return V_NUMBER;
end if;
end get_tax_1;
报错信息如下
SQL> SELECT get_tax_1(3.2),get_tax_1(3.3),get_tax_1(3.4),get_tax_1(3.7),get_tax_1(3.9),get_tax_1(3.34) FROM dual;
SELECT get_tax_1(3.2),get_tax_1(3.3),get_tax_1(3.4),get_tax_1(3.7),get_tax_1(3.9),get_tax_1(3.34) FROM dual
ORA-06503: PL/SQL: 函数未返回值
ORA-06512: 在 "RITONG.GET_TAX_1", line 19
经过复查及别人的提示,是由于函数逻辑的不严谨造成的,原因就是3.34传进去之后,没有返回值
所以改为如下之后
create or replace function get_tax(p_salary number)
return number as
tax_salary number;
V_NUMBER NUMBER;
begin
tax_salary := p_salary-FLOOR(p_salary);
IF round(tax_salary,1) <= 0.3 THEN
V_NUMBER :=FLOOR(p_salary);
RETURN V_NUMBER;
end if;
IF round(tax_salary,1) >= 0.4 AND round(tax_salary,1) <= 0.7 THEN
SELECT (CEIL(p_salary) + FLOOR(p_salary)) / 2 INTO V_NUMBER FROM DUAL;
return V_NUMBER;
end if;
IF round(tax_salary,1) >= 0.8 THEN
V_NUMBER :=CEIL(p_salary);
return V_NUMBER;
end if;
end get_tax;
再执行,不再报错
SQL> SELECT get_tax(3.2),get_tax(3.3),get_tax(3.4),get_tax(3.7),get_tax(3.9),get_tax(3.34) FROM dual;
GET_TAX(3.2) GET_TAX(3.3) GET_TAX(3.4) GET_TAX(3.7) GET_TAX(3.9) GET_TAX(3.34)
------------ ------------ ------------ ------------ ------------ -------------
3 3 3.5 3.5 4 3
虽然不报错,但是其实逻辑依旧不够严谨,这是客户提出的ap规则中的一种“三退四进七退八进”的规则
下面一段是我朋友写得,我觉得我还有需要学习的地方
CREATE OR REPLACE FUNCTION fun_update(p_data NUMBER)
RETURN NUMBER IS
v_data1 NUMBER;
BEGIN
IF p_data - FLOOR(p_data) <=0.3 THEN
v_data1:=FLOOR(p_data);
RETURN v_data1;
ELSIF p_data - CEIL(p_data) <=0.3 THEN
v_data1:=CEIL(p_data);
RETURN v_data1;
ELSE
v_data1:=FLOOR(p_data)+0.5;
RETURN v_data1;
END IF;
END fun_update;
执行结果如下
SQL> SELECT fun_update(3.2),fun_update(3.3),fun_update(3.4),fun_update(3.7)
2 ,fun_update(3.9),fun_update(3.34) FROM dual;
FUN_UPDATE(3.2) FUN_UPDATE(3.3) FUN_UPDATE(3.4) FUN_UPDATE(3.7) FUN_UPDATE(3.9) FUN_UPDATE(3.34)
--------------- --------------- --------------- --------------- --------------- ----------------
3 3 4 4 4 4
所以,学习Ing