概述
PL/SQL中的過程和函數(shù)(通常稱為子程序)是PL/SQL塊的一種特殊的類型,這種類型的子程序可以以編譯的形式存放在數(shù)據(jù)庫中,并為后續(xù)的程序塊調(diào)用。
相同點(diǎn): 完成特定功能的程序 
不同點(diǎn):是否用return語句返回值。
舉個例子:
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)建了改程序并將其存儲在數(shù)據(jù)庫中,就可以使用如下的方式調(diào)用該過程
begin PrintStudents('Computer Science'); PrintStudents('Match');end;/或者
exec PrintStudents('Computer Science');exec PrintStudents('Match');在命令窗口中: 
在pl/sql工具的sql窗口中: 
存儲過程的創(chuàng)建和調(diào)用
基本語法
create [ or replace] procedure procedure_name[( argument [ {IN | OUT | IN OUT }] type,......argument [ {IN | OUT | IN OUT }] type ) ] { IS | AS}procedure_body無參的存儲過程
/** 無參數(shù)的存過 打印hello world 調(diào)用存儲過程: 1. exec sayhelloworld(); 2 begin  sayhelloworld(); end; /*/create or replace procedure sayhelloworldas--說明部分begin dbms_output.put_line('hello world');end sayhelloworld;調(diào)用過程:
SQL> set serveroutput on ;SQL> exec sayhelloworld();hello worldPL/SQL procedure successfully completedSQL> begin 2 sayhelloworld(); 3 sayhelloworld(); 4 end; 5 /hello worldhello worldPL/SQL procedure successfully completed
帶參數(shù)的存儲過程
/**創(chuàng)建一個帶參數(shù)的存儲過程給指定的員工增加工資,并打印增長前后的工資*/create or replace procedure addSalary(staffName in xgj_test.username%type )as--定義一個變量保存調(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語句之后并沒有commit的操作。 
一般來講為了保證事務(wù)的一致性,由調(diào)用者來提交比較合適,當(dāng)然了是需要區(qū)分具體的業(yè)務(wù)需求的~
begin addSalary('xiao');addSalary('gong');commit ;end ;/存儲函數(shù)
基本語法
create [ or replace] function function_name[( argument [ {IN | OUT | IN OUT }] type,......argument [ {IN | OUT | IN OUT }] type ) ] RETURN { IS | AS}function_body其中 return子句是必須存在的,一個函數(shù)如果沒有執(zhí)行return就結(jié)束將發(fā)生錯誤,這一點(diǎn)和存過有說不同。
存儲函數(shù)
準(zhǔn)備的數(shù)據(jù)如下: 
/**查詢員工的年薪 (月工資*12 + 獎金)*/create or replace function querySalaryInCome(staffName in varchar2) return number as --定義變量保存員工的工資和獎金 pSalary xgj_test.sal%type; pComm xgj_test.comm%type;begin --查詢員工的工資和獎金 select t.sal, t.comm into pSalary, pComm from xgj_test t where t.username = staffName; --直接返回年薪 return pSalary * 12 + pComm;end querySalaryInCome;

存在一個問題,當(dāng)獎金為空的時候,算出來的年收入竟然是空的。
因?yàn)?如果一個表達(dá)式中有空值,那么這個表達(dá)式的結(jié)果即為空值。
所以我們需要對空值進(jìn)行處理, 使用nvl函數(shù)即可。
最后修改后的function為
create or replace function querySalaryInCome(staffName in varchar2) return number as --定義變量保存員工的工資和獎金 pSalary xgj_test.sal%type; pComm xgj_test.comm%type;begin --查詢員工的工資和獎金 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ù)
一般來講,存儲過程和存儲函數(shù)的區(qū)別在于存儲函數(shù)可以有一個返回值,而存儲過程沒有返回值。
那我們?nèi)绾芜x擇存儲過程和存儲函數(shù)呢?
原則:
如果只有一個返回值,用存儲函數(shù),否則(即沒有返回值或者有多個返回值)使用存儲過程。
/**根據(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) isbegin --查詢該員工的薪資,獎金和職位 select t.sal,t.comm,t.job into pSal,pComm,pJob from xgj_test t where t.username=staffName;end QueryStaffInfo;

先拋出兩個思考問題: