oracle 学习笔记(五)

     由于在学习oracle,总会不有自主的将它与微软的Sql Server相比较,有时候会把自己弄糊涂,就下定决心把这个两个主流数据库的存储过程总结一下。说道存储过程,那么存储过程有什么优势呢,相对于在应用程序执行sql语句。

  吸收前人的经验总结下它的优点,主要有四点。 

  ★允许模块化程序设计,以后可以重复调用;可以由专人来完成,并可独立于程序源代码而单独修改。这样一个项目在需求分析、界面设计以及数据库设计完了以后,就可以开始写存储过程了,同一时间数据访问层也可以开始写了。没有必要等详细设计说明完成了在编码的时候才开始写SQL语句。

  ★执行更快,存储过程都是预编译命令,执行起来比SQL语句更快。

  ★减少网络流量。

  ★可作为安全机制,能够屏蔽数据库,用户并不知道数据库的真实结构。

  分页存储过程是最好来说明这个过程的,下面是oracle的语句。

create  procedure fenye
(tableName in varchar2, --定义分页的表名
Pagesize in number, --定义每页的大小
pageNow in number, --定义当前第几页
myrows out number,
myPageCount out number, --定义输出这个表一共有几页
p_cursor out testpackage.test_cursor) is
v_sql varchar2(1000);
v_begin number:=(pageNow-1)*Pagesize+1; --定义某页开始选第几行
v_end number:=pageNow*Pagesize; --定义某页结束第几行
begin
v_sql:='select * from (select t1.*,rownum rn from (select * from '|| tableName
||') t1 where rownum<='||v_end||') where rn>='||v_begin;
open p_cursor for v_sql;
v_sql:='select count(*) from '|| tableName;
execute immediate v_sql into myrows;
if mod(myrows,Pagesize)=0 then
myPageCount:=myrows/Pagesize;
dbms_output.put_line(myPageCount);
else
myPageCount:=myrows/pagesize+1;
end if;
--close p_cursor;
end;

下面是微软数据库的分页存储过程

create procedure usp_Pagin
@pageSize int,
@pageIndex int,
@pageCount int output
as
declare @count int
select * from
(select row_number() over(order by sId)as num,* from student) as newTable
where num between (@pageSize*(@pageIndex-1)+1) and (@pageSize*@pageIndex)
select @count=count(*) from student
set @pageCount=ceiling(@count/convert(float,@pageSize))

从结构语法来说

      微软                                                                                           oracle

定义参数  create procedure 过程名 输入的参数名1,... as             create procedure 过程名(输入的参数名1.....) is

定义变量     declare @变量名 类型(declare @count int)             变量名 类型; //(v_sql varchar2(1000);)

变量赋值     =                                  :=

变量输入输出定义  变量名 类型 in/output                      变量名 in/out 类型

  到现在为止,就这么多吧!

原文地址:https://www.cnblogs.com/xianrongbin/p/2378177.html