最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Oracle存儲(chǔ)過(guò)程和自定義函數(shù)詳解

 更新時(shí)間:2016年11月15日 16:19:21   作者:小小工匠  
本篇文章主要介紹了Oracle存儲(chǔ)過(guò)程和自定義函數(shù)詳解,有需要的可以了解一下。

概述

PL/SQL中的過(guò)程和函數(shù)(通常稱(chēng)為子程序)是PL/SQL塊的一種特殊的類(lèi)型,這種類(lèi)型的子程序可以以編譯的形式存放在數(shù)據(jù)庫(kù)中,并為后續(xù)的程序塊調(diào)用。

相同點(diǎn): 完成特定功能的程序

不同點(diǎn):是否用return語(yǔ)句返回值。

舉個(gè)例子:

create or replace procedure PrintStudents(p_staffName in xgj_test.username%type) as

 cursor c_testData is
 select t.sal, t.comm from xgj_test t where t.username = p_staffName;

begin

 for v_info in c_testData loop
 DBMS_OUTPUT.PUT_LINE(v_info.sal || ' ' || v_info.comm);
 end loop;

end PrintStudents;

一旦創(chuàng)建了改程序并將其存儲(chǔ)在數(shù)據(jù)庫(kù)中,就可以使用如下的方式調(diào)用該過(guò)程

begin
 PrintStudents('Computer Science');
 PrintStudents('Match');
end;
/

或者

exec PrintStudents('Computer Science');
exec PrintStudents('Match');

在命令窗口中:

在pl/sql工具的sql窗口中:

存儲(chǔ)過(guò)程的創(chuàng)建和調(diào)用

基本語(yǔ)法

create [ or replace] procedure procedure_name
[( argument [ {IN | OUT | IN OUT }] type,
......
argument [ {IN | OUT | IN OUT }] type ) ] { IS | AS}
procedure_body

無(wú)參的存儲(chǔ)過(guò)程

/**
 無(wú)參數(shù)的存過(guò)
 打印hello world

 調(diào)用存儲(chǔ)過(guò)程:
 1. exec sayhelloworld();
 2 begin 
 sayhelloworld();
 end;
 /

*/
create or replace procedure sayhelloworld
as
--說(shuō)明部分
begin
 dbms_output.put_line('hello world');
end sayhelloworld;

調(diào)用過(guò)程:

SQL> set serveroutput on ;
SQL> exec sayhelloworld();

hello world

PL/SQL procedure successfully completed

SQL> begin
 2 sayhelloworld();
 3 sayhelloworld();
 4 end;
 5 /

hello world
hello world

PL/SQL procedure successfully completed

帶參數(shù)的存儲(chǔ)過(guò)程

/**
創(chuàng)建一個(gè)帶參數(shù)的存儲(chǔ)過(guò)程

給指定的員工增加工資,并打印增長(zhǎng)前后的工資

*/
create or replace procedure addSalary(staffName in xgj_test.username%type )
as
--定義一個(gè)變量保存調(diào)整之前的薪水
oldSalary xgj_test.sal%type;

begin
 --查詢員工漲之前的薪水
 select t.sal into oldSalary from xgj_test t where t.username=staffName; 

 --調(diào)整薪水
 update xgj_test t set t.sal = sal+1000 where t.username=staffName ;

 --輸出
 dbms_output.put_line('調(diào)整之前的薪水:'|| oldSalary || ' ,調(diào)整之后的薪水:' || (oldSalary + 1000));

end addSalary;

可以看到,update語(yǔ)句之后并沒(méi)有commit的操作。

一般來(lái)講為了保證事務(wù)的一致性,由調(diào)用者來(lái)提交比較合適,當(dāng)然了是需要區(qū)分具體的業(yè)務(wù)需求的~

begin 
addSalary('xiao');
addSalary('gong');
commit ;
end ;
/

存儲(chǔ)函數(shù)

基本語(yǔ)法

create [ or replace] function function_name
[( argument [ {IN | OUT | IN OUT }] type,
......
argument [ {IN | OUT | IN OUT }] type ) ] 
RETURN { IS | AS}
function_body

其中 return子句是必須存在的,一個(gè)函數(shù)如果沒(méi)有執(zhí)行return就結(jié)束將發(fā)生錯(cuò)誤,這一點(diǎn)和存過(guò)有說(shuō)不同。

存儲(chǔ)函數(shù)

準(zhǔn)備的數(shù)據(jù)如下:

/**
查詢員工的年薪 (月工資*12 + 獎(jiǎng)金)
*/

