好久没上来了, 难道今天工作时间稍有空闲, 研究了一下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 存储过程 返回结果集 CreationTime--2018年8月14日09点50分 Author:Marydon 1.情景展示 oracle存储过程如何返回结果集 2.解决方案 最简 ...

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

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

  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. cocos2dx 动画 二(iOS)

    7.Bezier曲线 需要ccBezierConfig结构体,设置2个控制点和一个结束点 ccBezierConfig bc; bc.controlPoint_1 = Vec2(,); bc.cont ...

  2. KindleRSS推送服务器搭建

    参考http://xcode.so/2010/12/google-gae-rss-to-kindle/这篇文章 1.首先尝试在本机搭建服务器直接推送到kindle 需要使用到kindlereader这 ...

  3. 第8章BOM笔记

    第八章 BOM 一. Window 在浏览器中window有双重角色,他既是JavaScript访问浏览器窗口的一个借口,又是ECMAscript 规定的Global对象. 1.全局作用域 由于win ...

  4. 16--Box2D使用(二、显示物理世界)

    在上一篇文章中我们创建了的一个物理世界,当物理世界中的刚体一个也没有显示出来.为显示物理世界中的物体,我们需要引入GLES-Render(调试Box2D使用).这两个文件可以再 %Cocos_Home ...

  5. 关于vs2013error C4996: 'strcmpi': The POSIX name for this item is deprecated.的错误解决办法!

    1.出现如下错误(如图1) 2.解决办法(如图2)在头文件处添加#pragma warning(disable: 4996)

  6. QT creator中使用opencv

    最近要用到opencv做图像方面的东西,网上很多是用VS加opencv,但自己对VS不怎么喜欢,想用QT Creator.在网上搜索了很多资料,终于花了一天的时间,在QT Creator上能使用ope ...

  7. java 发送 http 请求

    public class VoteHandler implements IVoteHandler { private static final Logger LOGGER = LoggerFactor ...

  8. 构建WDK驱动出现fatal error U1087: cannot have : and :: dependents for same target

    原因:WDK在编译驱动时,是不允许源文件所在的路径(全路径)中包含空格的,如果你包含了空格,就会出现上述错误. 解决方法:把源文件放在一个没有空格的路径下. reference: http://blo ...

  9. VS调试异常代码 异常:HRESULT: 0x80070057 (E_INVALIDARG) 的处理

    碰到这个异常的原因很偶然: 现象:Solution在ReBuild过程中断电了,来电恢复了,重析编译整个Solution不报错,但在浏览页面时始终无法正常浏览,而在design的视图中,每个aspx的 ...

  10. Polymorphism & Overloading & Overriding

    In Java, a method signature is the method name and the number and type of its parameters. Return typ ...