存储过程,存储函数(Oracle)

--打印hello world
create or replace procedure sayhelloworld
as
  --说明部分
begin
  dbms_output.put_line('hello world');
end;
/

编译后:

存储过程,存储函数(Oracle)

2、调用存储过程方法:

1、exec 过程名

2、begin

过程名;

过程名;

end;

/

测试调用存储过程

--连接数据库
C:\WINDOWS\system32>sqlplus scott/tiger@192.168.56.101:1521/orcl
SQL>--调用方式一
SQL> set serveroutput on
SQL> exec sayhelloworld;
hello world

PL/SQL 过程已成功完成。

SQL> --调用方式二:
SQL> begin
  2      sayhelloworld();
  3      sayhelloworld();
  4  end;
  5  /
hello world
hello world

PL/SQL 过程已成功完成。

带参数的存储过程:

--给指定员工薪水涨100,并且打印涨前和涨后的薪水
create or replace procedure raiseSalary(eno in number) --in为输入参数
as
  --说明部分
  psal emp.sal%type;

begin
  --得到涨前的薪水
  select sal into psal from emp where empno=eno;

  update emp set sal=sal+100 where empno=eno;

  --要不要commit?
  --为保证在同一事务中,commit由谁调用谁提交
  dbms_output.put_line('涨前:'||psal||'  涨后:'||(psal+100));
end;
/

测试:

存储过程,存储函数(Oracle)

存储函数

函数(function)为一命名的存储程序,可带参数,并返回一计算值。函数和过程的结构类似,但必须有一个return子句,用于返回函数值。函数说明要指定函数名、结果值的类型,以及参数类型等。

存储函数语法:

create[or replace] functiion 函数名(参数列表)

return函数值类型

as

PLSQL子程序体;

查询员工年收入

--查询某个员工的年收入
create or replace function queryempincome(eno in number)
return number
as
  --月薪和奖金
  psal   emp.sal%type;
  pcomm  emp.comm%type;
begin
  select sal,comm into psal,pcomm from emp where empno=eno;
  --返回年收入
  return psal*12+nvl(pcomm,0);
end;
/

测试:

存储过程,存储函数(Oracle)

过程和函数中的in 和out

一般来讲,过程和函数的区别在于函数可以有一个返回值;而过程没有返回值。

但过程和函数都可以通过out指定一个或多个输出参数,我们可以利用out参数,在过程和函数中实现返回多个值。

什么时候用存储过程/存储函数?

原则(不是必须的):

如果只有一个返回值,用存储函数;否则,就用存储过程。

存储过程

create or replace procedure queryEmpInfo(eno in number,
                                         pname out varchar2,
                                         psal  out number,
                                         pjob  out varchar2)
as
begin
  select ename,sal,empjob into pname,psal,pjob from emp where empno=eno;
end;

测试

存储过程,存储函数(Oracle)

使用java程序调用存储过程

/*
     * 存储过程
     * create or replace procedure queryEmpInfo(eno in number,
     *                                     pename out varchar2,
     *                                     psal out number,
     *                                     pjob out varchar2)
     */
    @Test
    public void testProcedure() {
       // {call [(,, ...)]}
       String sql = "{call queryEmpInfo(?,?,?,?)}";
       CallableStatement call = null;
       Connection connection = JDBCUtils.getConnection();
       try {
           call = connection.prepareCall(sql);

           //对于in参数,赋值
           call.setInt(1, 7839);

           //对于out参数,声明
           call.registerOutParameter(2, OracleTypes.VARCHAR);
           call.registerOutParameter(3, OracleTypes.NUMBER);
           call.registerOutParameter(4, OracleTypes.VARCHAR);

           //执行
           call.execute();

           //取出结果
           String name = call.getString(2);
           double sal = call.getDouble(3);
           String job = call.getString(4);
           System.out.println(name + "\t" + sal + "\t" + job);

       } catch (SQLException e) {
           e.printStackTrace();
       }finally{
           JDBCUtils.release(connection, call, null);
       }
    }

使用java程序调用存储函数

/*
     * 存储函数
     * create or replace function queryEmpIncome(eno in number)
       return number
     */
    @Test
    public void testFunction() {
       // {?= call [(,, ...)]}
       String sql = "{?=call queryEmpIncome(?)}";
       Connection conn = null;
       CallableStatement call = null;
       try {
           conn = JDBCUtils.getConnection();
           call = conn.prepareCall(sql);

           //对于out参数,赋值
           call.registerOutParameter(1, OracleTypes.NUMBER);

           //对于in参数,赋值
           call.setInt(2, 7839);

           //执行
           call.execute();

           //取出数据
           double income = call.getDouble(1);
           System.out.println(income);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           JDBCUtils.release(conn, call, null);
       }
    }

在out参数中使用光标

问题:查询某个部门中所有员工的所有信息

1、申明包结构

CREATE OR REPLACE
PACKAGE MYPACKAGE AS

  type empcursor is ref cursor;
  --创建存储过程,输出参数为自定义类型
  procedure queryEmpList(dno in number,empList out empcursor);

END MYPACKAGE;

2、创建包体(实现)

CREATE OR REPLACE
PACKAGE BODY MYPACKAGE AS

  procedure queryEmpList(dno in number,empList out empcursor) AS
  BEGIN
    --实现
    open empList for select * from emp where deptno=dno;

  END queryEmpList;

END MYPACKAGE;

使用java调用带包的存储过程

