RETURN NUMBER
AS
v_sal emp.sal%TYPE;
BEGIN
SELECT sal INTO v_sal FROM emp WHERE upper(ename)=upper(name);
RETURN v_sal;
END;
/
-- 19-3:建立帶有輸出參數(shù)的函數(shù)
CREATE OR REPLACE FUNCTION get_info
(eno NUMBER,title OUT VARCHAR2) RETURN VARCHAR2
AS
name emp.ename%TYPE;
BEGIN
SELECT ename,job INTO name,title FROM emp
WHERE empno=eno;
RETURN name;
END;
/
-- 19-4:建立帶有輸入輸出參數(shù)的函數(shù)
CREATE OR REPLACE FUNCTION get_upd_info
(eno NUMBER,sal_chg IN OUT NUMBER) RETURN VARCHAR2
AS
name emp.ename%TYPE;
BEGIN
UPDATE emp SET sal=sal+sal_chg WHERE empno=eno
RETURNING ename,sal INTO name,sal_chg;
RETURN name;
END;
/
-- 19-5:建立結(jié)果緩存函數(shù)
CREATE OR REPLACE FUNCTION get_name(no VARCHAR2)
RETURN NUMBER RESULT_CACHE RELIES_ON(emp)
AS
v_name emp.ename%TYPE;
BEGIN
SELECT ename INTO v_name FROM emp WHERE empno=no;
RETURN v_name;
END;
/
-- 19-6:調(diào)用無參數(shù)的函數(shù)
BEGIN
dbms_output.put_line(cur_datetime);
END;
/
-- 19-7:調(diào)用帶有輸入?yún)?shù)的函數(shù)
BEGIN
dbms_output.put_line('工資:'||get_sal('&name'));
END;
/
-- 19-8:調(diào)用帶有輸出參數(shù)的函數(shù)
DECLARE
v_name emp.ename%TYPE;
v_job emp.job%TYPE;
BEGIN
v_name:=get_info(&eno,v_job);
dbms_output.put_line('姓名:'||v_name||',崗位:'||v_job);
END;
/
-- 19-9:調(diào)用帶有輸入輸出參數(shù)的函數(shù)
DECLARE
v_empno emp.empno%TYPE;
v_name emp.ename%TYPE;
v_salchg emp.sal%TYPE;
BEGIN
v_empno:=&eno;
v_salchg:=&incre;
v_name:=get_upd_info(v_empno,v_salchg);
dbms_output.put_line('姓名:'||v_name||',新工資:'||v_salchg);
END;
/
-- 19-10:使用位置傳遞為參數(shù)傳遞變量和數(shù)據(jù)
SELECT get_sal('&name') 工資 FROM dual;
-- 19-11:使用名稱傳遞為參數(shù)傳遞變量和數(shù)據(jù)
VAR salary NUMBER
EXEC :salary:=get_sal(name=>'&name')
-- 19-12:使用組合傳遞為參數(shù)傳遞變量和數(shù)據(jù)
VAR name VARCHAR2(10)
VAR sal_chg NUMBER
EXEC :sal_chg:=200
EXEC :name:=get_upd_info(&eno,:sal_chg)
PRINT name sal_chg
-- 19-13:在sql語(yǔ)句中調(diào)用pl/sql函數(shù)
SELECT get_sal(name=>'scott') salary FROM dual;
-- 19-14:使用異常處理
CREATE OR REPLACE FUNCTION get_sal(name VARCHAR2)
RETURN NUMBER
AS
v_sal emp.sal%TYPE;
BEGIN
SELECT sal INTO v_sal FROM emp
WHERE upper(ename)=upper(name);
RETURN v_sal;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20000,'該雇員不存在');
END;
/
-- 19-15:使用紀(jì)錄類型作為返回類型
CREATE OR REPLACE FUNCTION get_info
(eno NUMBER) RETURN emp%ROWTYPE
IS
emp_record emp%ROWTYPE;
BEGIN
SELECT * INTO emp_record FROM emp WHERE empno=eno;
RETURN emp_record;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20000,'該雇員不存在');
END;
/
DECLARE
emp_record emp%ROWTYPE;
BEGIN
emp_record:=get_info(&eno);
dbms_output.put_line('姓名:'||emp_record.ename||',部門號(hào):'||emp_record.deptno);
END;
/
-- 19-16:使用集合類型作為返回類型
CREATE OR REPLACE TYPE ename_table_type IS TABLE OF VARCHAR2(10);
/
CREATE OR REPLACE FUNCTION get_name
(dno NUMBER) RETURN ename_table_type IS
ename_table ename_table_type;
BEGIN
SELECT ename BULK COLLECT INTO ename_table FROM emp WHERE deptno=dno;
RETURN ename_table;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20099,'該部門不存在');
END;
/
DECLARE
ename_table ename_table_type;
BEGIN
ename_table:=get_name(&dno);
FOR i IN 1..ename_table.COUNT LOOP
dbms_output.put_line('姓名:'||ename_table(i));
END LOOP;
END;
/
-- 19-17:刪除函數(shù)
DROP FUNCTION get_name;
-- 19-18:顯示編譯錯(cuò)誤
SHOW ERRORS
-- 19-19:確定函數(shù)狀態(tài)
SELECT object_name FROM user_objects WHERE status='INVALID' AND object_type='FUNCTION';
-- 19-20:編譯函數(shù)
ALTER FUNCTION get_info COMPILE;
-- 19-21:查看函數(shù)代碼
SELECT text FROM user_source WHERE name='GET_INFO';
更多信息請(qǐng)查看IT技術(shù)專欄