存储过程
plsql中语法:
create [or replace] procedure 过程名(参数名 in|out 类型)
as
begin
end
//声明pro_add_sal存储过程,作用是给指定员工涨1000元工资,并打印出涨前和涨后工资
create or replace procedure pro_add_sal(plempno in number)
as
totalSal number;
begin
select sal into totalSal from emp where empno=plempno;//查询工资并赋值给totalSal
dbms_output.put_line(totalSal);
update emp set sal = sal+1000 where empno=plempno;
dbms_output.put_line(totalSal+1000);
commit;
end;
plsql调用:
begin
pro_add_sal(1001);
end;
存储函数
plsql中语法:
create [or replace] function 方法名(参数 in|out 类型) return 参数类型
as
定义变量名 类型要和return返回类型一致
begin
return 变量名;
end
//声明fun_emp_totalsal存储函数,查询指定员工的年薪
create or replace function fun_emp_totalsal(plempno number) return number --in可以忽略,out不能忽略不写
as
totalsal number;--和上面return类型必须一致
begin
select (sal*12)+nvl(bouns,0) into totalsal from emp where empno=plempno;
return totalsal;
end
plsql调用:
declare
totalsal number;
begin
totalsal := fun_emp_totalsal(1001);
dbms_output.put_line(totalsal);
end;
tips:上面存储过程是直接语句打印输出,这里存储函数也可以用另外方式打印
存储过程与存储函数的区别:
1.定义的语法不一样procedure,function
2.function有返回值
3.function可以在select中进行调用
4.存储过程可以通过out类型来返回参数