1 --注意:函数必须有返回值
2 --创建函数
3 create or replace function f_emp(p_empno number)
4 return number
5 is
6 --定义存储返回值的变量
7 v_sal emp.sal%type;
8 begin
9 --查询操作
10 select sal into v_sal from emp where empno=p_empno;
11 return v_sal;
12 exception
13 when no_data_found then
14 DBMS_OUTPUT.put_line(p_empno||'员工没找到');
15 end;
16 /
17
18 --调用函数
19 set serverout on
20 declare
21 --定义变量接受函数的返回值
22 v_sal emp.sal%type;
23 begin
24 --直接调用函数
25 v_sal:=f_emp(7369);
26 dbms_output.put_line('7369工资为:'||v_sal);
27 exception
28 when others then
29 dbms_output.put_line('errors others!!');
30 end;
31 /