ORCL存储过程
简介
存储过程时在大型数据库系统中,一组为了完成特定功能的SQL语句集,经过编译后存储在数据库中,用户通过指定的存储过程的名字并给出参数(如果有)来执行它,存储过程时数据库中一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。
语法
create [or replace] PROCEDURE 过程名[(参数名 in/out 数据类型)]
AS
begin
PLSQL子程序体;
end;
或者
create [or replace] PROCEDURE 过程名[(参数名 in/out 数据类型)]
IS
begin
PLSQL子程序体;
end 过程名;
测试
问题1:给指定的员工涨100工资,并打印出涨前与涨后的工资
create or replace proceduce addSall(eno in number)
is
pemp myemp%rowtype;
begin
select * into pemp from myemp where empno=eno;
update myemp set sal=sal+100 where empno=eno;
dbms_output.put_line('涨工资前'|| pemp.sal||'涨工资后'||(pemp.sal+100));
end addSall;
调用
begin
addSall(eno => 7902);
commit;
end;
存储函数
区别
一般来讲,过程和函数的区别在于函数可以有一个返回值;而过程没有返回值。
但过程和函数都可以通过 out 指定一个或多个输出参数。我们可以利用 out 参数,在过程和函数中实
现返回多个值。
测试
create or replace function empincome(eno in emp.empno%type) return
number is
psal emp.sal%type;
pcomm emp.comm%type;
begin
select t.sal into psal from emp t where t.empno = eno;
return psal * 12 + nvl(pcomm, 0);
end;