-- 存储过程语法结构
create [or replace] procudure(存储过程关键字) pro_name(自定义存储过程名称)
(argument1 datatype,.....)
is[as]
声明
begin
...
exception
...
end;
select * from emp2;
-- 创建存储过程
create or replace procedure pro_emp2
is
begin
update emp2 set sal=sal+300;
dbms_output.put_line('更新成功!');
end;
--存储过程调用 1.call 存储过程名称() 2. exec 存储过程
--执行无参存储过程
call pro_emp2(); //sql window,command window 都可以执行
exec pro_emp2; //执行应用,在command window下执行
select * from emp2;
-- 创建带有输入(in)参数的存储过程
create or replace procedure pro_select
(v_empno in emp.empno%type)
is
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno=v_empno;
dbms_output.put_line('员工薪资:'||v_sal);
end;
--执行有参的存储过程
call pro_emp2(7369); //sql window,command window 都可以执行
exec pro_emp2(7369); //执行应用,在command window下执行
--执行带有输出参数的存储过程 out
--根据员工的编号,输出员工的薪资
create or replace procedure pro_select
(v_empno in emp.empno%type,v_sal out emp.sal%type)
is
begin
select sal into v_sal from emp where empno=v_empno;
end;
--带有输入,输出参数的存储过程调用
匿名程序块执行
declare
v_sal emp.sal%type;
begin
pro_select(7369,v_sal); --作为一个子程序执行
dbms_output.put_line('薪资:'||v_sal);
end;
-- 带有 in out 参数的存储过程
create or replace procedure pro_in_out(param_num in out number)
as
begin
select sal into param_num from emp where empno=param_num;
end;
--调用 in out 参数的存储过程
declare
param_num number:=7369;
begin
pro_in_out(param_num);
dbms_output.put_line('薪资:'||param_num);
end;
-- 参数传递方式 :1.位置传递 2.名称传递 3.组合传递
create or replace procedure pro_add_dept(v_deptno number,v_dname varchar2, v_loc varchar2)
as
begin
insert into dept values(v_deptno,v_dname,v_loc);
end;
1.位置传递 --调用时按参数的排列顺序
call pro_add_dept(2,'云和学习部','老君山');
2.名称传递 --按名称传递是指在调用时按照形参与实参的名称写出实参所对应的形参,将形参与实参关联起来进行传递
call pro_add_dept(v_loc=>'峨眉山',v_deptno=>3,v_dname=>'php培训部');
-- 函数的创建
create or replace function 函数名称(arg1 argType,arg2 arg2Type)
return dataType
is[as]
v_empno number;
begin
....
end;
--创建一个生成随机数的函数
create or replace function fun_random
return number
as
v_num number;
begin
v_num:=floor(dbms_random.value(1,100));
return v_num;
end;
函数的调用
declare
num number;
begin
num:=fun_random();
dbms_output.put_line('随机数:'||num);
end;
//创建输入输出过程
create or replace procedure proce_in(v_empno in emp3.empno%type,v_ename out emp3.ename%type) as begin select ename into v_ename from emp3 where empno = v_empno; dbms_output.put_line('名称是'||v_ename); end; //输入输出过程调用 declare v_ename1 emp3.ename%type; v_empno1 emp3.empno%type; begin v_empno1:=&empno; proce_in(v_empno1,v_ename1); dbms_output.put_line('名称是'||v_ename1); end; // declare v_ename1 emp3.ename%type; begin proce_in(7369,v_ename=>v_ename1); dbms_output.put_line('名称是'||v_ename1); end; //创建函数---必须由返回值 create or replace function fun_select(f_empno in emp3.empno%type,f_ename out emp3.ename%type) return emp3.ename%type as begin select ename into f_ename from emp3 where emp3.empno = f_empno; return f_ename; end; create or replace procedure pro_one(no in emp.empno%type) as name emp.ename%type; begin select ename into name from emp where emp.empno = no; dbms_output.put_line(name); end; create or replace procedure pro_emp2 is begin update emp3 set sal= sal+200; end;
转载请注明原文地址: https://ju.6miu.com/read-4774.html