1.過程【存儲過程】 CREATE [OR REPLACE] PROCEDURE <procedure name> [(<parameter list>)] IS|AS <local variable declaration> BEGIN <executable statements> [EXCEPTION <exception handlers>] END; 1.1無參存儲過程 寫一個存儲過程:實現向student表中插入10條記錄的功能 create or replace procedure pro_student is --聲明變量 begin -- 執行邏輯代碼 for i in 950..980 loop insert into student(id,name) values(i,'asasas'); commit; end loop; --異常處理 end; --注意:選中過程代碼按齒輪提交並不是執行代碼而是編譯代碼並保存在數據庫中 -- 保存在左側菜單欄中的 procedures目錄中 調用存儲過程: 1.選中過程名稱右鍵‘測試’選項執行 2.通過plsql塊調用 begin pro_student();--如果沒有參數 ()可以省略 end; 1.2 有參存儲過程 create or replace procedure pro_student2( p_id in number,-- 默認就是in 可以省略 p_name in varchar2, --沒有被out修飾的參數是常量,不能被改變 p_result out student%rowtype -- 接收返回信息 ) is begin select * into p_result from student where id=p_id and name=p_name; end; declare v_id student.id%type := '&請輸入id'; v_name student.name%type :='&請輸入姓名'; v_student student%rowtype; begin pro_student2(v_id,v_name,v_student); dbms_output.put_line(v_student.id); dbms_output.put_line(v_student.name); dbms_output.put_line(v_student.sex); dbms_output.put_line(v_student.birth); dbms_output.put_line(v_student.address); end; 案例:請根據性別或名字查詢相關記錄,並把結果 返回來 打印了出來 提示用 sys_refcursor create or replace procedure pro_student3( v_name in student.name%type, v_sex in student.sex%type, mycursor out sys_refcursor ) is v_sql varchar2(100); begin v_sql:='select * from student where 1=1'; if v_name is not null then v_sql := v_sql || 'name like ''%'||v_name||'%'''; end if; if v_sex is not null then v_sql := v_sql || 'sex = '''||v_sex||''''; end if; dbms_output.put_line(v_sql); open mycursor for v_sql; close mycursor; end; declare v_cursor sys_refcursor; v_name student.name%type:='&請輸入姓名'; v_sex student.sex%type:='&請輸入性別'; v_student student%rowtype; begin pro_student3(v_name,v_sex,v_cursor); loop fetch v_cursor into v_student; exit when v_cursor%notfound; dbms_output.put_line(v_student.id||v_student.name); end loop; close v_cursor; end; 2.函數【方法】:有顯示的返回值 CREATE [OR REPLACE] FUNCTION <function name> [(param1,param2)] RETURN <datatype> IS|AS [local declarations] BEGIN Executable Statements; RETURN result; EXCEPTION Exception handlers; END; 寫一個方法實現根據班級編號獲取班級名稱 create or replace function func_class( f_id number )return varchar2 is v_name t_class.cname%type; begin select cname into v_name from t_class where cid = f_id; return v_name; exception when no_data_found then dbms_output.put_line('找不到數據'); return null; when others then dbms_output.put_line('其他異常'); return null; end; 調用方法: 1.通過工具測試 2.通過plsql塊執行 begin dbms_output.put_line(func_class(1)); end; 3.通過SQL語句調用【方法中不能有DML操作】 select fun_test1(102) from dual; select t.*,fun_test1(t.classid) from t_student t
方法和過程的區別:
1.DML相關的操作我們一般都使用存儲過程實現
2.特定的公共的功能我們用方法實現
3.方法有顯示的返回結果
4.方法中同樣的也有 in out 關鍵字
create or replace package p_student is -- Author : BOBO -- Created : 2018-08-06 11:29:41 -- Purpose : -- Public type declarations 定義一個類型 --type <TypeName> is <Datatype>; --type myreftype is ref cursor; -- Public constant declarations -- 聲明一個常量 --<ConstantName> constant <Datatype> := <Value>; -- Public variable declarations --<VariableName> <Datatype>; -- 聲明一個變量 -- Public function and procedure declarations --function <FunctionName>(<Parameter> <Datatype>) return <Datatype>; procedure insert_student( p_id t_student.id%type, p_name t_student.name%type ); function getname( p_id t_student.id%type )return varchar2; end p_student; create or replace package body p_student is -- Private type declarations --type <TypeName> is <Datatype>; -- Private constant declarations --<ConstantName> constant <Datatype> := <Value>; -- Private variable declarations --<VariableName> <Datatype>; -- Function and procedure implementations -- function <FunctionName>(<Parameter> <Datatype>) return <Datatype> is --<LocalVariable> <Datatype>; -- begin --<Statement>; --return(<Result>); -- end; --begin -- Initialization --<Statement>; procedure insert_student( p_id t_student.id%type, p_name t_student.name%type )is begin insert into t_student(id,name)values(seq_t_student.nextval,p_name); commit; end; function getname1( p_id t_student.id%type )return varchar2 is v_name t_student.name%type; begin select name into v_name from t_student where id = p_id; return v_name; end; function getname( p_id t_student.id%type )return varchar2 is v_name t_student.name%type; begin select name into v_name from t_student where id = p_id; return getname1(p_id); end; end p_student;
不在package中聲明,直接在body中定義實現,是為了隱藏方法,同時給內部的其他方法或者存儲過程調用