欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Oracle 存储过程教程

程序员文章站 2023-12-05 08:19:46
oracle分页存储过程的思路于sqlserver的思路是一样的,但是我这里做了点改动,在因为oracle的语法和规则的不同所以,oracle分页存储过程看上去有点不一样。...
oracle分页存储过程的思路于sqlserver的思路是一样的,但是我这里做了点改动,在因为oracle的语法和规则的不同所以,oracle分页存储过程看上去有点不一样。见笑,见笑!
在oracle的存储过程中返回记录集,需要用到游标变量,oracle不能像sqlserver那样可以直接返回一个记录集。
由于设想在.net中把复杂的sql语句生成,所以在存储过程中没有去考虑生成sql语句的问题。
以下是在oracle中实现的分页存储过程。
复制代码 代码如下:

create or replace package dotnet is
-- author : good_hy
-- created : 2004-12-13 13:30:30
-- purpose :
type type_cur is ref cursor; --定义游标变量用于返回记录集
procedure dotnetpagination(
pindex in number, --分页索引
psql in varchar2, --产生dataset的sql语句
psize in number, --页面大小
pcount out number, --返回分页总数
v_cur out type_cur --返回当前页数据记录
);
procedure dotnetpagerecordscount(
psqlcount in varchar2, --产生dataset的sql语句
prcount out number --返回记录总数
);
end dotnot;
create or replace package body dotnet is
--***************************************************************************************
procedure dotnetpagination(
pindex in number,
psql in varchar2,
psize in number,
pcount out number,
v_cur out type_cur
)
as
v_sql varchar2(1000);
v_count number;
v_plow number;
v_phei number;
begin
------------------------------------------------------------取分页总数
v_sql := 'select count(*) from (' || psql || ')';
execute immediate v_sql into v_count;
pcount := ceil(v_count/psize);
------------------------------------------------------------显示任意页内容
v_phei := pindex * psize + psize;
v_plow := v_phei - psize + 1;
--psql := 'select rownum rn,t.* from cd_ssxl t' ; --要求必须包含rownum字段
v_sql := 'select * from (' || psql || ') where rn between ' || v_plow || ' and ' || v_phei ;
open v_cur for v_sql;
end dotnetpagination;
--**************************************************************************************
procedure dotnetpagerecordscount(
psqlcount in varchar2,
prcount out number
)
as
v_sql varchar2(1000);
v_prcount number;
begin
v_sql := 'select count(*) from (' || psqlcount || ')';
execute immediate v_sql into v_prcount;
prcount := v_prcount; --返回记录总数
end dotnetpagerecordscount;
--**************************************************************************************
end dotnot;

以下是在.net中调用oracle分页存储过程的步骤。
在.net调用返回记录集的存储过程,需要用到datareader,但是datareader不支持在datagrid中的分页,因此需要利用datagrid自定义分页功能。
复制代码 代码如下:

rotected withevents datagrid1 as system.web.ui.webcontrols.datagrid
dim conn as new oracleclient.oracleconnection()
dim cmd as new oracleclient.oraclecommand()
dim dr as oracleclient.oracledatareader
private sub gridbind(byval pindex as integer, byval psql as string, optional byval psize as integer = 10)
conn.connectionstring = "password=gzdlgis;user id=gzdlgis;data source=gzgis"
cmd.connection = conn
cmd.commandtype = commandtype.storedprocedure
conn.open()
'------------------------------------------------------------------------------------
cmd.commandtext = "dotnot.dotnetpagerecordscount"
'------------------------------------------------------------------------------------
cmd.parameters.add("psqlcount", oracletype.varchar).value = psql
cmd.parameters.add("prcount", oracletype.number).direction = parameterdirection.output
cmd.executenonquery()
me.datagrid1.allowpaging = true
me.datagrid1.allowcustompaging = true
me.datagrid1.pagesize = psize
me.datagrid1.virtualitemcount = cmd.parameters("prcount").value
cmd.parameters.clear()
'------------------------------------------------------------------------------------
cmd.commandtext = "dotnot.dotnetpagination"
'------------------------------------------------------------------------------------
cmd.parameters.add("pindex", data.oracleclient.oracletype.number).value = pindex
cmd.parameters.add("psql", data.oracleclient.oracletype.varchar).value = psql '"select rownum rn,t.* from cd_ssxl t"
cmd.parameters.add("psize", data.oracleclient.oracletype.number).value = psize
cmd.parameters.add("v_cur", data.oracleclient.oracletype.cursor).direction = parameterdirection.output
cmd.parameters.add("pcount", data.oracleclient.oracletype.number).direction = parameterdirection.output
dr = cmd.executereader()
me.datagrid1.datasource = dr
me.datagrid1.databind()
dr.close()
conn.close()
response.write("总计页数 " & cmd.parameters("pcount").value)
end sub
----------------------------------------------------------------------------------------
private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
if not page.ispostback then
dim psql as string = "select rownum rn,t.* from cd_ssxl t"
gridbind(0, psql, 20)
end if
end sub
---------------------------------------------------------------------------------------
private sub datagrid1_pageindexchanged(byval source as object, byval e as system.web.ui.webcontrols.datagridpagechangedeventargs) handles datagrid1.pageindexchanged
dim psql as string = "select rownum rn,t.* from cd_ssxl t"
me.datagrid1.currentpageindex = e.newpageindex
gridbind(e.newpageindex, psql, 20)
end sub