版权声明:如需转载,请注明出处 https://blog.csdn.net/qq_36260974/article/details/89053856
PL/SQL
存储过程
● 存储过程
-
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的 SQL 语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。
-
创建存储过程语法:
create [or replace] PROCEDURE 过程名[(参数名 in/out 数据类型)] AS begin PLSQL子程序体; End;
或者
create [or replace] PROCEDURE 过程名[(参数名 in/out 数据类型)] is begin PLSQL子程序体; End 过程名;
-
范例:创建一个输出 helloword 的存储过程
-
示例图:
-
在 plsql 中调用存储过程
begin -- Call the procedure helloworld; end;
-
范例:给指定的员工涨100工资,并打印出涨前和涨后的工资
-
分析:我们需要使用带有参数的存储过程
-
示例图:
-
范例:计算指定员工的年薪
-
示例图:
我么也可以通过 SQL 语句来调用存储过程
● 以上操作完整源码:
--存储过程的创建
create or replace procedure helloword as
begin
dbms_output.put_line('hello world');
end;
--创建涨工资的存储过程
create or replace procedure addsal(pno in myemp.empno%type) as
--定义变量
prec myemp%rowtype;
begin
select * into prec from myemp t where t.empno = pno;
update myemp t set t.sal = t.sal + 100 where t.empno = pno; --事务的提交要在调用端做
dbms_output.put_line('涨工资前是:' || prec.sal || ' 涨工资后是:' ||
(prec.sal + 100));
end;
select * from myemp t where t.empno = 7369;
---计算指定员工的年薪
create or replace procedure countYSal(pno in emp.empno%type,
ysal out number) is
psal emp.sal%type;
pcomm emp.comm%type;
begin
--多个值的赋值是按照先后顺序来赋值的
select t.sal, t.comm into psal, pcomm from emp t where t.empno = pno;
ysal := psal * 12 + nvl(pcomm, 0);
end countYSal;
--通过pl/sql程序调用存储过程
declare ysal number;
begin
countysal(7369, ysal); dbms_output.put_line(ysal);
end;
如有错误,欢迎指正!