设为首页 加入收藏

TOP

Oracle基础(四)pl/sql(一)
2015-11-21 01:48:20 来源: 作者: 【 】 浏览:3
Tags:Oracle 基础 pl/sql

PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL)。PL/SQL是Oracle数据库对SQL语句的扩展。在普通SQL语句的使用上增加了编程语言的特点,所以PL/SQL就是把数据操作和查询语句组织在PL/SQL代码的过程性单元中,通过逻辑判断、循环等操作实现复杂的功能或者计算的程序语言。

总结下来就是是sql语言的扩展,sql语句+ 变量和常量+条件语句+循环语句+例外处理各种错误!

PL/SQL的作用

使用PL/SQL可以编写具有很多高级功能的程序,虽然通过多个SQL语句可能也能实现同样的功能,但是相比而言,PL/SQL具有更为明显的一些优点:

⒈能够使一组SQL语句的功能更具模块化程序特点;

⒉采用了过程性语言控制程序的结构;

⒊可以对程序中的错误进行自动处理,使程序能够在遇到错误的时候不会被中断;

⒋具有较好的可移植性,可以移植到另一个Oracle数据库中;

⒌集成在数据库中,调用更快;

⒍减少了网络的交互,有助于提高程序性能。

通过多条SQL语句实现功能时,每条语句都需要在客户端和服务端传递,而且每条语句的执行结果也需要在网络中进行交互,占用了大量的网络带宽,消耗了大量网络传递的时间,而在网络中传输的那些结果,往往都是中间结果,而不是我们所关心的。

而使用PL/SQL程序是因为程序代码存储在数据库中,程序的分析和执行完全在数据库内部进行,用户所需要做的就是在客户端发出调用PL/SQL的执行命令,数据库接收到执行命令后,在数据库内部完成整个PL/SQL程序的执行,并将最终的执行结果返馈给用户。在整个过程中网络里只传输了很少的数据,减少了网络传输占用的时间,所以整体程序的执行性能会有明显的提高。

pl/sql基础

接下来主要介绍下用pl/sql编写的在块的基础上编写过程,函数,包以及pl/sql进阶的三大控制语句下篇介绍 视图,触发器,以及分页的存储过程

块结构示意图

Pl/sql块由定义,执行,例外处理部分组成

Declear 定义常量,变,游标,例外,复杂数据类型

Begin 执行

Exception 例外处理

End;

例如:

Declare

V_ename varchar2(5);定义字符串变量

V_sal number(7,2);

Begin

Select ename ,sal into v_ename ,v_sal from emp where empno=&no;

Dbms_output.put_line(‘雇员名:’||v_ename ||’工资:’|| v_sal);

--异常处理

Exception

When no_date_found thendbms_output.put_line(‘朋友,输入错误’);

End

使用Sqlplus开发工具:Pl/sql develper独立工具

(一)1、创建一个简单的表

Createtable mytest(name varchar2(30),passwd varchar2(30));

2、创建过程

Create or replace procedure sp_prol is

Begin

--执行部分

Insert into mytest values(‘韩顺平’,‘m1234’);

End;

3、调用

Exec 过程名(参数)

Call过程名(参数)

?

无返回值的存储过程

Create procedure sp_pro3(spnamevarchar2,newSal number)is

Begin

--执行部分,根据用户名去修改工资

Updateemp set sal= newsal where ename=spName;

End;

(二) 函数

Createfunction sp_fun2(spName varchar2) return

Number isyearSal number(7,2);

Begin

--执行部分

Selectsal*12+nvl(comm,0)*12 into yearSal from emp where ename =spName;

ReturnyearSal;

End;

调用函数

Sql varincome number

call annual_income(‘scott’) from into:income;

Sql>printincome

Java程序中通过rs.getInt(1)得到返回的结果

Sql>showerorr--显示错误

(三)包

--创建包

--创建一个包sp_package

__声明了该包里有一个过程update_sal

---声明一个函数

Createpackage sp_packge is

procedureupdate_sal(name varchar2,newsal number);

Functionannual_income(name varchar2) reture number;

End ;

创建包体

Createpackage body sp_package is

Procedure

Function

Begin

Select

Return

End

End

调用

Sql>call sp_package.function (procedule)

Pl/sql进阶

三种条件分支

If then

Create or replace procedure sp_pro6(spName varchar2) is

--定义

v_sal smp.sal%type;

Begin

--执行

Select sal into v_sal from empwhere ename=spName;

--判断

If v_sal<2000 then

Update empset sal=sal-sal*10% where ename =spName;

End if;

End;

调用

Sql> execsp_pro6('scott') scott为用户名

二重条件分支 if- then -else

Create or replace proceduresp_pro6(spName varchar2) is

- -定义

v_sal smp.sal%type;

Begin

--执行

Select sal into v_sal from empwhere ename=spName;

--判断

If v_com<>0 then

Update empset comm=comm+100 where ename =spName;

Else

Update empset comm=comm+200 where ename=spName;

End if;

End;

调用

Sql> execsp_pro6('scott') scott为用户名

多重条件分支 if-then-elseif --else

如果该员工的职位是president,就给他的工资增加1000,如果该员工的职位是manager就给他的工资增加500 ,其他职位的员工增加200

Create orreplace procedure sp_pro6(spNo number) is

--定义

v_job emp.job%type;

Begin

--执行

Selectjob into v_job from emp where empno=spNo;

If v_job='president' then

Update emp set sal=sal + 1000 where empno=spNo;

Elseif v_job=

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Oracle存储过程学习笔记 下一篇oracle查询某张表某个时间点的数..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: