12
返回列表 发新帖
楼主: xpxia

如何进行分批输出数据(非常急!!!)

[复制链接]
论坛徽章:
1
优秀写手
日期:2014-07-22 06:00:13
11#
发表于 2007-1-26 14:08 | 只看该作者
create or replace procedure P_QUERY_RESULT_BY_PAGE3(p_PageSize int,        --每页记录数
                  p_PageNo int,          --当前页码,从 1 开始
                  p_SqlSelect varchar2,  --查询语句,含排序部分
                  p_OutCursor out sys_refcursor)
as
    v_sql varchar2(3000);
    --v_count int;
    v_heiRownum int;
    v_lowRownum int;
begin
/*
  ----取记录总数
  v_sql := 'select count(*) from (' || p_SqlSelect || ')';
  execute immediate v_sql into v_count;
  p_OutRecordCount := v_count;
*/
  ----执行分页查询
  v_heiRownum := p_PageNo * p_PageSize;
  v_lowRownum := v_heiRownum - p_PageSize + 1;

  v_sql := 'SELECT *
            FROM (
                  SELECT A.*, rownum rn
                  FROM  ('|| p_SqlSelect ||') A
                  WHERE rownum <= '|| to_char(v_heiRownum) || '
                 ) B
            WHERE rn >= ' || to_char(v_lowRownum) ;
            --注意对rownum别名的使用,第一次直接用rownum,第二次一定要用别名rn
  
  OPEN p_OutCursor FOR  v_sql;


end P_QUERY_RESULT_BY_PAGE3;

使用道具 举报

回复
论坛徽章:
57
马上加薪
日期:2014-02-19 11:55:142011新春纪念徽章
日期:2011-01-25 15:42:562011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:41:01ITPUB9周年纪念徽章
日期:2010-10-08 09:28:522010系统架构师大会纪念
日期:2010-09-03 16:39:572010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010新春纪念徽章
日期:2010-03-01 11:21:02
12#
发表于 2007-1-26 14:32 | 只看该作者
最初由 冷月宫主 发布
[B]create or replace procedure P_QUERY_RESULT_BY_PAGE3(p_PageSize int,        --每页记录数
                  p_PageNo int,          --当前页码,从 1 开始
                  p_SqlSelect varchar2,  --查询语句,含排序部分
                  p_OutCursor out sys_refcursor)
as
    v_sql varchar2(3000);
    --v_count int;
    v_heiRownum int;
    v_lowRownum int;
begin
/*
  ----取记录总数
  v_sql := 'select count(*) from (' || p_SqlSelect || ')';
  execute immediate v_sql into v_count;
  p_OutRecordCount := v_count;
*/
  ----执行分页查询
  v_heiRownum := p_PageNo * p_PageSize;
  v_lowRownum := v_heiRownum - p_PageSize + 1;

  v_sql := 'SELECT *
            FROM (
                  SELECT A.*, rownum rn
                  FROM  ('|| p_SqlSelect ||') A
                  WHERE rownum <= '|| to_char(v_heiRownum) || '
                 ) B
            WHERE rn >= ' || to_char(v_lowRownum) ;
            --注意对rownum别名的使用,第一次直接用rownum,第二次一定要用别名rn
  
  OPEN p_OutCursor FOR  v_sql;


end P_QUERY_RESULT_BY_PAGE3; [/B]


要注意绑定变量的使用!

create or replace package pkg_rome_online_times as
--************************************************
-- ** Get connect time information of site.
-- ** In parameters: S_RQ
-- ** In parameters: E_RQ
-- ** In parameters: RDC_ID
-- ** In parameters: BRANCH_ID
-- ** In parameters: SITE_NO
-- ** In parameters: PAGE_NO
-- ** Returns:Ref Cursor
-- ** p_online_times_site:Ref Cursor(rq,weight)
-- ** p_online_times_branch:Ref Cursor(site_no,weight)
-- ** p_online_times_rdc:Ref Cursor(site_no,weight)
-- ************************************************
type t_ref is ref cursor;
procedure p_online_times_site(s_rq in date,e_rq in date,rdcid in varchar2,siteno in varchar2,page_no in number,num out number,cur_ref out t_ref);
procedure p_online_times_branch(s_rq in date,e_rq in date,rdcid in varchar2,branchid in varchar2,page_no in number,num out number,weight out varchar2,cur_ref out t_ref);
procedure p_online_times_rdc(s_rq in date,e_rq in date,rdcid in varchar2,page_no in number,weight out varchar2,cur_ref out t_ref);
end pkg_rome_online_times;
/

