PL/SQL custom function

Restrictions on calling functions from SQL expressions

In order to call a function from an SQL expression, a user-defined function must:

is the stored function

Only accepts IN functions

Accepts only valid SQL data types, not PL/SQL data types

The return data type is a valid SQL data type, not a PL/SQL special type

Functions called from SQL expressions cannot contain DML statements

A function called from an UPDATE/DELETE statement on table T, the content of the function cannot be contained in DML on the same table T

From a function called in an UPDATE or DELETE statement on table T, the contents of the function cannot query the same table

Functions called from SQL statements cannot contain statements that end things

Calls to subroutines that violate previous constraints are not allowed in functions


custom function

Function function: input job number, return salary

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 function

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;

delete function

DROP FUNCTION FUNCTION_NAME

Show errors displays compilation errors (if any)

show salary grade function

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;

/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325329379&siteId=291194637