create or replace function querySalaryInCome(staffName in varchar2)

 return number as
 --定義變量保存員工的工資和獎(jiǎng)金
 pSalary xgj_test.sal%type;
 pComm xgj_test.comm%type;

begin
 --查詢員工的工資和獎(jiǎng)金
 select t.sal, t.comm
 into pSalary, pComm
 from xgj_test t
 where t.username = staffName;
 --直接返回年薪
 return pSalary * 12 + pComm;
end querySalaryInCome;

存在一個(gè)問(wèn)題,當(dāng)獎(jiǎng)金為空的時(shí)候,算出來(lái)的年收入竟然是空的。

因?yàn)?如果一個(gè)表達(dá)式中有空值,那么這個(gè)表達(dá)式的結(jié)果即為空值。

所以我們需要對(duì)空值進(jìn)行處理, 使用nvl函數(shù)即可。

最后修改后的function為

create or replace function querySalaryInCome(staffName in varchar2)

 return number as
 --定義變量保存員工的工資和獎(jiǎng)金
 pSalary xgj_test.sal%type;
 pComm xgj_test.comm%type;

begin
 --查詢員工的工資和獎(jiǎng)金
 select t.sal, t.comm
 into pSalary, pComm
 from xgj_test t
 where t.username = staffName;
 --直接返回年薪
 return pSalary * 12 + nvl(pComm,0);
end querySalaryInCome;

out參數(shù)

一般來(lái)講,存儲(chǔ)過(guò)程和存儲(chǔ)函數(shù)的區(qū)別在于存儲(chǔ)函數(shù)可以有一個(gè)返回值,而存儲(chǔ)過(guò)程沒(méi)有返回值。

  • 存儲(chǔ)過(guò)程和存儲(chǔ)函數(shù)都可以有out參數(shù)
  • 存儲(chǔ)過(guò)程和存儲(chǔ)函數(shù)都可以有多個(gè)out參數(shù)
  • 存儲(chǔ)過(guò)程可以通過(guò)out參數(shù)實(shí)現(xiàn)返回值

那我們?nèi)绾芜x擇存儲(chǔ)過(guò)程和存儲(chǔ)函數(shù)呢?

原則:

如果只有一個(gè)返回值,用存儲(chǔ)函數(shù),否則(即沒(méi)有返回值或者有多個(gè)返回值)使用存儲(chǔ)過(guò)程。

/**
根據(jù)員工姓名,查詢員工的全部信息
*/
create or replace procedure QueryStaffInfo(staffName in xgj_test.username%type,
           pSal out number,
           pComm out xgj_test.comm%type,
           pJob out xgj_test.job%type) 

is

begin
 --查詢?cè)搯T工的薪資,獎(jiǎng)金和職位
 select t.sal,t.comm,t.job into pSal,pComm,pJob from xgj_test t where t.username=staffName;
end QueryStaffInfo;


先拋出兩個(gè)思考問(wèn)題:

  • 查詢員工的所有信息–> out參數(shù)太多怎么辦?
  • 查詢某個(gè)部門(mén)中所有員工的信息–> out中返回集合?

后面會(huì)講到如何解決? 總不能一個(gè)個(gè)的寫(xiě)out吧~

在應(yīng)用中訪問(wèn)存儲(chǔ)過(guò)程和存儲(chǔ)函數(shù)

概述

我們使用Java程序連接Oracle數(shù)據(jù)庫(kù)。

使用jar: ojdbc14.jar

關(guān)于oracle官方提供的幾個(gè)jar的區(qū)別

  • classes12.jar (1,600,090 bytes) - for use with JDK 1.2 and JDK 1.3
  • classes12_g.jar (2,044,594 bytes) - same as classes12.jar, except that classes were compiled with “javac -g” and contain some tracing information.
  • classes12dms.jar (1,607,745 bytes) - same as classes12.jar, except that it contains additional code`to support Oracle Dynamic Monitoring Service.
  • classes12dms_g.jar (2,052,968 bytes) - same as classes12dms.jar except that classes were compiled with “javac -g” and contain some tracing information.
  • ojdbc14.jar (1,545,954 bytes) - classes for use with JDK 1.4 and 1.5
  • ojdbc14_g.jar (1,938,906 bytes) - same as ojdbc14.jar, except that classes were compiled with “javac -g” and contain some tracing information.
  • ojdbc14dms.jar (1,553,561 bytes) - same as ojdbc14.jar, except that it contains additional code`to support Oracle Dynamic Monitoring Service.
  • ojdbc14dms_g.jar (1,947,136 bytes) - same as ojdbc14dms.jar, except that classes were compiled with “javac -g” and contain some tracing information.

