好久没上来了, 难道今天工作时间稍有空闲, 研究了一下oracle存储过程返回结果集.

配合oracle临时表, 使用存储过程来返回结果集的数据读取方式可以解决海量数据表与其他表的连接问题. 在存储过程中先根据过滤条件从海量数据表中选出符合条件的记录并存放到临时中, 可以通过一个视图将临时表与其他相关表连接起来, 从而避免海量数据造成的连接效率问题.

本文只讨论使用存储过程返回结果集.

具体实现如下:

-- 启用服务器输出
---------------------
set serveroutput on

-- 创建测试表
---------------------
create table test_pkg_test
(
 id number(10) constraint pk_test_pkg_test primary key,
 name varchar2(30)
);

-- 写入测试数据
---------------------
begin
insert into test_pkg_test(id) values(1);
insert into test_pkg_test(id) values(2);
insert into test_pkg_test(id) values(3);
insert into test_pkg_test(id) values(4);
insert into test_pkg_test(id) values(5);
insert into test_pkg_test(id) values(6);
insert into test_pkg_test(id) values(7);
insert into test_pkg_test(id) values(8);
insert into test_pkg_test(id) values(9);
insert into test_pkg_test(id) values(10);
insert into test_pkg_test(id) values(11);
insert into test_pkg_test(id) values(12);
insert into test_pkg_test(id) values(13);
insert into test_pkg_test(id) values(14);
insert into test_pkg_test(id) values(15);
insert into test_pkg_test(id) values(16);
insert into test_pkg_test(id) values(17);
insert into test_pkg_test(id) values(18);
end;
/
update test_pkg_test set name='name of ' || to_char(id);
commit;

-- 声明程序包
---------------------
create or replace package pkg_test
as
 type  type_cursor is ref cursor;
 procedure read_rows (header varchar2, result out type_cursor);
end pkg_test;
/

