根據(jù)慕課網(wǎng)課程Oracle存儲過程和自定義函數(shù)整理
1.概念
(1)定義:存儲在數(shù)據(jù)庫中供所有用戶程序調(diào)用的子程序
(2)相同點(diǎn):完成特定功能
(3)區(qū)別:存儲函數(shù)可以return一個值
(4)使用場景:
- 當(dāng)沒有返回值時用存儲過程
- 需要一個返回值時用存儲函數(shù)
- 需要多個返回值時用存儲過程(多個out參數(shù))
2(1).存儲過程(示例1:打印helloworld)(無參)
create or replace procedure sayHelloworld
as
declare
begin
dbms_oupput.put_line("Hello World");
end
/
2(2).存儲過程(示例2:給一個員工號,給他漲薪水)(帶參數(shù))
create or replace procedure raisesalary(eno in number)
as
psal emp.sal%type;
begin
select sal into psal from emp where empno = eno;
update emp.sal set sal = sal + 100 where empno = eno;
dbms_oupput_put_line(psal||(pasl + 100));
--不用commit 由調(diào)用這個過程的程序來commit
end;
/
3.調(diào)用存儲過程方式
(1) exec sayHelloworld() (命令行)
(2)其他處處過程過程或PL/SQL語句中調(diào)。
begin
sayHelloworld();
sayHelloworld();
end
4.調(diào)試存儲過程
(1)右擊存儲過程 --> 調(diào)試 --> 紅色調(diào)試按鈕 --> 會生成調(diào)用程序
(2)用sys登陸給scott用戶授權(quán)
grant debug connect session, debug any procedure to scott;
5.存儲函數(shù)
必須有返回值
-
示例:給定一個員工號,返回他的年薪(月薪乘12加獎金)
create or replace function querysal(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 + pcomm; end;
6.需要返回多個值時用存儲過程
create or replace procedure quarya(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;
7.java調(diào)用存儲過程核心代碼
//這個存儲過程第一個參數(shù)用于接受員工號,后三個用于存返回的結(jié)果
-
//quarya(eno in number, pname out varchar2, psal out number, pjob out varchar2)
String sql = "{call quarya(?,?,?,?)}"; Connection connection = null; CallableStatement statement = null; connection = JDBCUtils.getConn(); statement = connection.prepareCall(sql); statement.setInt(1, 7839); statement.registerOutParameter(2, OracleTypes.VARCHAR); statement.registerOutParameter(3, OracleTypes.NUMBER); statement.registerOutParameter(4, OracleTypes.VARCHAR); statement.execute(); String name = statement.getString(2); String job = statement.getString(4); double sal = statement.getDouble(3); System.out.println(name); System.out.println(job); System.out.println(sal);
8.java調(diào)用存儲函數(shù)核心代碼
//傳入員工號
//querysal(eno in number)
-
//return number
String sql = "{?= call querysal(?)"; Connection connection = null; CallableStatement statement = null; connection = JDBCUtils.getConn(); statement = connection.prepareCall(sql); statement.registerOutParameter(1, OracleTypes.NUMBER); statement.setInt(2, 7839); statement.execute(); double salary = statement.getDouble(1); System.out.print(salary);
9.包(包住了存儲過程)
(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)
begin
open empList for select * from emp where deptno = dno;
end queryEmpList;
end mypackage;
10.java調(diào)用包中的存儲過程核心代碼
Connection connection = null;
CallableStatement statement = null;
ResultSet resultSet = null;
String sql = "{call mypackage.queryEmpList(?, ?)";
connection = JDBCUtils.getConn();
statement = connection.prepareCall(sql);
statement.setInt(1, 20);
statement.registerOutParameter(2, OracleTypes.CURSOR);
statement.execute();
resultSet = ((OracleCallableStatement)statement).getCursor(2);
while(resultSet.next())
{
int empno = resultSet.getInt("empno");
String empname = resultSet.getString("ename");
System.out.println(empno + "\t" + empname);
}