一,Oracle中的过程,函数

对于oracle中的过程和函数,个人觉得可以化为一类,因为它们在写法上并没有什么的不同。公式无非就是

create or replace Package_name(paramater1 in type,paramater2 in type,out_message out type)

  is

  locate_paramater1 type;

  locate_paramater2 type;

  begin

    begin

        select sth into locate_paramater1  from table_name where 。。。。

    exception

        when no_data_found then

         locate_paramater1:=' ';

        out_message:='Some Information to user to look';    

    end

    begin

        select sth into locate_paramater2  from table_name where 。。。。

    exception

        when no_data_found then

         locate_paramater2:=' ';

        out_message:='Some Information to user to look';    

    end

  end

总而言之,言而总之:包声明,私有数据神马的在紧跟is之后。接着就是一串的begin......exception....end;

  函数

create or replace function_name(paramater1 in type,paramater2 in type)

  return return_type;

  is

  locate_paramater1 type;

  locate_paramater2 type;

  out_message return_type;

  begin

    begin

        select sth into locate_paramater1  from table_name where 。。。。

    exception

        when no_data_found then

           locate_paramater1:=' ';

          out_message:='Some Information to user to look'; 

          return  out_message;

        when others then

          out_message:='Error'; 

          return  out_message;

    end

    begin

        select sth into locate_paramater2  from table_name where 。。。。

    exception

        when no_data_found then

         locate_paramater2:=' ';

        out_message:='Some Information to user to look'; 

        return  out_message; 

    end

  end

实战演练

create or replace   procedure inOrUp_Sc_Number(ip_sno in number,ip_courseCount in number)
is
tempt_count number(4);
e_sc_number number(4);
begin begin
--tab 是一个系统表,存储当前用户下所拥有的表信息
select 1 into e_sc_number from tab where tname='SC_Number';
exception
when no_data_found then
--注意对私有数据的赋值方式!!!!
e_sc_number:=0;
when others then
dbms_output.put_line(sqlerrm);
return;
end;
--endOfBegin
if e_sc_number=0 then
--这个是动态sql,因为在执行这句话之前不知道是否要执行,所以要用动态的
--动态sql的语法:execute immediate 'sql statement'
execute immediate
'create table SC_Number(sno number(5) primary key,cnum number(3) )';
execute immediate
'insert into SC_Number select sno,count(*) from sc group by sno';
commit;
end if; begin
--这个也是动态sql
--需要注意的是给动态sql,动态的赋值,=:number useing paramater
execute immediate
'select 1 from SC_Number where sno=:1' into tempt_count using trim(ip_sno);
exception
when no_data_found then
tempt_count:=0;
when others then
dbms_output.put_line(sqlerrm);
return;
end;
--endOfBegin
if tempt_count=1 then
begin
execute immediate
--这个还是动态sql
--需要注意的是给动态sql,动态的赋值,=:number useing paramater
'update SC_Number set cnum=:1 where sno=:2'using ip_courseCount,trim(ip_sno);
commit;
exception
when others then
dbms_output.put_line(sqlerrm);
return;
end;
end if;
--endOfIf
if tempt_count=0 then
begin
execute immediate
--为神马这么多动态sql
'insert into SC_Number values(:1,:2)'using ip_sno,ip_courseCount;
commit;
exception
when others then
dbms_output.put_line(sqlerrm);
return;
end;
end if;
--endOfIf
end;
create or replace function func_InsetOrUpdateSc(ip_sname in varchar2,ip_cname in varchar2,ip_grade in varchar2)
--这个是函数的返回类型
return varchar2
is
student_num student.sno%type;
course_num course.cno%type;
out_messsage varchar2(30);--注意私有数据声明时要标注大小
tempt_count number(4);
begin begin
select sno into student_num from student where student.snmae=ip_sname;
exception
when no_data_found then
student_num:='';
out_messsage:='No such a Student';
return out_messsage;
when others then
out_messsage:='Error';
return out_messsage;
end;
--endOfBegin
begin
select cno into course_num from course where course.cname=ip_cname;
exception
when no_data_found then
course_num:='';--私有数据的赋值方法和过程类似
out_messsage:='No such a Course';
return out_messsage;
when others then
out_messsage:='Error';
return out_messsage;
end;
--endOfBegin
begin
select 1 into tempt_count from sc where sc.sno=student_num and sc.cno=course_num;
exception
when no_data_found then
tempt_count:=0;
when others then
out_messsage:='Error';
return out_messsage;
end;
--endOfBegin
if tempt_count=1 then
begin
update sc set sc.grade=ip_grade where sc.sno=student_num and sc.cno=course_num;
--注意下面的commit语句,在update,insert时,不要忘记了哦
commit;
out_messsage:='Update Success';
return out_messsage;
exception
when others then
out_messsage:='Error';
return out_messsage;
end;
end if;
--endOfIf
if tempt_count=0 then
begin
insert into sc values(student_num,course_num,ip_grade);
commit;
out_messsage:='Insert Success!';
return out_messsage;
exception
when others then
out_messsage:='Error';
return out_messsage;
end;
end if;
--endOfIf
end;

