定义包头:
create or replace package ex
is
procedure get_user_name(userid in emp.empno%type) ;
function get_user_sal(userid in emp.empno%type) return number;
end ex;
定义包体:
create or replace package body ex
is
procedure get_user_name(userid in emp.empno%type)
is
.........
end;
function get_user_sal(userid in emp.empno%type) return number
is
.........
exception
when NO_data_founD then
dbms_output.put_line('NOT FOUND!');
end;
end ex;
定义存储过程:
create or replace procedure get_user_name(userid in emp.empno%type)
is
xx emp.ename%type;
begin
select ename into xx from emp where empno=userid;
dbms_output.put_line(xx);
end;
定义函数:
create or replace function get_user_sal(userid in emp.empno%type) return number
is
salary emp.sal%type;
begin
select sal into salary from emp where empno=userid;
return salary;
exception
when NO_data_founD then
dbms_output.put_line('NOT FOUND!');
end get_user_sal;
procedure
CREATE OR REPLACE PROCEDURE 过程名
(参数名 [OUT]参数类型) 默认in in/out/in out 三种参数形式
is
变量
begin
过程体
end;/
执行存储过程 exec procedure_name;
函数function
用于返回特定的数据 经常需要通过执行SQL语句来返回特定数据 基于这些操作建立特定的函数
函数包含return子句 用来进行数据操作 并返回单独的函数值 函数的调用只能在一个表达式
create or replace function 函数名(参数 in/out 参数类型)
return 类型 as/is
变量名 类型;
begin
函数过程;
return 变量名
end;
参数 默认输入参数 in
输出参数 out
输入输出参数 in/out out 需要在执行的时候设定变量来接收
调用函数 可以再任意一个PL/SQL程序块中调用 有返回值 必须作为表达式的一部分来调用
=:函数名(参数名)
删除函数 drop function 函数名;
变量类型 table.column%type 指定为某个表的某一个列的数据类型