关于 游标 if,for 的例子
create or replace procedure peace_if
is
cursor var_c is select * from grade;
begin
for temp in var_c loop
if temp.course_name = 'OS' then
dbms_output.put_line('Stu_name = '||temp.stu_name);
elsif temp.course_name = 'DB' then
dbms_output.put_line('DB');
else
dbms_output.put_line('feng la feng la ');
end if;
end loop;
end;
---关于游标 for,case 的例子1
create or replace procedure peace_case1
is
cursor var_c is select * from test_case;
begin
for temp in var_c loop
case temp.vol
when 1 then
dbms_output.put_line('haha1');
when 2 then
dbms_output.put_line('haha2');
when 3 then
dbms_output.put_line('haha3');
when 4 then
dbms_output.put_line('haha4');
else
dbms_output.put_line('qita');
end case ;
end loop;
end;
---关于游标 for,case 的例子2
create or replace procedure peace_case2
is
cursor var_c is select * from test_case;
begin
for temp in var_c loop
case
when temp.vol=1 then
dbms_output.put_line('haha1');
when temp.vol=2 then
dbms_output.put_line('haha2');
when temp.vol=3 then
dbms_output.put_line('haha3');
when temp.vol=4 then
dbms_output.put_line('haha4');
else
dbms_output.put_line('qita');
end case ;
end loop;
end;
---关于for 循环的例子
create or replace procedure peace_for
is
sum1 number :=0;
temp varchar2(500);
begin
for i in 1..9 loop
temp := '';
for j in 1 .. i
loop
sum1 := i * j;
temp := temp||to_char(i) || ' * ' ||to_char(j) ||' = ' ||to_char(sum1) ||' ';
end loop;
dbms_output.put_line(temp );
end loop;
end;
---关于 loop循环的例子
create or replace procedure peace_loop
is
sum1 number := 0;
temp number :=0 ;
begin
loop
exit when temp >= 10 ;
sum1 := sum1+temp;
temp := temp +1;
end loop;
dbms_output.put_line(sum1 );
end;
---关于游标和loop循环的例子
create or replace procedure loop_cur
is
stu_name varchar2(100);
course_name varchar2(100);
cursor var_cur is select * from grade ;
begin
open var_cur;
loop
fetch var_cur into stu_name,course_name;
exit when var_cur%notfound;
dbms_output.put_line(stu_name|| course_name);
end loop;
close var_cur;
end;
---关于异常处理的例子
create or replace procedure peace_exp(in1 in varchar2)
is
c_n varchar2(100);
begin
select course_name into c_n from grade where stu_name = in1;
dbms_output.put_line(c_n);
exception
when no_data_found
then
dbms_output.put_line('try');
when TOO_MANY_ROWS
then
dbms_output.put_line('more');
end;
---关于异常处理的例子2
create or replace procedure peace_insert ( c_n in varchar2)
is
error EXCEPTION;
begin
if c_n = 'OK'
then
insert into course (course_name) values (c_n);
elsif c_n = 'NG' then
insert into course (course_name) values (c_n);
raise error;
else
Dbms_Output.put_line('c_n' || c_n);
end if;
commit;
exception
when error then
rollback;
Dbms_Output.put_line('ERRO');
end;
---关于包的例子 定义包
create or replace package peace_pkg
as
function test1(in1 in varchar2)
return number;
procedure test2 (in2 in varchar2);
end peace_pkg;
---关于包的例子 定义包体
create or replace package body peace_pkg
as
function test1(in1 in varchar2)
return number
as
temp number;
begin
temp := 0;
return temp;
end;
procedure test2 (in2 in varchar2)
is
begin
dbms_output.put_line(in2);
end;
end peace_pkg;
oracle 存储过程和函数例子
2019-11-06 13:31peace.zhao Oracle
本文没有对PL/SQL中的东西有什么说明,只是简单的举了几个例子说明oracle 存储过程和函数,希望对大家有用!
延伸 · 阅读
- 2019-12-06oracle分页存储过程 oracle存储过程实例
- 2019-12-02oracle存储过程创建表分区实例
- 2019-11-19oracle存储过程中return和exit区别概述及测试
- 2019-11-12Oracle存储过程基本语法介绍
- 2019-11-12oracle中lpad函数的用法详解
- 2019-11-11Oracle中返回结果集的存储过程分享
- Oracle
Oracle数据库的空间管理技巧
在oracle数据库中,DBA可以通过观测一定的表或视图来了解当前空间的使用状况,进而作出可能的调整决定 ...
- Oracle
oracle 多个字符替换实现
CSDN上的一个网友,需要一个sql语句的解决方案需求是这样的求写oracle多个字符替换(有测试数据) ...
- Oracle
Oracle除去数据中的换行符以免读取出现问题
将整条数据取出,并用特殊符号分割,如果数据出现换行的情况,那么读取时就有问题,这时就可以采用下面的方法来去除 ...
- Oracle
Oracle 批处理自动备份bat脚本语句的步骤详解
这篇文章主要介绍了Oracle 批处理自动备份bat脚本语句的步骤详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友...
- Oracle
ORACLE 系统函数大全SQLSERVER系统函数的异同
下面是Oracle支持的字符函数和它们的Microsoft SQL Server等价函数。...
- Oracle
Oracle 获得以百分号结尾的脚本有三种写法
本节主要介绍了Oracle 获得以百分号结尾的脚本有三种写法,以获得ID=4443的数据行为例 ...
- Oracle
oracle中add_months()函数及用法总结
这篇文章主要介绍了oracle中add_months()函数及用法总结,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下 ...
- Oracle
Oracle与Mysql自动增长列(id)的区别
这篇文章主要介绍了Oracle与Mysql自动增长列(id)的区别,需要的朋友可以参考下 ...