PL/SQL自定义函数

从SQL表达式中调用函数的限制

为了从SQL表达式中调用函数,一个用户定义函数必须:

是存储函数

只接受IN函数

只接收有受的SQL数据类型,而不接受PL/SQL数据类型

返回数据类型为有效的SQL数据类型,而非PL/SQL特殊的类型

从SQL表达式中调用的函数不能包含DML语句

从在表T上的UPDATE/DELETE语句中调用的函数,函数内容不能包含在同一个表T上的DML

从在表T上的UPDATE或DELETE语句中调用的函数,函数内容不能查询同一个表

从SQL语句中调用的函数不能包含结束事物的语句

在函数中不允许调用违反上一级约束的子程序


自定义函数

函数功能:输入工号,返回薪水

create or replace function get_sal

(p_id IN employees.employee_id%type)

return number

is

v_salary employees.salary%type:=0;

begin

select salary into v_salary from employees where employee_id=p_id;

return v_salary;

end get_sal;

/

select get_sal(employee_id) from employees;

Tax函数

create or replace function tax(p_value in number)

return number is

begin

return(p_value*0.08);

end tax;

/

select employee_id,last_name,salary,tax(salary) from employees where department_id=100;

删除函数

DROP FUNCTION FUNCTION_NAME

Show errors可显示编译错误(如果有的话)

显示工资等级函数

create or replace function f_grade(v_eno in employees.employee_id%type)

return varchar2 is

v_sal employees.salary%type;

v_result varchar2(50);

begin

select salary into v_sal from employees where employee_id=v_eno;

case

when v_sal>0 and v_sal<2000 then

v_result:='little case';

when v_sal>2000 and v_sal<5000 then

v_result:='medium case';

when v_sal>5000 then

v_result:='big case';

else

v_result:='no case';

end case;

return v_result;

end f_grade;

/

猜你喜欢

转载自www.cnblogs.com/kawashibara/p/8995598.html