Oracle FUNCTION创建函数与plsql调用方法 |
发表者:admin分类:数据库2017-09-04 16:39:30 阅读[1659] |
定义:CREATE [OR REPLACE] FUNCTION <过程名>[(参数列表)] RETURN 数据类型 IS
[局部变量声明]
BEGIN
可执行语句
EXCEPTION
异常处理语句
END [<过程名>];
变量的类型:in 为默认类型,表示输入; out 表示只输出;in out 表示即输入又输出;
使用:
示例1:
创建函数:
create or replace function f_1(n number) return number is
r emp%rowtype;
BEGIN
dbms_output.put_line('姓名 薪水');
select * into r from emp where empno=n;
dbms_output.put_line(r.ename||' '||r.sal); --输出结果,需要 set serverout on 才能显示.
return r.sal;
declare
n number;
m number;
begin
n:=&请输入员工号;
m:=f_1(n);
dbms_output.put_line('m的值为 '||m);
end;
DROP FUNCTION <函数名>;
[局部变量声明]
BEGIN
可执行语句
EXCEPTION
异常处理语句
END [<过程名>];
变量的类型:in 为默认类型,表示输入; out 表示只输出;in out 表示即输入又输出;
使用:
示例1:
创建函数:
create or replace function f_1(n number) return number is
r emp%rowtype;
BEGIN
dbms_output.put_line('姓名 薪水');
select * into r from emp where empno=n;
dbms_output.put_line(r.ename||' '||r.sal); --输出结果,需要 set serverout on 才能显示.
return r.sal;
END;
declare
n number;
m number;
begin
n:=&请输入员工号;
m:=f_1(n);
dbms_output.put_line('m的值为 '||m);
end;
示例1:
创建函数,查看员工年薪:
create or replace function f_annual_income(f_name emp.ename%TYPE)
return number is
y emp.sal%TYPE;
begin
select e.sal * 12 + nvl(e.comm, 0)
into y
from emp e
where lower(e.ename) = lower(f_name);
return y;
end;
PL/SQL使用或调用函数方法:
declare
v_name varchar2(10);
v_sal number;
begin
v_name :='SCOTT';
v_sal := f_annual_income(v_name);
dbms_output.put_line(v_sal);
end;
DROP FUNCTION <函数名>;
转载请标明出处【Oracle FUNCTION创建函数与plsql调用方法】。
《www.micoder.cc》
虚拟化云计算,系统运维,安全技术服务.
Tags: | [阅读全文...] |
最新评论