pl/sql存储过程

初识存储过程
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象。
包含一系列PL/SQL语句的集合。

创建存储格式
CREATE [OR REPLACE] PROCEDURE procedure_name
(argument1 [mode1] datatype1,
argument2 [mode2] datatype2, …)
AS [IS]
声明部分
BEGIN
执行部分
EXCEPTION
异常处理部分
END;
调用存储过程格式
call proc_update_emp();

示例:
create or replace procedure pro_stu_update
as
begin
update student set stu_name=‘edit_name’ where stu_id=5;
end;
– 调用
call pro_stu_update();

in 示例
– 根据员工号,查询员工工资
create or replace procedure
– in表示入参
pro_emp_selectArray(v_empId in employees.employee_id%type)
as v_sal employees.salary%type;
begin
select salary into v_sal from employees where employee_id=v_empId;
DBMS_OUTPUT.PUT_LINE(‘salary:’ || v_sal);
end;
– call调用
call pro_emp_selectArray(198);

in/out 示例
– 根据员工号,查询员工工资 带out参数
create or replace procedure
– in表示入参,out表示出参
pro_emp_selectArray(v_empId in employees.employee_id%type,v_sal out employees.salary%type)
as
begin
select salary into v_sal from employees where employee_id=v_empId;
DBMS_OUTPUT.PUT_LINE(‘salary:’ || v_sal);
end;
– PL/SQL调用
declare
– 对应的参数类型和数量保持一致
v_empId employees.employee_id%type := ‘&input_empId’;
v_sal employees.salary%type;
begin
pro_emp_selectArray(v_empId,v_sal);
exception
when no_data_found then
DBMS_OUTPUT.PUT_LINE(‘找不到对应员工’);
end;

inout示例
create or replace procedure
– in/out 参数共用,必须保持参数类型相同
pro_emp_selectArray(v_param in out number)
as
begin
select manager_id into v_param from employees where employee_id=v_param;
DBMS_OUTPUT.PUT_LINE(‘salary:’ || v_param);
end;
– PL/SQL调用
declare
v_param number := ‘&input_param’;
begin
pro_emp_selectArray(v_param);
exception
when no_data_found then
DBMS_OUTPUT.PUT_LINE(‘找不到对应员工’);
end;

多参数传递 示例
create or replace procedure
pro_multi_params(param1 in number,param2 in number,param3 in number)
as v_sum number;
begin
v_sum := param1+param2+param3;
DBMS_OUTPUT.PUT_LINE(‘v_sum:’ || v_sum);
end;
– call调用
call pro_multi_params(1,2,3);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值