存储过程,存储函数(Oracle)
--打印hello world
create or replace procedure sayhelloworld
as
--说明部分
begin
dbms_output.put_line('hello world');
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;
/
测试:

--查询某个员工的年收入
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;
/
测试:

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;
测试

使用java程序调用存储过程
/*
* 存储过程
* create or replace procedure queryEmpInfo(eno in number,
* pename out varchar2,
* psal out number,
* pjob out varchar2)
*/
@Test
public void testProcedure() {
// {call <procedure-name>[(<arg1>,<arg2>, ...)]}
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 <procedure-name>[(<arg1>,<arg2>, ...)]}
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);
}
}
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;
public void testCursor() {
// {call <procedure-name>[(<arg1>,<arg2>, ...)]}
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了
存储过程,存储函数(Oracle)的更多相关文章
- Oracle学习(十二):存储过程/存储函数
1.知识点 --第一个存储过程 /* 打印Hello World create [or replace] PROCEDURE 过程名(參数列表) AS PLSQL子程序体: 调用存储过程: 1. ex ...
- Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器
---视图 ---视图的概念:视图就是提供一个查询的窗口,来操作数据库中的数据,不存储数据,数据在表中. ---一个由查询语句定义的虚拟表. ---查询语句创建表 create table emp a ...
- Java代码调用Oracle的存储过程,存储函数和包
Java代码调用存储过程和存储函数要使用CallableStatement接口 查看API文档: 上代码: java代码调用如下的存储过程和函数: 查询某个员工的姓名 月薪 职位 create or ...
- Oracle数据库游标,序列,存储过程,存储函数,触发器
游标的概念: 游标是SQL的一个内存工作区,由系统或用户以变量的形式定义.游标的作用就是用于临时存储从数据库中提取的数据块.在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理, ...
- oracle 存储过程,存储函数,包,
http://heisetoufa.iteye.com/blog/366957 认识存储过程和函数 存储过程和函数也是一种PL/SQL块,是存入数据库的PL/SQL块.但存储过程和函数不同于已经介绍过 ...
- Plsql工具单步调试 存储过程或是 函数(oracle数据库)-留着自己用的
<案例1> 原地址: http://jingyan.baidu.com/article/3a2f7c2e144d2826aed61167.html 调试过程对找到一个存过的bug或错误是非 ...
- PL/SQL&存储过程||存储函数&触发器
plsql 有点:交互式 非过程化 数据操纵能力强 自动导航语句简单 调试简单 想率高 声明类型的方式 1.基本类型 2.引用变量 3.记录型变量 基本格式 declare 声明 b ...
- Oracle学习(12):存储过程,函数和触发器
存储过程和存储函数 l存储在数据库中供全部用户程序调用的子程序叫存储过程.存储函数. 注意:存储过程与存储函数声明变量时,用的是as 而不是declare 存储过程与存储函数差别 存储过程不带有返 ...
- day70 12-存储过程和存储函数
什么是相关子查询? 这是一个子查询,子查询本身又是一个多表查询.where不能用组函数,但是可以用字符函数instr().除了order by排序没有考,查询语句的所有内容都考了.这个题有点难度. 今 ...
随机推荐
- Dojo与jQuery综合比较分析
最近一直都在参与项目,无法抽空写些或者看些东西,周末抽了点时间看了下关于Dojo和Jquery的东西,在CSDN上看到一篇两个框架进行对比的文章,感觉写的不错,就拿了过来,没有别的意思,一来想保留下来 ...
- MHA-Failover可能遇到的坑
一.主从数据一致性 1.1.如何保证主从数据一致性 参考叶师傅文章:FAQ系列 | 如何保证主从复制数据一致性 在MySQL中,一次事务提交后,需要写undo.写redo.写binlog,写数据文件等 ...
- kan
http://blog.csdn.net/yahohi/article/details/7427724 http://duanhengbin.iteye.com/blog/1706635 http:/ ...
- Python的虚拟环境virtualenv
原文地址:blog.sina.com.cn/s/blog_4ddef8f80101eu0w.html Python的虚拟环境可以使一个Python程序拥有独立的库library和解释器interpre ...
- 让webstrom更好用的设置
一.让webstrom在编辑vue项目时更快 1.在webstrom的项目管理树中,找到node_modules文件夹,在文件夹上点右键,在出来的右键菜单中选择“Mark Directory as” ...
- kali linux 下搭建git服务器
参考:http://www.cnblogs.com/dee0912/p/5815267.html https://www.liaoxuefeng.com/wiki/001373951630592960 ...
- 【网络编程2】网络编程基础-发送ICMP包(Ping程序)
IP协议 网络地址和主机协议 位于网络层的协议,主要目的是使得网络能够互相通信,该协议使用逻辑地址跨网络通信,目前有两个版本IPV4,IPV6. 在IPV4协议中IP地址是一个32位的数备,采用点分四 ...
- Andrew Ng在coursera上的ML视频 知识点笔记(2)
一.由线性回归导出逻辑回归: 二.“一对多”算法解决多分类问题: 三.“过拟合”和“欠拟合”: (1)对线性回归加入正则项: (2)对逻辑回归加入正则项: (3)加入正则项之后的正规方程:
- linux内核中链表代码分析---list.h头文件分析(二)【转】
转自:http://blog.chinaunix.net/uid-30254565-id-5637598.html linux内核中链表代码分析---list.h头文件分析(二) 16年2月28日16 ...
- 笔记软件 notion
笔记软件 notion : https://www.notion.so 注册:zengxinle@126.com 团队:Hopesun