【Oracle11gR2 | 学习】plsql dev存储过程的使用、存储函数、out类型的使用
生活随笔
收集整理的這篇文章主要介紹了
【Oracle11gR2 | 学习】plsql dev存储过程的使用、存储函数、out类型的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
學(xué)習(xí)資料來源于:【Oracle】黑馬57期Oracle數(shù)據(jù)庫基礎(chǔ)教程_嗶哩嗶哩 (゜-゜)つロ 干杯~-bilibili
什么是存儲過程?
就是提前編譯好一段pl/sql語言,放置在數(shù)據(jù)庫端,可以被直接調(diào)用,來反復(fù)使用,不必再寫多余的代碼。
【代碼展示】:
--給指定的員工漲工資 create or replace procedure p1(eno emp.empno%type) is beginupdate emp set sal=sal+100 where empno=eno;commit;end;select * from emp where empno=7788; --測試p1 declare beginp1(7788);end;存儲過程的查看位置為
存儲函數(shù)和存儲過程的區(qū)別到底是什么?
- 首先,最本質(zhì)的區(qū)別是存儲函數(shù)有返回值,而存儲過程無返回值(需要用到out類型參數(shù)),在語法上:
存儲過程:create or replace procedure xx(name,type)-is-begin-end;
存儲函數(shù):create or replace function xx(name,type) return-is-begin-end;
- 根據(jù)第一條特性,可得出存儲函數(shù)可用來自定義函數(shù)。而存儲過程不能用來自定義函數(shù)。
用一個案例來表征二者的本質(zhì)區(qū)別:
【代碼展示——存儲函數(shù)】:
--通過存儲函數(shù)實現(xiàn)計算指定員工的年薪 --存儲過程和存儲函數(shù)的參數(shù)都不能帶長度 --存儲函數(shù)的返回值類型不能帶長度 create or replace function f_yearsal(eno emp.empno%type) return number--數(shù)據(jù)類型不能帶長度 iss number(10); beginselect sal*12+nvl(comm,0) into s from emp where empno=eno;return s;end; --測試f_yearsal --存儲函數(shù)在調(diào)用時,返回值需要接收 declares number(10); begins:=f_yearsal(7788);dbms_output.put_line(s);end;如何在存儲過程(無return的情形下)中來對結(jié)果進行運算并返回此最終結(jié)果呢?
在參數(shù)中添加:結(jié)果變量? out? 類型
【代碼展示】:
--out類型參數(shù)如何使用 --使用存儲過程來算年薪 create or replace procedure p_yearsal(eno emp.empno%type,yearsal out number) iss emp.sal%type;c emp.comm%type; beginselect sal*12,nvl(comm,0) into s ,c from emp where empno=eno;yearsal:=s+c;end;--測試p_yearsal declareyearsal number(10); beginp_yearsal(7788,yearsal);dbms_output.put_line(yearsal);end; --凡是涉及到into查詢語句賦值或者:=賦值操作的參數(shù),都必須使用out來修飾輸出變量?
總結(jié)
以上是生活随笔為你收集整理的【Oracle11gR2 | 学习】plsql dev存储过程的使用、存储函数、out类型的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每日一测4(装箱与拆箱)
- 下一篇: shell管道重定向程序的实现