Oracle學習筆記(十二)

來源:互聯網
上載者:User

標籤:檢測   語句   變數   列表   系統   如何   類型   ring   pre   

十三、預存程序和儲存函數
1、掌握預存程序(相當於建立一個函數或者方法體,然後通過外部對其調用)
指儲存在資料庫中供所有程式調用的子程式叫做預存程序或儲存函數。

相同點:
完成特定功能的程式
區別:
是否用return語句傳回值

(1)建立和使用預存程序
用create procedure命令建立預存程序和儲存函數

文法:
create or replace procedure 過程名(參數列表) as PL/SQL 子程式體(說明部分);


案例:
(a)列印一個預存程序:列印HelloWorld(無參數)
create or replace procedure sayhelloworld
as
--說明部分
begin
dbms_output.put_line(‘Hello World !!!‘);
end;
/
在編譯器左邊的過程裡形成大寫的SAYHELLOWORLD

對其調用:
方法一:
execute 簡寫為exec sayhelloworld();
方法二:
begin
sayhelloworld();
sayhelloworld();
end;
/

(b)帶參數的預存程序
案例:為指定的員工,漲100塊錢的工資,並且列印漲前和漲後的薪水
思路:1、建立一個帶參數的預存程序
2、給指定的員工漲100塊錢的工資,並且列印漲前和漲後的薪水
create or replace procedure raisesalary(eno in number)
as
--定義一個變數儲存漲錢的薪水
psal emp.sal%type;
begin
--得到員工漲前的薪水
select sal into psal from emp where empno=eno;

--給員工漲100
update emp set sal=sal+100 where empno=eno;
dbms_output.put_line(‘漲前薪水 :‘||psal||‘漲後薪水 :‘||(psal+100));

--是否需要commit?
--注意:一般不在預存程序或者儲存函數中,commit和rollback.原則不絕對
end;
/

如何調用:
begin
raisesalary(7839);
raisesalary(7566);
commit;
end;


如何調試
注意:不推薦遠端偵錯,推薦本地調試
過程-->右鍵-->編譯以進行調試

設定斷點
select sal into psal from emp where empno=eno;
點擊瓢蟲調試
更改值 ENO := 7839; 點擊確定
授權
系統認證登入:sqlplus / as sysdba
grant DEBUG CONNECT SESSION ,DEBUG ANY PROCEDURE to scott;
右鍵點擊檢測psal
調試
點擊F8

2、掌握儲存函數
函數為一命名的儲存程式,可帶參數,並返回一計算值.
函數和過程的結構類似,但必須有一個return子句,用於返回函數值.

(1)建立儲存函數的文法
create or replace function 函數名(參數列表)
return 函數實值型別
as
PL/SQL子程式體;

案例:查詢某個員工的年度營收
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;
/

set linesize 200
select * from emp;

調用儲存函數

(2)in和out參數
一般來講,預存程序和儲存函數的區別在於儲存函數可以有一個傳回值,而預存程序沒有傳回值.

預存程序和儲存函數都可以有out參數
預存程序和儲存函數都可以有多個out參數
預存程序可以通過out參數來實現傳回值

什麼時候用預存程序/儲存函數?
原則:
-- 如果只有一個傳回值,用儲存函數,否則,就用預存程序.

----out參數,查詢某個員工姓名,月薪和職位
create or replace procedure queryempinform(eno in number, pename out varchar2,psal out number,pjob out varchar2)
as
begin
--得到該員工的姓名,月薪和職位
select ename,sal,empjob into pename,psal,pjob from emp where empno=eno;
end;
/

思考:(1)查詢某個員工的所有資訊---->out參數太多了
set linesize 80
desc emp
(2)查詢某個部門中所有員工的所有資訊--->out中返回集合


(3)在應用程式中訪問預存程序和儲存函數
(a)訪問預存程序
public class JdbcUtils {
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String username = "scott";
private static String password = "tiger";
private static Connection conn;

public static Connection getConnection() {
conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("oracle串連擷取失敗");
}
return conn;
}

// 關閉連線物件
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("oracle串連關閉失敗");
}
}
}

public static void close(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("oracle串連關閉失敗");
}
}
}

public static void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("oracle串連關閉失敗");
}
}
}
}


public class TestProcedure {

@Test
public void testProcedure() {
String sql = "{call queryempinform(?,?,?,?)}";
Connection conn = null;
CallableStatement call = null;

try {
// 得到一個串連
conn = JdbcUtils.getConnection();
// 通過串連建立出statement
call = conn.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 (Exception e) {
e.printStackTrace();
}finally{
JdbcUtils.close(conn);
JdbcUtils.close(call);
}
}

}
(b)訪問儲存函數
public class TestFunction {

@Test
public void testFunction() {
String sql = "{?=call queryempincome(?)}";
Connection conn = null;
CallableStatement call = null;

try {
// 得到一個串連
conn = JdbcUtils.getConnection();
// 通過串連建立出statement
call = conn.prepareCall(sql);

// 對於輸出參數,申明
call.registerOutParameter(1, OracleTypes.NUMBER);

// 對於參數賦值
call.setInt(2, 7839);

// 執行調用
call.execute();

// 取出結果

double income = call.getDouble(1);

//列印結果集
System.out.println("年度營收:" + income);

} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.close(conn);
JdbcUtils.close(call);
}
}
}

(4)在out參數中使用游標

(a)申明包結構
(b)包頭
(c)包體

案例:查詢某個部門中所有員工的所有資訊

包頭:
create or replace package mypackage as
--type自訂類型empcursor
type empcursor is ref cursor;
procedure queryEmpList(dno in number,empList out empcursor);
end mypackage;
*****包體需要實現包頭中生命的所有方法*******

操作:
程式包----->右鍵--->建立程式包--->包名設定mypackage--->確定--->編譯--->ctril+s執行

包體:
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;

操作:
選中mypackage-->右鍵-->建立主體--->編譯--->ctril+s執行

 

查看包:
desc mypackage


注意:需要帶上包名訪問


(5)在應用中訪問包中的預存程序
public class TestMypackage {
@Test
public void testMypackage() {
String sql = "{call MYPACKAGE.queryEmpList(?,?)}";
Connection conn = null;
CallableStatement call = null;
ResultSet rs = null;

try {
// 得到一個串連
conn = JdbcUtils.getConnection();
// 通過串連建立出statement
call = conn.prepareCall(sql);

// 對於參數賦值
call.setInt(1, 10);

// 對於輸出參數,申明
call.registerOutParameter(2, OracleTypes.CURSOR);

// 執行調用
call.execute();

// 取出該部門的所有員工資訊

rs = ((OracleCallableStatement) call).getCursor(2);
while (rs.next()) {
// 該員工的員工號/薪水/職位
int empno =rs.getInt("empno");
String name =rs.getString("ename");
double salary = rs.getDouble("sal");
String job = rs.getString("empjob");
System.out.println(empno +"\t"+name+"\t"+salary+"\t"+job);

}

} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.close(conn);
JdbcUtils.close(call);
JdbcUtils.close(rs);
}
}
}

Oracle學習筆記(十二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.