一,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. VC++ 修改资源页面语言

    选中 资源文件,在“属性”页面中更改 “language”选项: 资源视图中: 属性 页面中:

  2. Fluentd安装——通过rpm方式

    0. td-agent是何物 为了灵活性,fluentd用Ruby写的,其中一些性能敏感的部件使用c写的.普通的用户直接安装和使用Ruby进程可能有困难,这样就把它封装成Fluentd的稳定版本——t ...

  3. SQL 数据库 right join 和left join 的区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  4. html 标签自己居中

    <div style="width: 200px; height: 200px; border: 1px solid red; margin: 0 auto;">< ...

  5. Centos7 安装 Nginx

    Nginx有很多版本的,下面我给个链接http://nginx.org/packages/mainline/centos/7/x86_64/RPMS/ 下载对应当前系统版本的nginx包(packag ...

  6. poj: 2159

    简单题,看起来很凶 #include <iostream> #include <stdio.h> #include <string> #include <st ...

  7. [转]ms sql 2000 下批量 附加/分离 数据库(sql语句)

    这次公司要把MS SQL Server 2000 服务器上的数据库复制到新的服务器上面去,于是几百个数据库文件就交给我附加到新服务器上了   以前一直没接触过这方面的东西,于是果断谷歌了也百度了  找 ...

  8. paper 90:人脸检测研究2015最新进展

    搜集整理了2004~2015性能最好的人脸检测的部分资料,欢迎交流和补充相关资料. 1:人脸检测性能 1.1 人脸检测测评 目前有两个比较大的人脸测评网站: 1:Face Detection Data ...

  9. shell 命令

  10. 夺命雷公狗---Thinkphp----10之后台登录.注销一条龙

    首先我们还是还是写一个控制器名字叫LoginController.class.php的控制器,首先来写一个code的方法来让验证码先显示出来: public function Code(){ //创建 ...