create or replace package body pkg_rome_online_times as
procedure p_online_times_site(s_rq in date,e_rq in date,rdcid in varchar2,siteno in varchar2,page_no in number,num out number,cur_ref out t_ref) is
begin               
        if page_no is null or page_no<0 then
                return;
        end if;
        select count(*) into num from site_connect_summary
                        where rq >= s_rq
                        and rq <= e_rq
                        and rdc_id = rdcid
                        and site_no = siteno;
        if page_no = 0 then
                open cur_ref for 'select to_char(rq,''yyyy-mm-dd'') rq,trunc(weight/60)||''小时''||lpad(mod(weight,60),2,''0'')||''分'' weight
                        from site_connect_summary
                        where rq >= :s_rq
                        and rq <= :e_rq
                        and rdc_id = :rdcid
                        and site_no = :siteno
                        order by rq' using s_rq,e_rq,rdcid,siteno;
        else
                open cur_ref for 'select /*+ FIRST_ROWS */ to_char(rq,''yyyy-mm-dd'') rq,trunc(weight/60)||''小时''||lpad(mod(weight,60),2,''0'')||''分'' weight
                        from (select rownum rn,rq,weight from (select rq,weight from site_connect_summary
                        where rq >= :s_rq
                        and rq <= :e_rq
                        and rdc_id = :rdc_id
                        and site_no = :site_no
                        order by rq)
                        where rownum <= :row_e)
                        where rn >= :row_s'
                        using s_rq,e_rq,rdcid,siteno,page_no*fn_get_system_config('ROWS_PER_PAGE'),(page_no - 1)*fn_get_system_config('ROWS_PER_PAGE')+1;
        end if;
exception when others then
        null;
end p_online_times_site;
procedure p_online_times_branch(s_rq in date,e_rq in date,rdcid in varchar2,branchid in varchar2,page_no in number,num out number,weight out varchar2,cur_ref out t_ref) is
begin               
        if page_no is null or page_no<0 then
                return;
        end if;
        select count(distinct a.site_no),trunc(sum(a.weight)/60)||'小时'||lpad(mod(sum(a.weight),60),2,'0')||'分'  into num,weight
                        from site_connect_summary a,site b
                        where a.rq >= s_rq
                        and a.rq <= e_rq
                        and a.rdc_id = rdcid
                        and a.rdc_id = b.rdc_id
                        and a.site_no = b.site_no
                        and b.branch_id = branchid;
        if page_no = 0 then
                open cur_ref for 'select a.site_no site_no,trunc(sum(a.weight)/60)||''小时''||lpad(mod(sum(a.weight),60),2,''0'')||''分'' weight
                        from site_connect_summary a,site b
                        where a.rq >= :s_rq
                        and a.rq <= :e_rq
                        and a.rdc_id = :rdcid
                        and a.rdc_id = b.rdc_id
                        and a.site_no = b.site_no
                        and b.branch_id = :branchid
                        group by a.site_no
                        order by a.site_no' using s_rq,e_rq,rdcid,branchid;
        else
                open cur_ref for 'select /*+ FIRST_ROWS */ site_no,trunc(weight/60)||''小时''||lpad(mod(weight,60),2,''0'')||''分'' weight  
                        from(select rownum rn,site_no,weight from(select a.site_no,sum(a.weight) weight from site_connect_summary a,site b
                        where a.rq >= :s_rq
                        and a.rq <= :e_rq
                        and a.rdc_id = :rdcid
                        and a.rdc_id = b.rdc_id
                        and a.site_no = b.site_no
                        and b.branch_id = :branchid
                        group by a.site_no
                        order by a.site_no)
                        where rownum <= :row_e)
                        where rn >= :row_s'
                        using s_rq,e_rq,rdcid,branchid,page_no*fn_get_system_config('ROWS_PER_PAGE'),(page_no - 1)*fn_get_system_config('ROWS_PER_PAGE')+1;
        end if;
