zoukankan      html  css  js  c++  java
  • 简单搞一下 Oracle 存储过程动态SQL之获取查询分页!

    Oracle 动态SQL之查询分页

    原理:动态组装SQL,利用oracle rownum 虚列嵌套查询实现!

    procedure proc_goods_search_list( p_type number,
    p_keywords
    varchar2,
    p_index
    number,
    p_size
    number,
    p_order
    number,
    p_cursor out p_cursor)
    is
    v_start
    NUMBER(10);
    v_end
    NUMBER(10);
    v_sql
    varchar2(4000);
    begin
    v_start :
    = (p_index -1) * p_size +1;
    v_end :
    = p_index * p_size;

    v_sql :
    =' select a.serialno as goodsId,
    a.goods_name as goodsName,
    a.sale_price as price,
    a.sale_amount as saleAmount,
    a.comment_count as commentCount,
    a.goods_images as goodsImage,
    a.promotion_type as promotionType,
    nvl(a.promotion_price,0) as promotionPrice,
    a.publish_time as createTime,
    a.promotion_start_time as promotionStartTime,
    a.promotion_end_time as promotionEndTime
    from lt_goods a
    where a.status = 2
    ';

    if p_type isnotnulland p_type <>0then
    v_sql :
    = v_sql ||' and a.goods_type ='|| p_type;
    endif;
    if p_keywords isnotnullthen
    v_sql :
    = v_sql ||' and a.goods_name like ''%'|| p_keywords ||'%''';
    endif;

    if p_order isnotnulland p_order =0then
    -- 全部商品
    v_sql := v_sql ||' order by goodsId desc ';
    endif;

    if p_order isnotnulland p_order =1then
    -- 时间
    v_sql := v_sql ||' order by createTime desc ';
    endif;

    if p_order isnotnulland p_order =2then
    -- 销量
    v_sql := v_sql ||' order by saleAmount desc';
    endif;

    if p_order isnotnulland p_order =3then
    -- 价格
    v_sql := v_sql ||' order by price desc';
    endif;
    v_sql :
    ='select m.*,rownum as id from ('|| v_sql ||')m ';
    v_sql :
    ='select * from ('|| v_sql ||') where id <= '||
    to_char(v_end);
    v_sql :
    ='select * from ('|| v_sql ||') where id >= '||
    to_char(v_start);
    /×dbms_output.put_line(v_sql);×/
    open p_cursor for v_sql;

    end proc_goods_search_list;

    查询分页存储过程定义

    procedure proc_goods_search_list( p_type number,
    p_keywords
    varchar2,
    p_index
    number,
    p_size
    number,
    p_order
    number,
    p_cursor out p_cursor)

     参数说明:

      传入参数: 商品类型        p_type,

                     查询关键字     p_keywords,

                     分页索引        p_index,

                     页包含数量     p_size,

                     排序标识        p_order,

      传出参数: 查询游标       p_cursor

    动态SQL局部变量及分页算法

    分页原理,通过利用rownum oracle 虚列标识,通过两层嵌套查询实现

    v_start NUMBER(10);
    v_end
    NUMBER(10);
    v_sql
    varchar2(4000);
     
    v_start := (p_index -1) * p_size +1;
    v_end :
    = p_index * p_size;
    v_sql :='select m.*,rownum as id from ('|| v_sql ||')m ';
    v_sql :
    ='select * from ('|| v_sql ||') where id <= '||
    to_char(v_end);
    v_sql :
    ='select * from ('|| v_sql ||') where id >= '||
    to_char(v_start);

    调试组装SQL

    dbms_output.put_line(v_sql);

    动态执行并输出查询列表

    open p_cursor for v_sql;
    一花一世界 一叶一菩提
  • 相关阅读:
    2.如何安装vmvare tools
    1.如何安装ubuntu
    14.如何读取配置文件的键值对
    android app调试没问题,但打包签名的apk,运行时出现闪退怎么办?
    如何使用jedis进行发布订阅
    如何使用mybatis对mysql数据库进行操作,batis的增删改查
    如何解释json的字符串
    redis可视化工具redisClient
    如何连接远程redis,并且选择某个库进行操作
    理解RESTful架构
  • 原文地址:https://www.cnblogs.com/darjuan/p/oracle_procedure_pager.html
Copyright © 2011-2022 走看看