How to use Functions in Oracle

Source: Internet
Author: User

1. Definition

Function: accepts 0 or more input parameters, has a return value, and the data type of the returned value is defined when the function is created.

function Example 1

CREATE OR REPLACE FUNCTION Tax (p_empno in number)

Return number is

V_sal number;

V_returnvalue number;

BEGIN

SELECT Sal into V_sal

From Emp1 WHERE empno=p_empno;

V_returnvalue: = v_sal*0.08;

return v_returnvalue;

End tax;

Return income tax for employees

Call function

DECLARE

V_tax number;

BEGIN

V_tax: = Tax (&empno);

Dbms_output. Put_Line (' TAX is ' | | V_tax);

End;


function Example 2

To create a function:

CREATE OR REPLACE FUNCTION get_salary (

P_dept_no number,

P_num out number)

Return number is

V_sum number;

BEGIN

SELECT SUM (SAL), COUNT (*) into V_sum, P_num

from EMP WHERE deptno=p_dept_no;

return v_sum;

End Get_salary;

/

Called in the Pl/sql block:

DECLARE

V_NUM2 number;

V_sum number;

BEGIN

V_sum: =get_salary (&&DEPTNO,V_NUM2);

Dbms_output. Put_Line (&deptno| | ' Department sum salary is ' | |    v_sum| | ' Employee: ' | | V_NUM2);

End;

/

2. Delete function

We can remove the unwanted function from the drop function command as follows:

DROP FUNCTION [user.] Function_name;

Author: Oracle Small-mixed son

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/database/Oracle/

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.