关于Oracle过程,函数的经典例子及解析的更多相关文章

  1. oracle 过程函数,包的区别和联系

    一.过程与函数区别 1.过程可以有0~N个返回参数,通过OUT or IN OUT参数返回:函数有且仅有1个返回值,通过return语句返回. 2.调用过程时,可做为单独的语句执行:调用函数时,函数必 ...

  2. Oracle 正则表达式函数-REGEXP_SUBSTR 使用例子

    原文在这 戳 REGEXP_SUBSTR 5个参数 第一个是输入的字符串 第二个是正则表达式 第三个是标识从第几个字符开始正则表达式匹配.(默认为1) 第四个是标识第几个匹配组.(默认为1) 第五个是 ...

  3. Oracle 正则表达式函数-REGEXP_INSTR 使用例子

    原文在这 戳 REGEXP_INSTR 6个参数 第一个是输入的字符串 第二个是正则表达式 第三个是标识从第几个字符开始正则表达式匹配.(默认为1) 第四个是标识第几个匹配组.(默认为1) 第五个是指 ...

  4. Oracle 正则表达式函数-REGEXP_LIKE 使用例子

    原文在这 戳 REGEXP_LIKE 3个参数 第一个是输入的字符串 第二个是正则表达式 第三个是取值范围: i:大小写不敏感: c:大小写敏感: n:点号 . 不匹配换行符号: m:多行模式: x: ...

  5. Oracle 正则表达式函数-REGEXP_REPLACE 使用例子

    原文在这: 戳 REGEXP_REPLACE 6个参数 第一个是输入的字符串 第二个是正则表达式 第三个是替换的字符 第四个是标识从第几个字符开始正则表达式匹配.(默认为1) 第五个是标识第几个匹配组 ...

  6. oracle over 函数几个例子

    测试使用的数据为scott/tiger模式下的emp表: 我们使用JOB和SAL这两个列测试: 上面语句指按照职业JOB分组(partition by job)然后在每个分组内,按照薪水(sal)进行 ...

  7. Oracle过程及函数的参数模式,In、out、in out模式

    Oracle过程及函数的参数模式 In.out.in out模式 在Oracle中过程与函数都可以有参数,参数的类型可以指定为in.out.in out三种模式. 三种参数的具体说明,如下图所示: ( ...

  8. 24、jQuery常用AJAX-API/Java调用MySQL / Oracle过程与函数

      1)掌握jQuery常用AJAX-API 2)掌握Java调用MySQL / Oracle过程与函数 一)jQuery常用AJAX-API 目的:简化客户端与服务端进行局部刷新的异步通讯 (1)取 ...

  9. Oracle中函数/过程返回结果集的几种方式

    原文 Oracle中函数/过程返回结果集的几种方式 Oracle中函数/过程返回结果集的几种方式:    以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过.    ...

随机推荐

  1. Android的LinearLayout中的权重android:layout_weight

    当前EditText和Button部件只是适应了他们各自内容的大小,如下图所示: 这样设置对按钮来说很合适,但是对于文本框来说就不太好了,因为用户可能输入更长的文本内容.因此如果能够占满整个屏幕宽度会 ...

  2. HBase -- 基于HDFS的开源分布式NoSQL数据库

    HBase(Hadoop Database)是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,我们可以利用HBase技术在廉价的PC上搭建起大规模结构化存储集群.同Google的Bigtable ...

  3. ipmi使用

    1.安装ipmitool Linux: yum -y install OpenIPMI-tools 备注:Linux机器也可以安装ipmi  yum -y install OpenIPMI OpenI ...

  4. MQ基础

    1. 什么时候用activeMQ 在大量场合,ActiveMQ和异步消息对系统架构有意味深长的影响.下面举一些例子: 1). 异构系统集成 2). 取代RPC 3). 应用间的解耦 4). 事件驱动架 ...

  5. SqlServer跨库查询

    由于业务的拆分,数据库拆分为两种作用: 汇总数据库(Master,头节点数据库), 子节点数据库(Compute Node,计算子节点数据库) 这样,就设计到子节点访问头节点数据库中的某张汇总表,这种 ...

  6. windows下根据端口号杀死进程

    Windows不像Linux,Unix那样,ps -ef 查出端口和进程号,然后根据进程号直接kill进程. Windows根据端口号杀死进程要分三步: 第一步 根据端口号寻找进程号 C:\>n ...

  7. Codeforce Round #225 Div2

    这回的C- -,弄逆序,我以为要弄个正的和反的,没想到是等价的,弄两个还是正确的,结果我又没注意1和0只能指1个方向,结果弄了4个,取了4个的最小值就错了,自己作死没弄出来...,后面又玩去了...哎 ...

  8. Winform TreeView 节点拖动

    private void treeView_ItemDrag(object sender, ItemDragEventArgs e) { TreeNode dragNode = e.Item as T ...

  9. bean中集合属性的配置

    在实际的开发中,有的bean中会有集合属性,如下: package com.sevenhu.domain; import java.util.List; /** * Created by hu on ...

  10. Myeclipse10编写jsp时出现 Multiple annotations found at this line:

    今天,老师讲完课做了一个小练习,就是编写一个jsp页面.写完后,我发现少些了点东西,我准备使用<% %>添加是发现,报错了 Multiple annotations found at th ...