Oracle的基本操作(二:存储过程)
生活随笔
收集整理的這篇文章主要介紹了
Oracle的基本操作(二:存储过程)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
1、編寫一個存儲過程,根據輸入的工作類型,輸入該工作的平均工資。
-- Created on 2018/9/30 by YANXUKUNcreate or replace procedure avgsal(v_job in scott.emp.job%type)isavgsal2 number;beginselect avg(sal) into avgsal2 from scott.emp where job = v_job;dbms_output.put_line(v_job || '---'|| avgsal2);end;
?
2、創建一個存儲過程,以員工號為參數,輸出該員工的工資;
create or replace procedure p_sxtl(v_empno in scott.emp.empno%type,v_sal out scott.emp.sal%type)is? beginselect sal into v_sal from scott.emp where empno = v_empno;end;-- Created on 2018/9/30 by YANXUKUNdeclarev_empno scott.emp.empno%type := 7369;v_sal scott.emp.sal%type;beginp_sxtl(v_empno,v_sal);dbms_output.put_line(v_empno || '員工工資為:'||v_sal);end;
?
3、創建一個存儲過程,以員工號為參數,修改該員工的工資。若該員工屬于10號部門,則工資增加150;若屬于20號部門,則工資增加200;若屬于30號部門,則工資增加250;--若屬于其他部門,則增加300。
create or replace procedure p_sxt2(v_empno in emp.empno%type)isv_deptno emp.deptno%type;v_sal emp.sal%type;Beginselect deptno into v_deptno from emp where empno=v_empno;select sal into v_sal from emp where empno=v_empno;dbms_output.put_line(v_empno || '的部門是' || v_deptno || '修改前的工資是' || v_sal);case v_deptnowhen 10 then update emp set sal=sal+150 where empno=v_empno;when 20 then update emp set sal=sal+200 where empno=v_empno;when 30 then update emp set sal=sal+250 where empno=v_empno;else? update emp set sal=sal+300 where empno=v_empno;end case;select sal into v_sal from emp where empno=v_empno;dbms_output.put_line(v_empno || '部門是' || v_deptno || '修改后的工資是:' || v_sal);commit;end;調用存儲過程:beginp_sxt2(7369);end;
4、創建一個函數,以員工號為參數,返回該員工的工資。
create or replace function f_gongzi(v_empno scott.emp.empno%type)return scott.emp.sal%type isvr_sal scott.emp.sal%type;beginselect sal into vr_sal from scott.emp where empno = v_empno;return vr_sal;end;
5、創建一個函數,以員工號為參數,返回該員工所在的部門的平均工資
create or replace function avegsal(v_empno in emp.empno%type)return emp.sal%typeasavgsal2 emp.sal%type;begin-- Test statements hereselect avg(sal) into avgsal2 from emp where deptno = (select deptno from emp where empno=v_empno);return avgsal2;end;調用函數:begin:result := avegsal(7369);end;
6、創建一個存儲過程,以員工號和部門號作為參數,修改員工所在的部門為所輸入的部門號。
--如果修改成功,則顯示“員工由……號部門調入調入……號部門”;如果不存在該員工,則顯示
--“員工號不存在,請輸入正確的員工號。”;如果不存在該部門,則顯示
--“該部門不存在,請輸入正確的部門號。”。
create or replace procedure p_deptno(v_empno in emp.empno%type,v_deptno in emp.deptno%type)asvu_empno number := 0 ;vu_deptno number :=0 ;vm_deptno emp.deptno%type;beginselect deptno into vm_deptno from emp where empno=v_empno;select count(*) into vu_empno from emp where empno=v_empno;select count(distinct deptno) into vu_deptno from emp where deptno=v_deptno;if vu_empno=0 then dbms_output.put_line('員工號不存在,請輸入正確的員工號.');end if;if vu_deptno=0 then dbms_output.put_line('該部門不存在,請輸入正確的部門號.');end if;if vu_empno=1 and vu_deptno=1 then dbms_output.put_line('員工由' || vm_deptno || '號部門調入' || v_deptno || '號部門');update emp set deptno=v_deptno where empno=v_empno;commit;end if;end;
調用存儲過程:
(1)begin
? p_deptno(7369,40);
end;
(2)begin
? p_deptno(7369,10);
end;
?
總結
以上是生活随笔為你收集整理的Oracle的基本操作(二:存储过程)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle的基本操作(一:子查询与常用
- 下一篇: MongoDB数据库(一:基本操作)