--第一个存储过程
create or replace procedure sayhelloworld
as
begin
dbms_output.put_line("hello world");
end;
--调用存储过程
--1.execute exec
--2.在存储过程中调用
/*
begin
sayhelloworld();
end;
*/
--带参数的存储过程
create or replace procedure raisesalary(eno in number)
--指定参数是输入参数还是输出参数
as
psal emp.sal%type;
begin
select sal into psal from emp where empno = eno;
update emp set sal=sal+100 where empno = eno;
dbms_output.put_line(psal);
end;
/*
注意:
一般不在存储过程或者存储函数中,commit rollback
*/
存储函数,可带参数,并返回一个计算值。
函数和过程的结构类似,但必须有一个return子句,用于返回函数值。
--查询某个员工的年收入
create or replace function querysal(eno in number)
return number;
as
--定义变量要存员工的薪水和奖金
psal emp.sal%type;
pcomm emp.comm%type;
begin
select sal,comm into psal,pcomm from emp where empno=eno;
return psal*12+nvl(pcomm,0);
end;
/*
过程和函数多可以通过out指定一个或多个输入参数。我们可以利用
out参数,在过程和函数中实现返回多个值。
--过程可以通过out参数来实现有返回值
*/
--如果只有一个返回值,用存储函数。否则用存储过程。
--查询某个员工姓名、月薪和职位
create or replace procedure queryinfo(
eno in number,
pename out varchar2,
psal out number,
pjob out varchar2
)
as
begin
select ename,sal,job from emp where empno = eno;
end;
oracle存储过程和存储函数(1)
最新推荐文章于 2022-11-16 18:44:43 发布