exception when others then
        null;
end p_online_times_branch;
procedure p_online_times_rdc(s_rq in date,e_rq in date,rdcid in varchar2,page_no in number,weight out varchar2,cur_ref out t_ref) is
begin
        select trunc(sum(weight)/60)||'小时'||lpad(mod(sum(weight),60),2,'0')||'分' into weight
                        from site_connect_summary a,site b,branch c
                        where a.rq >= s_rq
                        and a.rq <= e_rq
                        and a.rdc_id = rdcid
                        and a.rdc_id = b.rdc_id
                        and a.site_no = b.site_no
                        and b.branch_id = c.branch_id;
        open cur_ref for 'select b.branch_id,c.branch_no,c.branch_name,trunc(sum(weight)/60)||''小时''||lpad(mod(sum(weight),60),2,''0'')||''分'' weight
                        from site_connect_summary a,site b,branch c
                        where a.rq >= :s_rq
                        and a.rq <= :e_rq
                        and a.rdc_id = :rdcid
                        and a.rdc_id = b.rdc_id
                        and a.site_no = b.site_no
                        and b.branch_id = c.branch_id
                        group by b.branch_id,c.branch_no,c.branch_name
                        order by c.branch_no' using s_rq,e_rq,rdcid;
        --if page_no = 0 then
        --        open cur_ref for 'select site_no,trunc(sum(weight)/60)||''小时''||lpad(mod(sum(weight),60),2,''0'')||''分'' weight
        --                from site_connect_summary
        --                where rq >= :s_rq
        --                and rq <= :e_rq
        --                and rdc_id = :rdc_id
        --                group by site_no
        --                order by site_no' using s_rq,e_rq,rdc_id;
        --else
        --        open cur_ref for 'select /*+ FIRST_ROWS */ site_no,trunc(weight/60)||''小时''||lpad(mod(weight,60),2,''0'')||''分'' weight
        --                from(select rownum rn,site_no,weight from(select site_no,sum(weight) weight from site_connect_summary
        --                where rq >= :s_rq
        --                and rq <= :e_rq
        --                and rdc_id = :rdc_id
        --                group by site_no
        --                order by site_no)
        --                where rownum <= :row_e)
        --                where rn >= :row_s'
        --                using s_rq,e_rq,rdc_id,page_no*fn_get_system_config('ROWS_PER_PAGE'),(page_no - 1)*fn_get_system_config('ROWS_PER_PAGE')+1;
        --end if;
exception when others then
        null;
end p_online_times_rdc;
end pkg_rome_online_times;
/

使用道具 举报

回复
论坛徽章:
16
ITPUB元老
日期:2006-12-29 17:11:00秀才
日期:2015-12-25 15:31:102015年新春福章
日期:2015-03-06 11:57:31沸羊羊
日期:2015-03-04 14:43:432012新春纪念徽章
日期:2012-01-04 11:49:54ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412010新春纪念徽章
日期:2010-03-01 11:21:02祖国60周年纪念徽章
日期:2009-10-09 08:28:002009新春纪念徽章
日期:2009-01-04 14:52:28生肖徽章2007版:鼠
日期:2008-01-02 17:35:53
13#
发表于 2007-1-26 17:06 | 只看该作者

Re: 如何进行分批输出数据(非常急!!!)

最初由 xpxia 发布
[B]如何进行分批输出数据?我现在从数据库中查询出来一批数据,比如有10000条,现在我想300条300条地往外输出,使用SQL语句该怎么来实现???

非常着急,在线等候!!!!
请大师们帮忙!!! [/B]


要输出为什么格式?文本文件,还是直接通过其他语言处理?

