好久没上来了, 难道今天工作时间稍有空闲, 研究了一下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. oracle数据库实验讲义-读书笔记(一)

    1.激活锁定的用户alter user scott account unlock identified by tiger;2.使用内含脚本建立scott用户@%oracle_home%\rdbms\a ...

  2. Hashtable键值集合

    //Hashtable键值集合 键必须是维一的 类似于索引 Hashtable ht = new Hashtable(); ht.Add(, "中国"); ht.Add(, ); ...

  3. var foo= {} ;foo.method() 和 单例模式有什么区别

    var Foo={} Foo.method = function() { var i = 0; return { a:function(){ console.log('aa', i++); }, b: ...

  4. linux挂载查看、添加与取消

    挂载概念: 查看挂载:df 添加挂载mount:mount 挂载的源 目的点 mount /dev/sdb1 /mnt mount挂载常用参数(Option) -t 指定文件系统类型,例如:-t ex ...

  5. 4月13日 php

    用php打印乘法表: <html> <head> <title>我的第一个PHP页面</title> </head> <body> ...

  6. Linux下查看Apache的请求数

    在Linux下查看Apache的负载情况,以前也说过,最简单有有效的方式就是查看Apache Server Status(如何开启Apache Server Status点这里),在没有开启Apach ...

  7. QQ在线客服

    css代码: .float0831 { POSITION: fixed; TOP: 180px; RIGHT: 1px; _position: absolute } .float0831 A { CO ...

  8. SQL Fetch size

    JDBC performance tuning with optimal fetch size February 1, 2009 31 Comments Tuning performance usin ...

  9. 超实用--删除MYSQL中指定的数据的全部表

    作过的人都知道,重复测试数据库的苦恼. 用法:# Usage: ./script user password dbnane mysql.nixcraft.in ~~~~~~~~~~~~~ #!/bin ...

  10. 在O(1) 时间删除链表节点

    struct Node { int val; Node * next; }; void deleteNode(Node ** head, Node * target) { assert(head != ...