一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

创建 Oracle 分页存储过程

时间:2022-06-29 10:09:37 编辑:袖梨 来源:一聚教程网

,可以在执行时将其设置为指向不同的结果集。使用 REF CURSOR 输出参数可以将 Oracle 结构化程序的结果集传递回调用应用程序。通过在调用应用程序中定义 OracleType.Cursor 数据类型的输出参数,可以访问 REF CURSOR 所指向的结果集。
create or replace package MF_PAK_001 is
type t_cursor is ref cursor;
procedure GetDataByPage(
p_tableName varchar2,
p_fields varchar2,
p_filter varchar2,
p_sort varchar2,
p_curPage number,
p_pageSize number,
p_cursor out t_cursor,
p_totalRecords out number
);
end MF_PAK_001;
2.创建包体。在包体中实现具体的分页存储过程。
create or replace package body MF_PAK_001 is
procedure GetDataByPage(
p_tableName varchar2,--要查询的表名
p_fields varchar2,--要查询的字段
p_filter varchar2,--过滤条件
p_sort varchar2,--排序字段及方向
p_curPage number,
p_pageSize number,
p_cursor out t_cursor,
p_totalRecords out number
)
is
v_sql varchar2(1000):='';
v_startRecord number(4);
v_endRecord number(4);
begin
--获取总的记录数
v_sql:='select to_number(count(*)) from '||p_tableName;
if p_filter is not null then
v_sql:=v_sql||' where 1=1 and '||p_filter;
end if;
execute immediate v_sql into p_totalRecords;

v_startRecord:=(p_curPage-1)*p_pageSize;
v_endRecord:=p_curPage*p_pageSize;

v_sql:='select '||p_fields||' from (select '||p_fields||',rownum r from '||
'(select '||p_fields||' from '||p_tableName;
if p_filter is not null then
v_sql:=v_sql||' where 1=1 and '||p_filter;
end if;
if p_sort is not null then
v_sql:=v_sql||' order by '||p_sort;
end if;
v_sql:=v_sql||') A where rownum<='||to_char(v_endRecord)||') B where r>='||to_char(v_startRecord);
open p_cursor for v_sql;

end GetDataByPage;
end MF_PAK_001;
3.在Oracle中编写查询语句,执行包体中的分页存储过程,看是否能够正确执行。
declare
v_cur MF_PAK_001.t_cursor;
v_job jobs%rowtype;
v_totalRecords number;
begin
MF_PAK_001.GetDataByPage('jobs','job_id,job_title,min_salary,max_salary','min_salary>0','job_id asc',
  1,10,v_cur,v_totalRecords);
fetch v_cur into v_job;
while v_cur%found loop
dbms_output.put_line(v_job.job_id||','||v_job.job_title);
fetch v_cur into v_job;
end loop;
dbms_output.put_line('总记录数为:'||v_totalRecords);
end;
4.在.NET中调用该分页存储过程。
string connString = "Data Source=ORCL;User Id=hr;Password=Pwd123456";
OracleConnection conn = new OracleConnection(connString);

OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "MF_PAK_001.GetDataByPage";
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("p_tableName", OracleType.VarChar).Value = "jobs";
cmd.Parameters.Add("p_fields", OracleType.VarChar).Value = "job_id,job_title,min_salary,max_salary";
cmd.Parameters.Add("p_filter", OracleType.VarChar).Value = "";
cmd.Parameters.Add("p_sort", OracleType.VarChar).Value = "job_id asc";
cmd.Parameters.Add("p_curPage", OracleType.Number).Value = 1;
cmd.Parameters.Add("p_pageSize", OracleType.Number).Value = 10;
cmd.Parameters.Add("p_cursor", OracleType.Cursor).Direction =ParameterDirection.Output;
cmd.Parameters.Add("p_totalRecords", OracleType.Number).Direction = ParameterDirection.Output;

conn.Open();
OracleDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
for (int i = 0; i < dr.FieldCount; i++)
Response.Write(dr[i].ToString() + ";");
Response.Write("
");
}
conn.Close();

热门栏目