Oracle儲存過程

2021-08-14 21:41:08 字數 2944 閱讀 2544

--儲存過程

--語法上來說,比函式少了返回值

create or replace procedure pro_add(in1 number,

in2 number,

out1 out number) as

begin

out1 := in1 + in2;

end pro_add;

--呼叫儲存過程

declare

v_re number;

begin

pro_add(3, 5, v_re);

dbms_output.put_line(v_re);

end;

--返回游標的儲存過程

create or replace procedure pro_table(deptid number,

cur_emp out sys_refcursor) as

begin

open cur_emp for

select * from emp where deptno = deptid;

end pro_table;

--呼叫儲存過程

declare

cur_re sys_refcursor;

deptid number := &id;

rec_emp emp%rowtype;

begin

pro_table(deptid, cur_re);

loop

fetch cur_re

into rec_emp;

exit when cur_re%notfound;

dbms_output.put_line(rec_emp.ename || ',' || rec_emp.deptno);

end loop;

end;

--刪除函式或者儲存過程

drop function get_salary;

drop procedure 儲存過程名;

--定義乙個儲存過程,返回學號是***的學生的姓名,所學課程,成績,該科教師

create or replace procedure pro_stu(stuid number,

cur_stu out sys_refcursor) as

begin

open cur_stu for

select st.s_name, su.su_name, sc.sco,te.t_name

from student st, teacher te, subject su, score sc

where st.s_id = sc.s_id

and sc.su_id = su.su_id

and su.t_id = te.t_id

and st.s_id=stuid;

end pro_stu;

--呼叫儲存過程

declare

cur_st sys_refcursor;

stuid number := &id;

v_name student.s_name%type;

v_sname subject.su_name%type;

v_sco score.sco%type;

v_tname teacher.t_name%type;

begin

pro_stu(stuid, cur_st);

loop

fetch cur_st

into v_name,v_sname,v_sco,v_tname;

exit when cur_st%notfound;

dbms_output.put_line(v_name||','||v_sname||','||v_sco||','||v_tname);

end loop;

end;

--定義乙個游標變數:cur_stu(變數名) out(輸出引數) sys_refcursor(資料型別是游標變數)

--綜合練習,建立函式fun_emp(6,5),查詢emp表中工資排序後,第6人開始,一共查5個。

--利用函式或者儲存過程執行複雜的查詢,供其它語言呼叫,效率更高,提高**的可讀性

--利用檢視來簡化查詢

create or replace function fun_emp(startnum number, mach number)

return sys_refcursor is

cur_re sys_refcursor;

begin

open cur_re for

select *

from ord_emp

where id < startnum + mach

and id >= startnum;

return cur_re;

end fun_emp;

--呼叫

declare

cur_emp sys_refcursor;

type rec_emp is record(

v_id number,

v_no emp.empno%type,

v_name emp.ename%type,

v_sal emp.sal%type);

emp1 rec_emp;

begin

cur_emp := fun_emp(3, 4);

loop

fetch cur_emp

into emp1;

exit when cur_emp%notfound;

dbms_output.put_line(emp1.v_id || ',' || emp1.v_name || ',' ||

emp1.v_sal || ',' || emp1.v_no);

end loop;

close cur_emp;

end;

Oracle儲存過程呼叫儲存過程

oracle儲存過程呼叫有返回結果集的儲存過程一般用光標的方式,宣告乙個游標,把結果集放到游標裡面,然後迴圈游標 declare newcs sys refcursor cs1 number cs2 number cstype table rowtype table列的個數和newcs返回的個數一樣...

ORACLE儲存過程

自定義函式開始 create or replace function fn wftemplateidget templatecategoryid number,organid number,templatemode number return number istemplateid number i...

Oracle 儲存過程

create or replace procedure p 有就替換,沒有就建立 iscursor c is select from emp for update begin for v emp in c loop if v emp.deptno 10 then update emp2 set sa...