存储过程,存储函数(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排序没有考,查询语句的所有内容都考了.这个题有点难度. 今 ...
随机推荐
- Vue Admin - 基于 Vue & Bulma 后台管理面板
Vue Admin 是一个基于 Vue 2.0 & Bulma 0.3 的后台管理面板(管理系统),相当于是 Vue 版本的 Bootstrap 管理系统,提供了一组通用的后台界面 UI 和组 ...
- VS中修改工程名的解决方案
VS中修改工程名的解决方案: 一.先修改工程名/解决方案名(在VS中修改即可)举例,原先的工程名为OldProject 想要改成NewProject1.找到工程/解决方案所在的文件夹(已工程名/解 ...
- 键盘按键js效果
<input id="name" type="text" > JS: <script> document.onkeydown = fun ...
- ViewPager制作APP引导页+若干动画效果
ViewPager使用FragmentStatePagerAdapter做Adapter,引导页使用多Fragment形式. 见http://www.cnblogs.com/bmbh/p/567276 ...
- 爬虫之验证码IP攻防心得——小总结
小前言: 一般来说,现在很多平台注册.登录的时候会涉及到验证码,这样做的目的是为了防止恶意程序恶意访问,从而给服务器造成一定的压力,会浪费一定的资源,大家也都知道,现在这种短信平台,邮箱平台等都是收费 ...
- qt无法使用终端启动的解决方法
在Terminal中直接输入命令就能打开QtCreator, i.e. ~$ qtcreator 就可以打开Qt Creator了. 想完成这个功能的原因是,一般在Linux下打命令比较方便,而师兄给 ...
- .NET中制做对象的副本(二)继承对象之间的数据拷贝
定义学生 /// <summary> /// 学生信息 /// </summary> public class Student { /// <summary> // ...
- 【CTF MISC】隐写术wireshark找出图片-“强网杯”网络安全挑战赛writeup
这场CTF中有一道题是分析pcap包的.. 13.大黑阔: 从给的pcap包里把图片提取出来,是一张中国地图. 题目提示是黑阔在聊天,从数据里可以找出几段话. 思路:主要考察wireshark的过滤规 ...
- 【转】Python之日志处理(logging模块)
[转]Python之日志处理(logging模块) 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging ...
- HTTP基础知识3
HTTP之URL HTTP使用统一资源标识符(Uniform Resource Identifiers, URI)来传输数据和建立连接.URL是一种特殊类型的URI,包含了用于查找某个资源的足够的信息 ...