工程目錄如下:

簡(jiǎn)單的寫(xiě)下獲取數(shù)據(jù)庫(kù)連接的工具類(lèi)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtils {

 // 設(shè)定數(shù)據(jù)庫(kù)驅(qū)動(dòng),數(shù)據(jù)庫(kù)連接地址端口名稱(chēng),用戶名,密碼
 private static final String driver = "oracle.jdbc.driver.OracleDriver";
 private static final String url = "jdbc:oracle:thin:@ip:xxxx";
 private static final String username = "xxxx";
 private static final String password = "xxxx";

 /**
  * 注冊(cè)數(shù)據(jù)庫(kù)驅(qū)動(dòng)
  */
 static {
  try {
   Class.forName(driver);
  } catch (ClassNotFoundException e) {
   throw new ExceptionInInitializerError(e.getMessage());
  }
 }

 /**
  * 獲取數(shù)據(jù)庫(kù)連接
  */
 public static Connection getConnection() {
  try {
   Connection connection = DriverManager.getConnection(url, username, password);
   // 成功,返回connection
   return connection;
  } catch (SQLException e) {
   e.printStackTrace();
  }
  // 獲取失敗,返回null
  return null;
 }

 /**
  * 釋放連接
  */
 public static void cleanup(Connection conn, Statement st, ResultSet rs) {

  if (rs != null) {
   try {
    rs.close();
   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    rs = null;
   }
  }

  if (st != null) {
   try {
    st.close();
   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    st = null;
   }
  }

  if (conn != null) {
   try {
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   } finally {
    conn = null;
   }
  }

 }
}

在應(yīng)用程序中訪問(wèn)存儲(chǔ)過(guò)程

根據(jù)官方提供的API,我們可以看到:

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

import com.turing.oracle.dbutil.DBUtils;

import oracle.jdbc.OracleTypes;


public class TestProcedure {

 @Test
 public void callProcedure(){
  // {call <procedure-name>[(<arg1>,<arg2>, ...)]}

  Connection conn = null ;
  CallableStatement callableStatement = null ;

  /**
   *
   根據(jù)員工姓名,查詢員工的全部信息
   create or replace procedure QueryStaffInfo(staffName in xgj_test.username%type,
              pSal out number,
              pComm out xgj_test.comm%type,
              pJob out xgj_test.job%type) 
   is
   begin
    --查詢?cè)搯T工的薪資,獎(jiǎng)金和職位
    select t.sal,t.comm,t.job into pSal,pComm,pJob from xgj_test t where t.username=staffName;
   end QueryStaffInfo;
   */
  // 我們可以看到該存過(guò) 4個(gè)參數(shù) 1個(gè)入?yún)?3個(gè)出參
  String sql = "{call QueryStaffInfo(?,?,?,?)}";

  try {
   // 獲取連接
   conn = DBUtils.getConnection();
   // 通過(guò)連接獲取到CallableStatement
   callableStatement = conn.prepareCall(sql);

   // 對(duì)于in 參數(shù),需要賦值
   callableStatement.setString(1, "xiao");
   // 對(duì)于out 參數(shù),需要聲明
   callableStatement.registerOutParameter(2, OracleTypes.NUMBER); // 第二個(gè) ?
   callableStatement.registerOutParameter(3, OracleTypes.NUMBER);// 第三個(gè) ?
   callableStatement.registerOutParameter(4, OracleTypes.VARCHAR);// 第四個(gè) ?

   // 執(zhí)行調(diào)用
   callableStatement.execute();

   // 取出結(jié)果
   int salary = callableStatement.getInt(2);
   int comm = callableStatement.getInt(3);
   String job = callableStatement.getString(3);

   System.out.println(salary + "\t" + comm + "\t" + job);

  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   DBUtils.cleanup(conn, callableStatement, null);
  }


 }
}

在應(yīng)用程序中訪問(wèn)存儲(chǔ)函數(shù)

根據(jù)官方提供的API,我們可以看到:

import java.sql.CallableStatement;
import java.sql.Connection;

import org.junit.Test;

import com.turing.oracle.dbutil.DBUtils;

import oracle.jdbc.OracleTypes;

public class TestFuction {

