Oracle存储过程详解:https://www.2cto.com/database/201802/723493.html(比较好)
oracle中的存储过程例子:https://www.cnblogs.com/zhao123/p/3911537.html
Oracle存储过程入门及基础语法:https://www.cnblogs.com/lideng/p/3427822.html
基本语法介绍
可以看出,基本的功能实现,调用完成。
下面,来看看基本语法:
1,变量赋值
变量名 := 值;
2,判断语句。
if 比较式 then
begin
......
end;
end
if
结合起来写个简单例子:
注意事项:
存储过程参数不带取值范围,in表示传入,out表示输出; 变量带取值范围,后面接分号; 在判断语句前最好先用count(*)函数判断是否存在该条操作记录; 用select … into … 给变量赋值; 在代码中抛异常用 raise+异常名;
存储过程创建的语法:
create [or replace] procedure 存储过程名(param1 in type,param2 out type)
as
变量1 类型(值范围);
变量2 类型(值范围);
Begin
Select count(*) into 变量1 from 表A where列名=param1;
If (判断条件) then
Select 列名 into 变量2 from 表A where列名=param1;
Dbms_output.Put_line(‘打印信息’);
Elsif (判断条件) then
Dbms_output.Put_line(‘打印信息’);
Else
Raise 异常名(NO_DATA_FOUND);
End if;
Exception
When others then
Rollback;
End;
/************************************************************************************/
例子:
create or replace procedure Test(x in out number)
is
begin
if x<0 then
begin
x:= 0 - x;
end;
elsif x > 0 then --noted here elsif
begin
x:= x ;
end;
else
x:= 0;
end if;
end Test;
Test:
set serveroutput on; --没这句话,看不到dmbs_output信息。
declare
num number;
begin
num:= -1;
test(num);
dbms_output.put_line( 'num = ' || num );
end;
/******************************
num = 1
PL/SQL procedure successfully completed.
*******************************/