從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;
/