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:建立带有输出参数的函数
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:建立带有输入输出参数的函数
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:建立结果缓存函数
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:调用无参数的函数
BEGIN
dbms_output.put_line(cur_datetime);
END;
/
-- 19-7:调用带有输入参数的函数
BEGIN
dbms_output.put_line('工资:'||get_sal('&name'));
END;
/
-- 19-8:调用带有输出参数的函数
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:调用带有输入输出参数的函数
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:使用位置传递为参数传递变量和数据
SELECT get_sal('&name') 工资 FROM dual;
-- 19-11:使用名称传递为参数传递变量和数据
VAR salary NUMBER
EXEC :salary:=get_sal(name=>'&name')
-- 19-12:使用组合传递为参数传递变量和数据
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语句中调用pl/sql函数
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:使用纪录类型作为返回类型
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||',部门号:'||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:删除函数
DROP FUNCTION get_name;
-- 19-18:显示编译错误
SHOW ERRORS
-- 19-19:确定函数状态
SELECT object_name FROM user_objects WHERE status='INVALID' AND object_type='FUNCTION';
-- 19-20:编译函数
ALTER FUNCTION get_info COMPILE;
-- 19-21:查看函数代码
SELECT text FROM user_source WHERE name='GET_INFO';
中国足彩网信息请查看IT技术专栏
2025国考·省考课程试听报名