-- 声明一个变量i
declare i number;
-- begin 开始存储过程
begin
select count(*) into i from DVD_USER;
ifi>0 and i<5 then
dbms_output.put_line('0<>5');
ELSIF i>=5 THEN
dbms_output.put_line('>=5');
elsedbms_output.put_line('<=0');
endif;
end;
case
declare i number;
BEGIN
select count(*) into i from DVD_DVD;
case i
when0then dbms_output.put_line(0);
when1then dbms_output.put_line(1);
when2then dbms_output.put_line(2);
when3then dbms_output.put_line(3);
when4then dbms_output.put_line(4);
when5then dbms_output.put_line(5);
else dbms_output.put_line('else');endcase;
end;
/
declare i number;
begin
select count(*) into i from dvd_lr;
casewhen0<i and i<5then dbms_output.put_line('0<>5');
when5<=i then dbms_output.put_line('>=5');
else dbms_output.put_line('<=0');
endcase;
end;
/
循环语句
无条件循环 LOOP
declare
i number := 1;
j number := 0;
begin
loop
/* 另一种写法
if i >= 11thenexit;
endif;
*/
exitwhen i>=5;
j := j + i;
i := i + 1;
endloop;
dbms_output.put_line(j);
end;
/
while循环
declare
i number := 1;
j number := 1;
begin
whilei <= 100 loop
ifmod(i, 2) = 0 then
j := j * i;
endif;
i := i + 1;
end loop;
dbms_output.put_line(j);
dbms_output.put_line(length(to_char(j)));
end;
/
for循环
declare
i number := 1;
j number := 1;
begin
fori in 1..100 loop
ifmod(i, 2) = 0 then
j := j * i;
endif;
end loop;
dbms_output.put_line(j);
dbms_output.put_line(length(to_char(j)));
end;
/
存储过程
存储过程,倾向于数据库操作,没有返回值,可以写事务
关键字:Procedure
语法格式:
-- 参数有三种写法
-- 入参,出参
-- 参数格式 参数名称 in(可以省略)/out/inout 数据类型
createor replace procedureaddDVDP(dvdName in varchar2, mResult out number)asbegininsertintodvd_dvdvalues(default,dvdName,default,default,default);
mResult:=1;
commit;
exception
when others then
mResult := 0;
rollback;
end addDVDP;
存储函数,倾向于数据操作,有返回值,不可以写事务
语法结构(无返回值):
createorreplace function getUserCount return numberasbegindeclare
user_count number;beginselectcount(*) into user_count from dvd_user;
return user_count;
end;end getUserCount;--调用函数select getUserCount from dual;--查看用户对象select object_name, object_type, status from user_objects orderby object_type;--查看用户资源select name,type,line,text from user_source;
语法结构(含参,注意参数不能添加长度):
createorreplace function testArgument(i number) return varchar2 asbegindeclare n varchar2(50);beginselect user_name into n from dvd_user where user_id=i;
return n;
end;end testArgument;
/
--测试函数begin
dbms_output.put_line (testArgument(3));end;
/
其他
游标(行转列) cursor
在关系型数据库中,数据都是以行/记录形式存在的,因此,Oracle提供游标来循环访问每行数据;
游标分为:
静态游标(常用):分为显式游标(常用)和隐式游标
动态游标
声明游标
declare cursor mCursor isselect dvd_id,dvd_name,dvd_date,dvd_lendcount,dvd_status from dvd_dvd orderby dvd_id;
c mCursor%rowtype;
begin
open mCursor;
fetch mCursor into c;
while mCursor%found loop
--dbms_output.put_line(c.dvd_id || '-----' || c.dvd_name|| '-----' || c.dvd_date|| '-----' || c.dvd_lendcount|| '-----' || c.dvd_status);
dbms_output.put(c.dvd_id || '-----' || c.dvd_name|| '-----' || c.dvd_date|| '-----' || c.dvd_lendcount|| '-----' || c.dvd_status);
dbms_output.put_line('');
fetch mCursor into c;
endloop;
close mCursor;
end;
流程控制条件语句 if else-- 声明一个变量ideclare i number;-- begin 开始存储过程beginselect count(*) into i from DVD_USER;if i>0 and i5 thendbms_output.put_line('0);ELSIF i>=5 THEN dbms_output.put_line