-- 实现程序包
---------------------
create or replace package body pkg_test
as
 procedure read_rows (header varchar2, result out type_cursor)
 is
  sqlText varchar2(500);
 begin
  if header is null or length(header)=0 then
   sqlText := 'select * from test_pkg_test';
  else
   sqlText := 'select * from test_pkg_test where substr(name,1,' || to_char(length(header)) || ')=''' || header || '''';
  end if;
  --dbms_output.put_line(sqlText);
  open result for sqlText;
 end read_rows;
end pkg_test;
/

-- 在 sqlplus 中测试
---------------------
var result refcursor
exec pkg_test.read_rows(null,:result);
print result
exec pkg_test.read_rows('name of 1', :result);
print result;

-- 在程序中测试(c#.Net)
-- ***************************************
    static class pkg_test
    {
        public static void Test()
        {
            using (OracleConnection conn = new OracleConnection())
            {
                conn.ConnectionString = "Data Source=mydb;User Id=myuser;Password=mypassword";
                conn.Open();

using (OracleCommand cmd = new OracleCommand("pkg_test.read_rows", conn))
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    OracleParameter p = new OracleParameter("header", OracleType.VarChar);
                    p.Value = "name of 1";
                    //p.Value = DBNull.Value;
                    cmd.Parameters.Add(p);

p = new OracleParameter("result", OracleType.Cursor);
                    p.Direction = System.Data.ParameterDirection.Output;
                    cmd.Parameters.Add(p);

OracleDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Console.WriteLine("{0}\t{1}", reader.GetValue(0), reader.GetValue(1));
                    }
                }
            }
        }

-- ***************************************

-- 删除程序包和测试表
---------------------
drop package pkg_test;
drop table test_pkg_test;

oracle 存储过程返回结果集的更多相关文章

  1. oracle 存储过程返回结果集 (转载)

    好久没上来了, 难道今天工作时间稍有空闲, 研究了一下oracle存储过程返回结果集. 配合oracle临时表, 使用存储过程来返回结果集的数据读取方式可以解决海量数据表与其他表的连接问题. 在存储过 ...

  2. oracle 存储过程 返回结果集

      oracle 存储过程 返回结果集 CreationTime--2018年8月14日09点50分 Author:Marydon 1.情景展示 oracle存储过程如何返回结果集 2.解决方案 最简 ...

  3. 160307、Java调用Oracle存储过程返回结果集

    一:无返回值的存储过程调用 存储过程: CREATE OR REPLACE PROCEDURE PRO_1(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2)   AS BEGI ...

  4. oracle存储过程返回结果集

    http://www.2cto.com/database/201204/127180.html oracle实现存储过程返回查询结果集合的方法   --实现存储过程返回查询结果集合的方法 ,以下代码来 ...

  5. C#中使用Oracle存储过程返回结果集

    问题: 在MSSQLServer中定义的存储过程可以直接返回一个数据集,如: create procedure sp_getAllEmployees as SELECT * FROM [NORTHWN ...

  6. PB中用oracle的存储过程返回记录集做数据源来生成数据窗口,PB会找不到此存储过程及不能正常识别存储过程的参数问题(转)

    (转)在PB中用oracle的存储过程返回记录集做数据源来生成数据窗口 首先oracle的存储过程写法与MSSQL不一样,差别比较大. 如果是返回数据集的存储过程则需要利用oracle的包来定义游标. ...

  7. 123 c#调用oracle存储过程返回数据集 --通过oracle存储过程返回数据集

    c#调用oracle存储过程返回数据集 2008-12-20 10:59:57|  分类: net|字号 订阅   CREATE OR REPLACE PACKAGE pkg_tableTypeIS  ...

  8. c#调用oracle存储过程返回数据集

    c#调用oracle存储过程返回数据集 2008-12-20 10:59:57|  分类: net|字号 订阅   CREATE OR REPLACE PACKAGE pkg_tableTypeIS  ...

  9. PostgreSQL 调用存储过程返回结果集

    创建返回结果集类型的存储过程: CREATE OR REPLACE FUNCTION public.f_get_member_info( id integer, productname charact ...

随机推荐

  1. 自定义规则,对List<Map<String,Object>> List<Object>进行排序

    package lltse.java.collection; import java.util.ArrayList; import java.util.Collections; import java ...

  2. JS和JSP的差别

    近期非常多同学在纠结于名词缩写之间的相似性.因此本人也来写一篇,讲讲JS和JSP的差别. SUN首先发展出SERVLET,其功能比較强劲,体系设计也非常先进,仅仅是,它输出HTML语句还是採用了老的C ...

  3. ajax异步请求,session失效处理

    后台拦截器代码: // 判断是否是AJAX请求 if (isAjaxRequest(request)) { log.info("AjaxRequest请求"); ActionCon ...

  4. C++代码优化方法总结

    C++代码优化方法总结 优化是一个非常大的主题,本文并不是去深入探讨性能分析理论,算法的效率,况且我也没有这个能力.我只是想把一些可以简单的应用到你的C++代码中 的优化技术总结在这里,这样,当你遇到 ...

  5. android:scaleType=&quot;matrix&quot;布局文件载入图片时候的显示方式

    android:scaleType="center" 以原图的几何中心点和ImagView的几何中心点为基准,按图片的原来size居中显示,不缩放,当图片长/宽超过View的长/宽 ...

  6. Atitit. 解决unterminated string literal 缺失引号

    Atitit. 解决unterminated string literal 缺失引号 原因:::或许string没使用引号括号起来...missingMessage缺失了一个单个的引号 Error:  ...

  7. [svc]ssh生成key不交互

    ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" 首次执行不交互 第二次再次执行会让输入y

  8. PKI/CA

    PKI( Public Key Infrastructure )指的是公钥基础设施. CA ( Certificate Authority )指的是认证中心. PKI从技术上解决了网络通信安全的种种障 ...

  9. 华中农业大学校赛--c The Same Color

    Problem C: The Same Color Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 993  Solved: 595[Submit][St ...

  10. 一款基于jQuery的全屏广告图片焦点图

    之前为大家分享了很多jQuery焦点图插件.今天我们要介绍的这款jQuery全屏广告图片焦点图插件也非常不错,图片切换时有淡出淡出的动画效果,并且也相当流畅.效果图如下: 在线预览   源码下载 实现 ...