Table of Contents
1、建立函数,不带任何参数
2、建立函数:带有IN参数
4、建立IN OUT参数
Home Database Mysql Tutorial 开发Oracle 函数

开发Oracle 函数

Jun 07, 2016 pm 03:21 PM
oracle function develop data return

函数用于返回特定的数据。如果在应用程序中,经常需要执行SQL语句来返回特定数据,那么可以基于这些操作建立特定的函数。建立函数的语法如下: CREATE [OR REPLACE] FUNCTION function_name (argument1 [model] datatype1, argument2 [model] datatype2, ...

     函数用于返回特定的数据。如果在应用程序中,经常需要执行SQL语句来返回特定数据,那么可以基于这些操作建立特定的函数。建立函数的语法如下:

CREATE [OR REPLACE]  FUNCTION function_name
      (argument1 [model] datatype1,
        argument2 [model] datatype2,
        ....)
   RETURN datatype
 IS|AS
     PL/SQL  Block;     
Copy after login

如上所示,function_name用于指定函数的名称;argument1、argument2等则用于指定函数的参数,注意当指定参数的数据类型时,不能指定其长度;RETURN子句用于指定函数返回值的数据类型;IS|AS用于开始一个PLS/QL块。注意,当建立函数时,在函数头部必须要带有RETURN子句,在函数体内必须要包含一条RETURN语句。另外,当建立函数时,既可以指定输入参数(IN),也可以指定输出参数(OUT),以及输入输出参数(IN OUT)。下面通过实例说明建立函数,以及使用各种参数模式的方法。

1、建立函数,不带任何参数

   当建立函数时,既可以带有参数,也可以不带参数。下面以建立用于显示当前数据库用户名的函数为例,说面建立该种函数的方法。示例如下:   

CREATE OR REPLACE FUNCTION get_user
  RETURN VARCHAR2
IS
   v_user VARCHAR2(100);
BEGIN
	  SELECT username INTO v_user FROM user_users;
	  RETURN v_user;
END;	 
Copy after login

建立了函数get_user之后,就可以在应用程序中调用该函数了。因为函数有返回值,所以它只能作为表达式的一部分来调用。调用示例如下:

示例一:使用变量接收函数返回值

SQL> var v1 varchar2(100);
SQL> exec :v1:=get_user;
 
PL/SQL procedure successfully completed
v1
---------
SCOTT
 
SQL> print v1;
v1
---------
SCOTT
Copy after login

示例二:在SQL语句中直接调用函数

SQL> select get_user from dual;
 
GET_USER
--------------------------------------------------------------------------------
SCOTT
Copy after login

示例三:使用包DBMS_OUTPUT调用函数

SQL> set serveroutput on;
SQL> exec dbms_output.put_line('当前数据库用户:'||get_user);
 
当前数据库用户:SCOTT
 
PL/SQL procedure successfully completed
Copy after login

 

2、建立函数:带有IN参数

   当建立函数时,通过使用输入参数,可以将应用程序的数据传递到函数中,最终通过执行函数可以将结果返回到应用程序中。下面以建立返回雇员工资的函数为例,说明建立带有输入参数函数的方法。示例如下:

CREATE OR REPLACE FUNCTION  get_sal(name IN VARCHAR2)
RETURN NUMBER
AS
  v_sal emp.sal%TYPE;
BEGIN
    SELECT sal INTO v_sal  FROM emp
      WHERE upper(ename)=upper(name);
      RETURN v_sal;
  EXCEPTION
      WHEN NO_DATA_FOUND THEN
      raise_application_error(-20000,'该雇员不存在');       
END; 
Copy after login

 在建立了get_sal之后,就可以在应用程序中调用该函数了。调用该函数的示例如下:

示例一:输入存在的雇员

<strong>SQL> var sal NUMBER;
SQL> exec :sal:=get_sal('scott');
 
PL/SQL procedure successfully completed
sal
---------
1200
 
SQL> print sal;
sal
---------
1200</strong>
Copy after login

示例二:输入不存在的雇员

SQL> exec :sal:=get_sal('mary');
 
begin :sal:=get_sal('mary'); end;
 
ORA-20000: 该雇员不存在
ORA-06512: 在 "SCOTT.GET_SAL", line 11
ORA-06512: 在 line 2
Copy after login


3、建立函数:带有OUT参数

一般情况下,函数只需要返回单个数据。如果希望使用函数同同时返回多个数据,例如同时返回雇员名和工资那么就需要使用输出参数了。为了在函数中使用输出参数,必须要指定OUT参数模式。下面以建立用于返回雇员所在部门名和岗位的函数为例,说明建立带有OUT参数函数的方法。示例如下:

 CREATE OR REPLACE FUNCTION get_info
  (name VARCHAR2,title OUT VARCHAR2)
  RETURN VARCHAR2
AS
  deptname dept.dname%TYPE;
BEGIN 
   SELECT a.job,b.dname INTO title,deptname
     FROM emp a,dept b
     WHERE a.deptno=b.deptno
     AND upper(a.ename)=upper(name);     
    RETURN  deptname;
EXCEPTION
     WHEN NO_DATA_FOUND  THEN
      raise_application_error(-20000,'该雇员不存在');   
END;         
        
Copy after login

当建立了函数get_info 后,就可以在应用程序中调用该函数了。注意,因为该函数,带有OUT参数,所以不能在SQL语句中,调用该函数,而必须要定义变量接收OUT参数和函数的返回值。在SQL*PLUS中调用函数get_info的示例如下:

SQL> var job varchar2(20)
SQL> var dname varchar2(20)
SQL> exec :dname:=get_info('scott',:job);
PL/SQL procedure successfully completed
dname
---------
ANALYST
job
---------
ANALYST   
Copy after login

如上所示:除了输出函数自定义返回的值,也返回了OUT参数的返回值。
 

4、建立IN OUT参数

   建立函数时,不仅可以指定IN和OUT参数,也可以指定IN OUT参数。IN OUT 参数,也被成为输入输出参数。使用这种参数时,在调用函数之前需要通过变量给该种参数传递数据,在调用结束之后Oracle会将函数的部分结果通过该变量传递给应用程序。下面以计算两个数值相除的结果的函数Result为例,说明在函数中使用IN OUT参数的方法。示例如下:

CREATE OR REPLACE FUNCTION result_num
(num1 NUMBER,num2 IN OUT NUMBER)
RETURN NUMBER
IS
  v_result NUMBER(6);
  v_remainder NUMBER;
BEGIN 
    v_result:=num1/num2;
    v_remainder:=MOD(num1,num2);  
    num2:=v_remainder;
    RETURN v_result;
 EXCEPTION 
      WHEN 
         ZERO_DIVIDE  THEN
          raise_application_error(-20000,'不能除0');
 END;            
Copy after login

 注意,因为该函数带有IN OUT 参数,所以不能再SQL语句中调用该函数,而必须使用变量为IN OUT参数传递数值并接收数据。另外,还需要定义变量接收函数返回值。调用该函数的示例如下:

SQL> var result2 number;
SQL> var result1 number;
SQL> var result2 number;
SQL> exec :result2:=30;
 
PL/SQL procedure successfully completed
result2
---------
30
 
SQL> exec :result1:=result_num(100,:result2);
 
PL/SQL procedure successfully completed
result1
---------
3
result2
---------
10
Copy after login


 

 

 

 

 

 

 

 


 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to export oracle view How to export oracle view Apr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

See all articles