1. 過程的語法結構 完整的過程結構如下: create or replace PRocedure 過程名 as 聲明語句段; begin 執行語句段; exception 異常處理語句段; end; 2. 過程的特點 過程是有名稱的程序塊,as要害詞代替了無名塊的declare。 3. 創建過程實例 在【SQLPlus Worksheet】中執行下列PL/SQL程序,該程序將創建名為tempprocedure的過程,create是創建過程的標識符,replace表示若同名過程存在將覆蓋原過程。該過程定義了一個變量,其類型和testtable數據表中的currentdate字段類型相同,都是日期型,將數據表中的recordnumber字段為88的currentdate字段內容送入變量中,然后輸出結果。 ――――――――――――――――――――――――――――――――――――― set serveroutput on create or replace procedure tempuser.tempprocedure as tempdate tempuser.testtable.currentdate%type; begin select currentdate into tempdate from testtable where recordnumber=88; dbms_output.put_line(to_char(tempdate)); end; ――――――――――――――――――――――――――――――――――――― 【配套程序位置】:第9章/ createprocedure.sql。 執行結果如圖9.41所示。
在【SQLPlus Worksheet】中執行下列PL/SQL程序,執行結果如圖9.45所示。 ――――――――――――――――――――――――――――――――――――― set serveroutput on begin tempprocedure; end; ――――――――――――――――――――――――――――――――――――― 【配套程序位置】:第9章/ executeprocedure.sql。 在Oracle中,創建好的過程可以被任何程序調用。
帶參數的過程
前面介紹的過程沒有參數,主程序和過程沒有數據的傳遞,下面介紹帶參數的過程的設計和使用。 1. 參數類型 在PL/SQL過程中,可以有3種類型的參數。 in參數:讀入參數,主程序向過程傳遞參數值。 out參數:讀出參數,過程向主程序傳遞參數值。 in out 參數:雙向參數,過程與主程序雙向交流數據。 2. 定義帶參數的過程 在下面的PL/SQL程序代碼中,將創建三個調用參數。 tempdeptno:類型為in,與scott.dept.deptno的類型一致,為數值型。 tempdname:類型為out,與scott.dept.dname的類型一致,為字符型。 temploc:類型為in out,與scott.dept.loc類型一致,為字符型。 創建兩個過程內參數。 loc1:與scott.dept.loc的類型一致,為字符型。 dname1:與scott.dept.dname的類型一致,為字符型。 該帶參數的過程的功能是從數據表scott.dept中尋找deptno字段等于tempdeptno調用參數值的dname和loc字段,和其他字符組合,送給兩個出口參數。 以system用戶名、sysdba身份登錄【SQLPlus Worksheet】,執行下列PL/SQL程序,執行結果如圖9.46所示。 ――――――――――――――――――――――――――――――――――――― Set serveroutput on create or replace procedure scott.tempprocedure( tempdeptno in scott.dept.deptno%type, tempdname out scott.dept.dname%type, temploc in out scott.dept.loc%type)as loc1 scott.dept.loc%type; dname1 scott.dept.dname%type; begin select loc into loc1 from scott.dept where deptno=tempdeptno; select dname into dname1 from scott.dept where deptno=tempdeptno; temploc:='地址:'loc1; tempdname:='姓名'dname1; end; ――――――――――――――――――――――――――――――――――――― 【配套程序位置】:第9章/ createscottprocedure.sql。