使用道具 举报

回复
论坛徽章:
24
生肖徽章:狗
日期:2006-09-07 10:14:43数据库板块每日发贴之星
日期:2008-07-26 01:02:20生肖徽章2007版:兔
日期:2008-10-13 11:10:11奥运会纪念徽章:铁人三项
日期:2008-10-24 13:27:21开发板块每日发贴之星
日期:2008-12-27 01:01:09生肖徽章2007版:马
日期:2009-11-18 10:45:032010新春纪念徽章
日期:2010-03-01 11:21:02ITPUB9周年纪念徽章
日期:2010-10-08 09:28:51ERP板块每日发贴之星
日期:2011-05-18 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:21:15
14#
发表于 2007-1-28 13:48 | 只看该作者
LZ不是都说显示在网页上么,肯定是前台程序用了,应该是返回cursor的那种!

学习了分页查询了,哈哈!

使用道具 举报

回复
论坛徽章:
24
生肖徽章:狗
日期:2006-09-07 10:14:43数据库板块每日发贴之星
日期:2008-07-26 01:02:20生肖徽章2007版:兔
日期:2008-10-13 11:10:11奥运会纪念徽章:铁人三项
日期:2008-10-24 13:27:21开发板块每日发贴之星
日期:2008-12-27 01:01:09生肖徽章2007版:马
日期:2009-11-18 10:45:032010新春纪念徽章
日期:2010-03-01 11:21:02ITPUB9周年纪念徽章
日期:2010-10-08 09:28:51ERP板块每日发贴之星
日期:2011-05-18 01:01:01ITPUB十周年纪念徽章
日期:2011-11-01 16:21:15
15#
发表于 2007-1-28 13:53 | 只看该作者
最初由 冷月宫主 发布
[B]create or replace procedure P_QUERY_RESULT_BY_PAGE3(p_PageSize int,        --每页记录数
                  p_PageNo int,          --当前页码,从 1 开始
                  p_SqlSelect varchar2,  --查询语句,含排序部分
                  p_OutCursor out sys_refcursor)
as
    v_sql varchar2(3000);
    --v_count int;
    v_heiRownum int;
    v_lowRownum int;
begin
/*
  ----取记录总数
  v_sql := 'select count(*) from (' || p_SqlSelect || ')';
  execute immediate v_sql into v_count;
  p_OutRecordCount := v_count;
*/
  ----执行分页查询
  v_heiRownum := p_PageNo * p_PageSize;
  v_lowRownum := v_heiRownum - p_PageSize + 1;

  v_sql := 'SELECT *
            FROM (
                  SELECT A.*, rownum rn
                  FROM  ('|| p_SqlSelect ||') A
                  WHERE rownum <= '|| to_char(v_heiRownum) || '
                 ) B
            WHERE rn >= ' || to_char(v_lowRownum) ;
            --注意对rownum别名的使用,第一次直接用rownum,第二次一定要用别名rn
  
  OPEN p_OutCursor FOR  v_sql;


end P_QUERY_RESULT_BY_PAGE3; [/B]


为什么要to_char(v_heiRownum)呢,rownum就是number的吧。

还是帮定变量好些!

----执行分页查询
v_heiRownum := p_PageNo * p_PageSize;
v_lowRownum := v_heiRownum - p_PageSize + 1;

v_sql := 'SELECT *
FROM (
SELECT A.*, rownum rn
FROM ('|| p_SqlSelect ||') A
WHERE rownum <= :1
) B
WHERE rn >= :2 ' ;
--注意对rownum别名的使用,第一次直接用rownum,第二次一定要用别名rn

OPEN p_OutCursor FOR v_sql Using v_heiRownum,v_lowRownum;

使用道具 举报

回复
论坛徽章:
1
大众
日期:2013-11-06 13:26:53
16#
发表于 2017-1-5 00:02 | 只看该作者
呵呵,实际上就这两种就基本上够用了。如果碰到大型数据,应该考虑使用存储过程或函数来实现,返回给客户端才行的

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表