public void testCursor() {
       // {call [(,, ...)]}

       String sql = "{call MYPACKAGE.queryEmpList(?,?)}";
       Connection conn = null;
       CallableStatement call = null;
       ResultSet rs = null;
       try {
           conn = JDBCUtils.getConnection();
           call = conn.prepareCall(sql);

           //对于in参数,赋值ֵ
           call.setInt(1, 20);

           //对于out参数,赋值
           call.registerOutParameter(2, OracleTypes.CURSOR);

           //执行
           call.execute();

           // 取出结果
           rs = ((OracleCallableStatement)call).getCursor(2);
           while(rs.next()){
              String name = rs.getString("ename");
              double sal = rs.getDouble("sal");
              System.out.println(name+"\t"+sal);
           }
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           JDBCUtils.release(conn, call, rs);
       }
    }

此案例光标没有关闭,原因:当resultSet关闭的时候 光标就close了

Original: https://www.cnblogs.com/dooor/p/5599351.html
Author: Dvomu
Title: 存储过程,存储函数(Oracle)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/610341/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 【原创】Linux虚拟化KVM-Qemu分析(九)之virtio设备

    背景 Read the fucking source code! –By 鲁迅 A picture is worth a thousand words. –…

    Linux 2023年6月8日
    071
  • IEEE浮点数标准

    IEEE浮点数标准 阅读笔记:Computer System : A Programmmer’s Perspective 基本概念 IEEE浮点数标准采用 [V=(-1…

    Linux 2023年6月8日
    0121
  • Linux中CentOS 7的安装及Linux常用命令

    前言 什么是Linux Linux是一套免费使用和自由传播的操作系统。说到操作系统,大家比较熟知的应该就是Windows和MacOS操作系统,我们今天所学习的Linux也是一款操作…

    Linux 2023年6月6日
    0141
  • Redis主从配置总结

    grep ‘^[a-Z]’ /usr/local/redis/conf/redis.conf bind 127.0.0.1 192.168.27.115 protected-mod…

    Linux 2023年5月28日
    097
  • 安卓逆向从0到1学习总结

    PS:该文已经首发于公众号信安之路!!! 初识安卓逆向是在2019年的暑假,到现在也快一年了,这一年来有刚从web渗透转来的迷茫,有成功破解了第一个app的喜悦,也有通宵熬夜逆向的…

    Linux 2023年6月8日
    0118
  • Docker 容器虚拟化

    Docker 容器虚拟化 1、虚拟化网络 Network Namespace 是 Linux 内核提供的功能,是实现网络虚拟化的重要功能,它能创建多个隔离的网络空间,它们有独自网络…

    Linux 2023年6月7日
    0133
  • 【论文笔记】(模型压缩)Do Deep Nets Really Need to be Deep?

    摘要 作者通过模型压缩(model compression)使浅层的网络学习与深层网络相同的函数,以达到深层网络的准确率(accuracy)。当与深浅模型的参数量相同时,浅层模型可…

    Linux 2023年6月7日
    0105
  • 【转】windows下Redis的安装和使用

    2、在下载网页中,找到最后发行的版本(此处是3.2.100)。找到Redis-x64-3.2.100.msi和Redis-x64-3.2.100.zip,点击下载。这里说明一下,第…

    Linux 2023年5月28日
    092
  • Weblogic页面应用查询oracle数据库后台报错或页面日期格式显示错误

    问题:在生产环境中有两台WEB服务器,分别为227和228,部署的应用代码都是每日同步的,两边完全一致,但是某些页面查询数据时,227无结果,并且后台报java数组越界的错误,而2…

    Linux 2023年6月14日
    093
  • 大数据——综合案例

    一、本地数据集上传到到数据仓库Hive 1、 实验数据集的下载 将user.zip下载到指定目录 2.给hadoop用户赋予针对bigdatacase目录的各种操作权限 3.创建一…

    Linux 2023年6月6日
    090
  • 【5】2022年7月

    7月3日 总结3号的一天就是只有一句话,”自己经历了什么只有自己最清楚,不要辜负自己经历的”。 7月3号凌晨2点,收拾好行李和整理房间,在网上购买日常用品到…

    Linux 2023年6月13日
    083
  • 实验3: OpenFlow协议分析实战

    实验三: OpenFlow协议分析实战 (一) 基本要求 1.搭建拓扑、IP配置、主机通信 1.1 搭建拓扑 1.2 IP配置 1.3 代码 #!/usr/bin/env pyth…

    Linux 2023年6月7日
    0100
  • Visual Studio使用docker开发卡 vs2017u5 exists, deleting (vsdbg的问题)

    引言在使用 Visual Studio 进行 Docker 运行调试的时候会出现无法调试的问题。出现类似一下症状:Info: Using vsdbg version ‘…

    Linux 2023年6月14日
    070
  • docker使用

    什么是虚拟化 在计算机中,虚拟化(英语:Virtualization)是一种资源管理技术,是将计算机的各种实体资源,如服务器、网络、内存及存储等,予以抽象、转换后呈现出来,打破实体…

    Linux 2023年6月14日
    087
  • PHP利用Apache、Nginx的特性实现免杀Webshell

    环境函数用法 nginx get_defined_vars() 返回由所有已定义变量所组成的数组 apache getallheaders() 获取全部 HTTP 请求头信息 ap…

    Linux 2023年5月28日
    080
  • linux 网络配置

    安装linux之后一般都是网络自启动, 适合在没有网络工具的情况下修改配置文件 ubuntu: 网络配置文件/etc/network/interfaces 配置类似于: auto …

    Linux 2023年6月14日
    079
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球