Oracle存储过程
3、在Oracle中对存储过程的调用 (1)过程调用方式一 Sql代码 declare realsalemp.sal%type; realname varchar (40); realjob varchar (40); begin //过程调用开始 realsal:=1100; realname:= '' ; realjob:= 'CLERK' ; runbyparmeters(realsal,realname,rea
3、在Oracle中对存储过程的调用
(1)过程调用方式一
Sql代码
- declare
- realsal emp.sal%type;
- realname varchar(40);
- realjob varchar(40);
- begin //过程调用开始
- realsal:=1100;
- realname:='';
- realjob:='CLERK';
- runbyparmeters(realsal,realname,realjob);--必须按顺序
- DBMS_OUTPUT.PUT_LINE(REALNAME||' '||REALJOB);
- END; //过程调用结束
(2)过程调用方式二
Sql代码
- declare
- realsal emp.sal%type;
- realname varchar(40);
- realjob varchar(40);
- begin //过程调用开始
- realsal:=1100;
- realname:='';
- realjob:='CLERK';
- --指定值对应变量顺序可变
- runbyparmeters(sname=>realname,isal=>realsal,sjob=>realjob);
- DBMS_OUTPUT.PUT_LINE(REALNAME||' '||REALJOB);
- END; //过程调用结束
(3)过程调用方式三(SQL命令行方式下)
Sql代码
- 1、SQL>exec proc_emp('参数1','参数2');//无返回值过程调用
- 2、SQL>var vsal number
- SQL> exec proc_emp ('参数1',:vsal);// 有返回值过程调用
- 或者:call proc_emp ('参数1',:vsal);// 有返回值过程调用
4、JAVA调用Oracle存储过程
(1)不带输出参数情况,过程名称为pro1,参数个数1个,数据类型为整形数据
Java代码
- import java.sql. * ;
- public class ProcedureNoArgs{
- public static void main(String args[]) throws Exception{
- //加载Oracle驱动
- DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
- //获得Oracle数据库连接
- Connection conn = DriverManager.getConnection
- ("jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd " );
- //创建存储过程的对象
- CallableStatement c = conn.divpareCall( " {call pro1(?)} " );
- //给Oracle存储过程的参数设置值 ,将第一个参数的值设置成188
- c.setInt( 1 , 188 );
- // 执行Oracle存储过程
- c.execute();
- conn.close();
- }
- }
(2)带输出参数的情况,过程名称为pro2,参数个数2个,数据类型为整形数据,返回值为整形类型。
Java代码
- import java.sql.*;
- public class ProcedureWithArgs {
- public static void main(String args[]) throws Exception{
- //加载Oracle驱动
- DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
- //获得Oracle数据库连接
- Connection conn = DriverManager.getConnection
- ("jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd ");
- //创建Oracle存储过程的对象,调用存储过程
- CallableStatement c=conn.divpareCall("{call pro2(?,?)}");
- //给Oracle存储过程的参数设置值 ,将第一个参数的值设置成188
- c.setInt(1,188);
- //注册存储过程的第二个参数
- c.registerOutParameter(2,java.sql.Types.INTEGER);
- c.execute(); //执行Oracle存储过程
- //得到存储过程的输出参数值并打印出来
- System.out.println (c.getInt(2));
- conn.close();
- }
- }
二、 函数
1、基本语法规则
Sql代码
- create or replace function (Name in type, Name in type, ...)
- return number
- is
- Result number;
- begin
- return (Result);
- end ;
2、具体事例(查询出empno=7935的sal值)
Sql代码
- create or replace function ret_emp_sal(v_ename varchar2)
- return number
- is
- v_sal number(7,2);
- begin
- select nvl(sal,0) into v_sal from emp where lower(ename)=lower(v_ename);
- return v_sal;
- end;
3、函数调用:
Sql代码
- SQL> var vsla number
- SQL> call ret_emp_sal('7935') into :vsal;
4、与存储过程的区别
(1)返回值的区别,函数有1个返回值,而存储过程是通过参数返回的,可以有多个或者没有
(2)调用的区别,函数可以在查询语句中直接调用,而存储过程必须单独调用.
(3)使用场景的区别,函数一般情况下是用来计算并返回一个计算结果
而存储过程一般是用来完成特定的数据操作(比如修改、插入数据库表或执行某些DDL语句等等)
三、包
包用于组合逻辑相关的过程和函数,它由包规范和包体两个部分组成。包规范用于定义公用的常量、变量、过程
和函数,创建包规范可以使用CREATE PACKAGE命令,创建包体可以使用CREATE PACKAGE BODY.
1、创建包规范
Sql代码
- create package emp_pkg is
- procedure emp_update_ename(v_empno varchar2,v_ename varchar2);
- function emp_get_sal(v_empno varchar2) return number;
- end;
2、创建包体
Sql代码
- create or replace package body emp_pkg
- is
- // 存储过程
- procedure emp_update_ename
- (
- v_empno varchar2,
- v_ename varchar2
- )
- is
- vename varchar2(32);
- begin
- update emp set ename=v_ename where empno=v_empno;
- commit;
- select ename into vename from emp where empno=v_empno;
- dbms_output.put_line('雇员名称:'||vename);
- end;
- // 函数
- function emp_get_sal
- (
- v_empno varchar2
- )
- return number is
- vsal number(7,2);
- begin
- select sal into vsal from emp where empno=v_empno;
- return vsal;
- end;
- nd;
3、包调用
在没有创建包规范就创建包体,会失败,要使用包,必须先创建包规范,然后在创建包体。
当要调用包的过程和函数时,在过程和函数的名称前加上包名作为前缀(包名.子程序名称),
而如果要访问其他方案的包时需要在包的名称前加上方案的名称(方案名称.包名.子程序名称)。
(1)调用包函数
Sql代码
- SQL> var vsla number
- SQL> call emp_pkg.emp_get_sal('7935') into :vsal;
(2)调用包存储过程
Sql代码
- SQL> exec emp_pkg.emp_update_ename('7935','helong');

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

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.

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

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

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

The steps to open an Oracle database are as follows: Open the Oracle database client and connect to the database server: connect username/password@servername Use the SQLPLUS command to open the database: SQLPLUS
