1、存储过程的理解sql
create or replace procedure 存储过程名数据库
asoracle
begin函数
nulloop
endspa
行1:create or replace procedure 是一个SQL语句,通知oracle数据库去建立一个叫作skeleton储存过程,若是存在就覆盖它。io
行2:as关键词代表后面将跟随一个pl/sql体。for循环
行3:begin关键词代表pl/sql体的开始。test
行4:null 代表pl/sql语句什么事情都不作,这句不能删除,由于pl/sql体中至少须要有一句。变量
行5:end关键词代表pl/sql体的结束。
2、存储过程建立语法
create or replace procedure 存储过程名 (param1 in type,para2 out type)
as
变量1 类型(值范围); eg: vs_msg VARCHAR2(20);
变量2 类型(值范围);
begin
select count(*) into 变量1 from 表A where 列名 = param1;
if (判断条件) then
select 列名 into 变量2 from 表A where 列名=param1;
dbms_output.put_line('打印信息');
elseif (判断条件) then
dbms_output.putline('打印信息');
else
raise 异常名 (no_data_found);
end if;
exception
when others then
rollback;
end
对如上理解:
存储过程参数不带取值范围,in表示传入,out表示输出。类型可使用任意oracle中的合法类型。
变量带取值范围,后面接分号
在判断语句以前最好先用count(*)函数判断是否存在该条操做记录
用select ... into ...给变量赋值
在代码中抛异经常使用 raise+异常名
3、
create or replace procedure 存储过程名
{
--定义参数
param1 in char(6),
param2 out number,
}
as
--定义变量
param3 varchar2(10);
param4 varchar2(16);
begin
--用输入参数给变量赋值
end
4、oracle存储过程语法
1.判断语句
if 比较式 then begin end;end if
create or replace procedure test(x in number)
as //本例子不须要定义变量,因此as后面没有变量
begin
if x > 0 then
begin
x: = -x;
end;
end if;
if x = 0 then
begin
x: = 1;
end;
end if;
end test;
2.for循环语句
for ... in ... loop
--执行语句
end loop;
(1)循环遍历游标
create or replace procedure test() //这里没有输入参数
as
Cursor cursor is select name from student;
name varchar2(16);
begin
for name in cursor loop
begin
dbms_output.putline(name); //输出表student中字段为name的全部的数据
end;
end loop;
end test;
(2)while循环
while 条件语句 loop
begin
end;
endloop;
//举个例子
create or replace procedure test(i in number)
as
begin
while i < 10 loop
begin
i:=i+1;
end;
end loop;
end test;
3.游标的使用
oracle 中的cursor很是有用,用于遍历临时表中的查询结果。其相关方法和属性不少,下面介绍一-二。
3-1 Cursor型游标 (注意:游标不能用于参数传递)
create or replace procedure test()
as
cursor1 Cursor is select name from studentTable where ...;//游标的使用方式1
cursor2 Cursor;
begin
select name into cursor2 from studentTable where...;//游标的使用方式2
可使用 for x in cursor loop ...end loop;来实现对cursor的遍历。
end test;