oracle 存储过程和函数例子

作者:peace.zhao 来源:asp之家 时间:2009-08-08 22:27:00 


关于 游标 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,存储过程
0
投稿

猜你喜欢

  • 关于淘宝网导航几个让人不解的问题

    2009-03-24 21:08:00
  • 学习ASP.NET八天入门:第八天

    2007-08-07 13:55:00
  • SQLServer中字符串左对齐或右对齐显示的sql语句

    2012-06-06 19:36:45
  • 区别Javascript中的Null与Undefined

    2007-12-13 20:24:00
  • 简单方法实现网页自动适应任何分辨率任何窗口大小

    2008-09-13 19:28:00
  • CSS背景属性5个应用实例

    2009-09-13 20:54:00
  • 如何解决国外空间显示乱码问题

    2007-11-18 14:28:00
  • Safari参考样式库之webkit

    2009-07-26 09:50:00
  • 嵌入Flash应该考虑不支持Flash的浏览器

    2007-12-20 12:29:00
  • ASP的数据命名有什么规则吗?

    2009-10-28 18:23:00
  • ASP无组件汉字验证码

    2008-05-08 13:19:00
  • css中如何使div居中(垂直水平居中)

    2007-08-13 08:17:00
  • Oracle对两个数据表交集的查询

    2010-07-26 12:51:00
  • 教你四种方法用来查看mysql版本

    2009-06-28 11:13:00
  • asp #include file 与 #include virtual 的区别小结第1/2页

    2011-04-02 11:17:00
  • 不得不承认:韩国的 Web 设计很唯美

    2009-05-13 13:26:00
  • © 版权符号显示不清楚解决方法

    2008-02-18 14:46:00
  • 多维度导航探秘II

    2010-08-17 21:24:00
  • 带农历的JavaScript日期时间js代码

    2010-08-01 10:29:00
  • asp利用aspjpeg给图片生成PNG透明水印

    2009-03-20 14:01:00
  • asp之家 网络编程 m.aspxhome.com