create [or replace] PROCEDURE過程名(參數列表)
AS
PLSQL子程序體;
1. exec sayHello(); 2. begin sayHello(); end; / */ create or replace procedure sayHello as --變量說明 begin dbms_output.put_line('Hello World'); end; /
set serveroutput on
begin
raisesalary(7369);
end;
/
set serveroutput on
exec raisesalary(7369);
代碼: ************************************************************************************************** /* 為指定的職工在原工資的基礎上長10%的工資,並打印漲工資前和漲工資後的工資 可能用到的sql語句 update emp set sal = sal * 1.1 where empno = empid; */ create or replace procedure raiseSalary(empid in number) as pSal emp.sal%type; --保存員工當前工資 begin --查詢該員工的工資 select sal into pSal from emp where empno=empid; --給該員工漲工資 update emp set sal = sal * 1.1 where empno = empid; --打印漲工資前後的工資 dbms_output.put_line('員工號:' || empid || ' 漲工資前:' || psal || ' 漲工資後' || psal * 1.1); end; / **************************************************************************************************
代碼: ************************************************************************************************** create or replace procedure raiseSalary2(empid in number, rate in NUMBER) as pSal emp.sal%type; --保存員工當前工資 begin --查詢該員工的工資 select sal into pSal from emp where empno=empid; --給該員工漲工資 update emp set sal = sal * rate where empno = empid; --打印漲工資前後的工資 dbms_output.put_line('員工號:' || empid || ' 漲工資前:' || psal || ' 漲工資後' || psal * rate); end; / **************************************************************************************************
CREATE [OR REPLACE] FUNCTION函數名(參數列表)
RETURN 函數值類型
AS
PLSQL子程序體;
**************************************************************************** /* 查詢某職工的總收入。 */ create or replace function queryEmpSalary(empid in number) RETURN NUMBER as pSal number; --定義變量保存員工的工資 pComm number; --定義變量保存員工的獎金 begin select sal,comm into pSal, pcomm from emp where empno = empid; return psal*12+ nvl(pcomm,0); end; / ****************************************************************************
declare
v_salnumber;
begin
v_sal:=queryEmpSalary(7934);
dbms_output.put_line('salary is:' ||v_sal);
end;
/
begin
dbms_output.put_line('salaryis:' ||queryEmpSalary(7934));
end;
/* out參數:查詢某個員工的姓名,月薪和職位 */ create or replace procedure queryEmpInfo(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; /
package demo.test; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.ResultSet; import oracle.jdbc.OracleTypes; import oracle.jdbc.OracleCallableStatement; import org.junit.Test; import demo.utils.JDBCUtills; /* * Statement < PreparedStatement < CallableStatement */ public class TestOracle { /* * create or replace procedure queryEmpInfo(eno in number, pename out varchar2, psal out number, pjob out varchar2) */ @Test public void testProcedure(){ //存儲過程測試用例 //{call procedure-name(??/)} String sql = "{call queryEmpInfo(?,?,?,?)}"; Connection conn = null; CallableStatement call = null; try{ conn = JDBCUtills.getConnection(); call = conn.prepareCall(sql); //Set value to param call.setInt(1, 7839); //declare out parameter call.registerOutParameter(2, OracleTypes.VARCHAR); call.registerOutParameter(3, OracleTypes.NUMBER); call.registerOutParameter(4, OracleTypes.VARCHAR); //run call.execute(); //get returned values String name = call.getString(2); double sal = call.getDouble(3); String job = call.getString(4); System.out.println(name); System.out.println(sal); System.out.println(job); }catch(Exception ex){ ex.printStackTrace(); }finally{ JDBCUtills.release(conn, call, null); } } /* * create or replace function queryEmpIncome(eno in number) return number */ @Test public void testFunction(){ //存儲函數測試用例 //{?=call procedure-name(??/)} String sql = "{?= call queryEmpIncome(?)}"; Connection conn = null; CallableStatement call = null; try{ conn = JDBCUtills.getConnection(); 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 ex){ ex.printStackTrace(); }finally{ JDBCUtills.release(conn, call, null); } } @Test public void testCursor(){ //訪問游標測試用例 String sql = "{call MYPACKAGE.queryEmpList(?,?)}"; //注意此句要有{} Connection conn = null; CallableStatement call = null; ResultSet rs = null; try{ conn = JDBCUtills.getConnection(); call = conn.prepareCall(sql); call.setInt(1, 20); call.registerOutParameter(2, OracleTypes.CURSOR); call.execute(); rs = ((OracleCallableStatement)call).getCursor(2); while(rs.next()){ String name = rs.getString("ename"); double sal = rs.getDouble("sal"); System.out.println(name+ " " + sal); } }catch(Exception ex){ ex.printStackTrace(); }finally{ JDBCUtills.release(conn, call, rs); } } }
CREATE [or REPLACE] TRIGGER 觸發器名 {BEFORE | AFTER} {DELETE | INSERT | UPDATE [OF列名]} ON 表名 [FOR EACH ROW [WHEN(條件) ] ] PLSQL塊
/* 第一個觸發器:對update */ create or replace trigger sayHello after update on emp begin dbms_output.put_line('Hello World'); end; /
1. 周末:星期六 星期日 to_char(sysdate,'day') 2. <9 or >18點 to_number(to_char(sysdate,'hh24')) */ create or replace trigger securityEmp before insert on emp begin if to_char(sysdate,'day') in ('星期三','星期六','星期日') or to_number(to_char(sysdate,'hh24')) not between 9 and 18 then raise_application_error(-20001,'不能在非工作時間插入數據'); end if; end; /
*/ create or replace trigger checksal before update on emp for each row begin if :old.sal > :new.sal then raise_application_error(-20002,'漲後的工資不能少於漲前的。漲後:'||:new.sal||' 漲前:'||:old.sal); end if; end; /
***************************************************************************** /* 練習:限制每個部門只招聘5名職工,超過計劃則報出錯誤信息 */ create or replace trigger limitEmpCount before insert on emp for each row declare pCount number;-- 保存每個部門的員工數 begin select count(*) into pcount from emp where deptno=:new.deptno; if pcount > 5 then raise_application_error(-20004,'部門:' || :new.deptno || ' 員工已有5人'); end if; end;