procedure
定义:
create or replace procedure 名称
(param1 in number, param2 out varchar2)
is
begin
...程序块
end
/
create or replace procedure getApple
(useCode in number, apple out number)
is
begin
select apple into apple from t_user where user_code=userCode;
end;
/
var number userCode;--声明变量
exec :userCode:=121;--变量赋值
exec getApple(:userCode);//调用procedure
多个参数只对某一个赋值:
create or replace procedure addUser
(
useCode in number default user_seq.nextval,
user_name in varchar2 default '小明',
age in number default 20,
user_birth_day in date default sysdate,
apple in number default 3
)
is
begin
select apple into apple from t_user where user_code=userCode;
end;
/
调用:
exec addUser(3,‘小王’,apple=>8);
查看procedure内容:
select text from user_source where name='ADDUSER';
默认procedure的事务与外部一致
自治事务(事务只在procedure内部,与外部无关):
create or replace procedure addApple
(userCode in varchar2, apple in number)
pragma autonomous_transaction
is
begin
update t_user set apple=apple where user_code=userCode;
commit;
end;
/
授权其他用户访问procedure
grant execute on addApple to chuyf;
用户chuyf访问:exec system.addApple(3,3);--作用于用户system的t_user表
create or replace procedure getApple
(userCode in varchar2, apple out number)
authid current_user --授权其他用户访问时,访问的是自己的表
is
begin
select apple into apple from t_user where user_code=userCode;
end;
/
grant execute on getApple to chuyf;
用户chuyf访问:
var number apple;
exec system.addApple(3);--作用于用户chuyf的t_user表
pint;
报错时,使用show err显示详细的错误
select * from session_privs;--查询当前用户的权限
动态sql创建表时,当前用户的create table权限不能是继承于角色
create or replace procedure testCre
is
begin
execute immediate 'create table t_friend{ id number,fre_id number}';
end;
/