 @Test
 public void callFuction(){
  //{?= call <procedure-name>[(<arg1>,<arg2>, ...)]}
  Connection conn = null;
  CallableStatement call = null;
  /**
   * create or replace function querySalaryInCome(staffName in varchar2)
     return number as
     --定義變量保存員工的工資和獎(jiǎng)金
     pSalary xgj_test.sal%type;
     pComm xgj_test.comm%type;

    begin
     --查詢員工的工資和獎(jiǎng)金
     select t.sal, t.comm
     into pSalary, pComm
     from xgj_test t
     where t.username = staffName;
     --直接返回年薪
     return pSalary * 12 + nvl(pComm,0);
    end querySalaryInCome;
   */

  String sql = "{?=call querySalaryInCome(?)}";

  try {
   // 獲取連接
   conn = DBUtils.getConnection();
   // 通過(guò)conn獲取CallableStatement
   call = conn.prepareCall(sql);

   // out 參數(shù),需要聲明
   call.registerOutParameter(1, OracleTypes.NUMBER);
   // in 參數(shù),需要賦值
   call.setString(2, "gong");

   // 執(zhí)行
   call.execute();
   // 取出返回值 第一個(gè)?的值
   double income = call.getDouble(1);
   System.out.println("該員工的年收入:" + income);
  } catch (Exception e) {
   e.printStackTrace();
  }finally {
   DBUtils.cleanup(conn, call, null);
  }
 }

}

在out參數(shù)中訪問(wèn)光標(biāo)

在out參數(shù)中使用光標(biāo)

我們之前拋出的兩個(gè)思考問(wèn)題:

  • 查詢員工的所有信息–> out參數(shù)太多怎么辦?
  • 查詢某個(gè)部門(mén)中所有員工的信息–> out中返回集合?

我們可以通過(guò)返回Cursor的方式來(lái)實(shí)現(xiàn)。

在out參數(shù)中使用光標(biāo) 的步驟:

  • 申明包結(jié)構(gòu)
  • 包頭
  • 包體

包頭:

create or replace package MyPackage is

 -- Author : ADMINISTRATOR
 -- Created : 2016-6-4 18:10:42
 -- Purpose : 

 -- 使用type關(guān)鍵字 is ref cursor說(shuō)明是cursor類(lèi)型
 type staffCursor is ref cursor;

 procedure queryStaffJob(pJob   in xgj_test.job%type,
       jobStaffList out staffCursor);

end MyPackage;

創(chuàng)建完包頭之后,創(chuàng)建包體,包體需要實(shí)現(xiàn)包頭中聲明的所有方法。

包體

create or replace package body MyPackage is

 procedure queryStaffJob(pJob   in xgj_test.job%type,
       jobStaffList out staffCursor)

 as
 begin
  open jobStaffList for select * from xgj_test t where t.job=pJob;
 end queryStaffJob;

end MyPackage;

事實(shí)上,通過(guò)plsql工具創(chuàng)建包頭,編譯后,包體的框架就會(huì)自動(dòng)的生成了。

在應(yīng)用程序中訪問(wèn)包下的存儲(chǔ)過(guò)程

在應(yīng)用程序中訪問(wèn)包下的存儲(chǔ)過(guò)程

在應(yīng)用程序中訪問(wèn)包下的存儲(chǔ)過(guò)程 ,需要帶包名

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;

import org.junit.Test;

import com.turing.oracle.dbutil.DBUtils;

import oracle.jdbc.OracleTypes;
import oracle.jdbc.driver.OracleCallableStatement;

public class TestCursor {

 @Test
 public void testCursor(){
  /**
   * 
   * create or replace package MyPackage is
     type staffCursor is ref cursor;

     procedure queryStaffJob(pJob   in xgj_test.job%type,
           jobStaffList out staffCursor);

    end MyPackage;
   */
  String sql = "{call MyPackage.queryStaffJob(?,?)}" ;

  Connection conn = null;
  CallableStatement call = null ;
  ResultSet rs = null;

  try {
   // 獲取數(shù)據(jù)庫(kù)連接
   conn = DBUtils.getConnection();
   // 通過(guò)conn創(chuàng)建CallableStatemet
   call = conn.prepareCall(sql);

   // in 參數(shù) 需要賦值
   call.setString(1, "Staff");
   // out 參數(shù)需要聲明
   call.registerOutParameter(2, OracleTypes.CURSOR);

   // 執(zhí)行調(diào)用
   call.execute();

   // 獲取返回值
   rs = ((OracleCallableStatement)call).getCursor(2);
   while(rs.next()){
    // 取出值
    String username = rs.getString("username");
    double sal = rs.getDouble("sal");
    double comm = rs.getDouble("comm");

    System.out.println("username:" + username + "\t sal:" + sal + "\t comm:" + comm);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }finally {
   DBUtils.cleanup(conn, call, rs);
  }
 }

}

原文鏈接:http://blog.csdn.net/yangshangwei/article/details/51581952

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Oracle19c最新版保姆級(jí)別最詳細(xì)的安裝配置教程(2023年)

