PL/SQL Custom functions

Source: Internet
Author: User

Restrictions on calling a function from an SQL expression

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

is a storage function

Accept only in functions

Receives only the SQL data type that is affected, not the. PL data type

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

A function called from an SQL expression cannot contain a DML statement

A function that is called from a update/delete statement on a table T, the contents of the function cannot be contained in a DML on the same table T

Function contents cannot query the same table from a function called in an UPDATE or DELETE statement on a table T

A function called from an SQL statement cannot contain a statement that ends a thing

Calling a subroutine that violates a previous level of constraint is not allowed in a function


Custom functions

function function: Enter work 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 a function

DROP FUNCTION function_name

Show errors can display compilation errors (if any)

Show wage rank 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;

/

PL/SQL Custom functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.