    Oracle19c最新版保姆級(jí)別最詳細(xì)的安裝配置教程(2023年)

    這篇文章主要給大家介紹了關(guān)于Oracle19c最新版保姆級(jí)別安裝配置教程的相關(guān)資料,19c作為原有序列的12c最后一個(gè)版本,可以說(shuō)是集大成的版本,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • 整理匯總Oracle常用命令 方便你我他

    整理匯總Oracle常用命令 方便你我他

    想統(tǒng)一整理Oracle中常用的命令語(yǔ)句,以后遇到,會(huì)不斷更新此博客中。為了以后方便查詢,因此整理此博客中
    2012-11-11
  • oracle表空間中空表統(tǒng)計(jì)方法示例介紹

    oracle表空間中空表統(tǒng)計(jì)方法示例介紹

    這篇文章主要介紹了oracle表空間中空表統(tǒng)計(jì)方法,需要的朋友可以參考下
    2014-04-04
  • Windows10系統(tǒng)中Oracle完全卸載正確步驟

    Windows10系統(tǒng)中Oracle完全卸載正確步驟

    自己剛到公司就是熟悉數(shù)據(jù)庫(kù)的安裝卸載,所以分享一下學(xué)到的,下面這篇文章主要給大家介紹了關(guān)于Windows10系統(tǒng)中Oracle完全卸載正確步驟的相關(guān)資料,文章通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • Oracle9i數(shù)據(jù)庫(kù)異常關(guān)閉后的啟動(dòng)

    Oracle9i數(shù)據(jù)庫(kù)異常關(guān)閉后的啟動(dòng)

    Oracle9i數(shù)據(jù)庫(kù)異常關(guān)閉后的啟動(dòng)...
    2007-03-03
  • Oracle連續(xù)相同數(shù)據(jù)的統(tǒng)計(jì)

    Oracle連續(xù)相同數(shù)據(jù)的統(tǒng)計(jì)

    今天小編就為大家分享一篇關(guān)于Oracle連續(xù)相同數(shù)據(jù)的統(tǒng)計(jì),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • oracle下一條SQL語(yǔ)句的優(yōu)化過(guò)程(比較詳細(xì))

    oracle下一條SQL語(yǔ)句的優(yōu)化過(guò)程(比較詳細(xì))

    很簡(jiǎn)單的一次調(diào)整,語(yǔ)句加了適當(dāng)?shù)乃饕笮阅芫陀写蠓奶嵘?。?dāng)時(shí)看到這條語(yǔ)句的時(shí)候,第一感覺(jué)就是執(zhí)行效率肯定低下。語(yǔ)句的功能是求某一客戶當(dāng)天產(chǎn)品的總銷(xiāo)量。
    2010-04-04
  • 修改Oracle監(jiān)聽(tīng)默認(rèn)端口號(hào)1521的方法

    修改Oracle監(jiān)聽(tīng)默認(rèn)端口號(hào)1521的方法

    我們都知道,Oracle的監(jiān)聽(tīng)默認(rèn)端口是1521,但是如果系統(tǒng)上1521已經(jīng)被占用或業(yè)務(wù)要求不用默認(rèn)端口,又或者是為了安全,這個(gè)時(shí)候我們就需要修改監(jiān)聽(tīng)的默認(rèn)端口。下面這篇文章主要介紹了修改Oracle監(jiān)聽(tīng)默認(rèn)端口號(hào)1521的方法,需要的朋友可以參考下。
    2017-01-01
  • Oracle Report中加入下載超鏈接操作方法

    Oracle Report中加入下載超鏈接操作方法

    有些朋友可能會(huì)在Oracle Report中加入下載超鏈接,本文將介紹如何實(shí)現(xiàn)此更能
    2012-11-11
  • 基于Oracle閃回詳解(必看篇)

    基于Oracle閃回詳解(必看篇)

    下面小編就為大家分享一篇基于Oracle閃回詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12

最新評(píng)論

五家渠市| 肥东县| 柏乡县| 巴彦淖尔市| 土默特右旗| 海城市| 裕民县| 桐城市| 克东县| 南木林县| 阿克苏市| 泗洪县| 东光县| 罗定市| 汝城县| 双桥区| 岚皋县| 吉木乃县| 双城市| 兴城市| 镇赉县| 义乌市| 如皋市| 涞源县| 屏东市| 凤山市| 工布江达县| 高密市| 巍山| 惠州市| 汾西县| 大邑县| 灵寿县| 应城市| 哈密市| 岳阳县| 湟中县| 尖扎县| 墨竹工卡县| 界首市| 习水县|