本文转自:http://www.telerik.com/help/openaccess-orm/openaccess-tasks-oracle-execute-sp-result-set.html

In this topic you will learn how to execute Oracle stored procedures that return SYS_REFCURSOR as out parameters. With the REF_CURSOR you can return a recordset/cursor from a stored procedure.

Suppose, you have two tables named Products and Categories with one-to-many relation.

And you have two stored procedures named SPSingleRefCur and SPTwoRefCursor. The first one is a simple stored procedure having an SYS_REFCURSOR out parameter. The procedure returns all category rows via this cursor.

SQL

CREATE OR replace PROCEDURE SPSingleRefCur(catCur OUT SYS_REFCURSOR )
IS
BEGIN
OPEN catCur FOR SELECT * FROM "CATEGORIES";
END; The second procedure have two SYS_REFCURSOR out parameters. The procedure returns all rows from the Categories and Products tables. It will be used to demonstrate how to obtain multiple result sets using the Telerik OpenAccess ADO API. SQL CREATE OR replace PROCEDURE SPTwoRefCursor (catCur OUT SYS_REFCURSOR, prodCur OUT SYS_REFCURSOR )
IS
BEGIN
OPEN catCur FOR SELECT * FROM "CATEGORIES";
OPEN prodCur FOR SELECT * FROM "PRODUCTS";
END; Telerik OpenAccess ORM uses the ADO.NET (Oracle.DataAcces.Client) Provider from Oracle called ODP.NET. In order to create a new domain model based on a Oracle database, you have to install the ODP.NET Driver from Oracle. You can download it from here. Suppose, you have created a domain model that looks like: Also, you need to add a reference to the Oracle.DataAccess.dll assembly. By default the assembly is located in the following directory: C:\app\{UserName}\product\11.2.\client_1\odp.net\bin\\Oracle.DataAccess.dll Executing Stored Procedures There are two ways to execute stored procedures and get the out parameters:
•Use the generic OpenAccessContext.ExecuteQuery<T> method.
•Create a new OACommand and execute the stored procedure using the OACommand.ExecuteReader method. The first approach is easier and involves less code than the second approach. The second approach gives you more control of the result because you can get a DbDataReader from the OACommand. Using the OpenAccessContext.ExecuteQuery<T> Method The first stored procedure (SPSingleRefCur) will be executed by using the generic OpenAccessContext.Execute<T> method. Because you are using an Oracle specific type in the stored procedures, you need to pass an OracleParameter to the context. You need to set the OracleDbType property of the OracleParameter to OracleDbType.RefCursor. Next, when the parameter is set up, you need to call the ExecuteQuery<T> method of the context to get materialized list of Category objects. The following code-snippet demonstrates how to achieve this: C# using System.Collections.Generic;
using System.Data;
using Oracle.DataAccess.Client; namespace OracleSP
{
class Program
{
static void Main(string[] args)
{
using (EntitiesModel ctx = new EntitiesModel())
{
OracleParameter cursorParameter = new OracleParameter();
cursorParameter.ParameterName = "catCur";
cursorParameter.Direction = ParameterDirection.Output;
cursorParameter.OracleDbType = OracleDbType.RefCursor; IList<Category> categories = ctx.ExecuteQuery<Category>("SPSingleRefCur", CommandType.StoredProcedure, cursorParameter);
}
}
}
} VB.NET Module Module1
Sub Main()
Using ctx As New EntitiesModel()
Dim cursorParameter As New Oracle.DataAccess.Client.OracleParameter() cursorParameter.ParameterName = "catCur"
cursorParameter.Direction = ParameterDirection.Output
cursorParameter.OracleDbType = Oracle.DataAccess.Client.OracleDbType.RefCursor Dim categories As IList(Of Category) = ctx.ExecuteQuery(Of Category)("SPSingleRefCur", CommandType.StoredProcedure, cursorParameter)
End Using
End Sub
End Module Although the direction of the parameter is set to ParameterDirection.Output the result set is returned via the reader, not in the Value property of the parameter. This reader is then materialized a list of Category objects. Note that if the specified type is a persistent type (as in this example), then the returned instances are automatically managed by the context. Using the OACommand.ExecuteReader Method If you have a stored procedure that returns two out parameters of type SYS_REFCURSOR (e.g. the SPTwoRefCursor procedure), then using the generic ExecuteQuery<T> method is no longer appropriate. If you are using the ExecuteQuery<T> method, you can not get both result sets materialized. Only the first result set, which is obtained by the underlying reader will be materialized. In this case, you need to use the second approach, i.e. execute the stored procedure by using an OACommand. The second way to execute stored procedures is to use the OACommand class. With this approach, you have the ability to work with the new lower level ADO API. The Telerik OpenAccess ADO API introduces the Translate<T> method, which can be used to materialize a DbDataReader object to persistent capable or non-persistent capable objects. The example here is similar to the first one. What you need is just another parameter, because you need two output parameters for the SPTwoRefCursor procedure. Once the parameters are initialized, you pass them to the OACommand. Next you execute the command by calling the ExecuteReader method to get a data reader. With the reader in hand, you use the generic Translate<T> method on the OpenAccessContext to materialize instances of the Category entity from the reader. By executing the reader.NextResult() method, you switch to the next result set and get the values of the second out parameter. These values could also be materialized with the Translate<T> method but this time you will get objects of type Product. C# using System.Collections.Generic;
using System.Data;
using Oracle.DataAccess.Client;
using Telerik.OpenAccess.Data.Common; namespace OracleSP
{
class Program
{
static void Main(string[] args)
{
using (EntitiesModel ctx = new EntitiesModel())
{
using (OACommand cmd = ctx.Connection.CreateCommand())
{
OracleParameter refCurPar1 = new OracleParameter();
refCurPar1.ParameterName = "catCur";
refCurPar1.Direction = ParameterDirection.Output;
refCurPar1.OracleDbType = OracleDbType.RefCursor; OracleParameter refCurPar2 = new OracleParameter();
refCurPar2.ParameterName = "prodCur";
refCurPar2.Direction = ParameterDirection.Output;
refCurPar2.OracleDbType = OracleDbType.RefCursor; cmd.CommandText = "SPTwoRefCursor";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(new object[] { refCurPar1, refCurPar2 }); using (OADataReader reader = cmd.ExecuteReader())
{
IEnumerable<Category> categories = ctx.Translate<Category>(reader);
reader.NextResult();
IEnumerable<Product> products = ctx.Translate<Product>(reader);
}
}
}
}
}
} VB.NET Module Module1
Sub Main()
Using ctx As New EntitiesModel()
Using cmd As Telerik.OpenAccess.Data.Common.OACommand = ctx.Connection.CreateCommand() Dim refCurPar1 As New Oracle.DataAccess.Client.OracleParameter()
refCurPar1.ParameterName = "catCur"
refCurPar1.Direction = ParameterDirection.Output
refCurPar1.OracleDbType = Oracle.DataAccess.Client.OracleDbType.RefCursor Dim refCurPar2 As New Oracle.DataAccess.Client.OracleParameter()
refCurPar2.ParameterName = "prodCur"
refCurPar2.Direction = ParameterDirection.Output
refCurPar2.OracleDbType = Oracle.DataAccess.Client.OracleDbType.RefCursor cmd.CommandText = "SPTwoRefCursor"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddRange(New Object() {refCurPar1, refCurPar2}) Using reader As Telerik.OpenAccess.Data.Common.OADataReader = cmd.ExecuteReader()
Dim categories As IEnumerable(Of Category) = ctx.Translate(Of Category)(reader)
reader.NextResult()
Dim products As IEnumerable(Of Product) = ctx.Translate(Of Product)(reader)
End Using
End Using
End Using
End Sub
End Module

[转]How to: Execute Oracle Stored Procedures Returning RefCursors的更多相关文章

  1. Spring, Hibernate and Oracle Stored Procedures

    一篇英文博文,写的是利用hibernate处理存储过程中的游标等等: Motivation: While there are a few resources available online for ...

  2. [转]Oracle Stored Procedures Hello World Examples

    本文转自:http://www.mkyong.com/oracle/oracle-stored-procedures-hello-world-examples/ List of quick examp ...

  3. Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python

    f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can fol ...

  4. [MySQL] Stored Procedures 【转载】

    Stored routines (procedures and functions) can be particularly useful in certain situations: When mu ...

  5. Good Practices to Write Stored Procedures in SQL Server

    Reference to: http://www.c-sharpcorner.com/UploadFile/skumaar_mca/good-practices-to-write-the-stored ...

  6. An Introduction to Stored Procedures in MySQL 5

    https://code.tutsplus.com/articles/an-introduction-to-stored-procedures-in-mysql-5--net-17843 MySQL ...

  7. Cursors in MySQL Stored Procedures

    https://www.sitepoint.com/cursors-mysql-stored-procedures/ After my previous article on Stored Proce ...

  8. MySQL Error Handling in Stored Procedures 2

    Summary: this tutorial shows you how to use MySQL handler to handle exceptions or errors encountered ...

  9. Oracle Stored Procedure demo

    1.how to find invalid status stored procedure and recompile them? SELECT OBJECT_NAME , status FROM u ...

随机推荐

  1. [原创]使用命令行工具提升cocos2d-x开发效率(二)之CocosBuilder篇

    如果你正在使用CocosBuilder或者是其他基于CocosBuilder源码改装而成的工具为你的游戏搭建场景或者UI,那你一定要看看这篇文章:)   你是否已经厌倦了无聊的手工publish操作? ...

  2. Codeforces Round #355 (Div. 2)

    A 弯腰 #include<cstdio> #include<cstring> #include<iostream> #include<queue> # ...

  3. grails2.3.11第一课

    以指令的方式Getting Started 1. 创建一个项目 grails create-app HelloGrails 2. 因为我环境变量中配置的jdk是1.8的,所以我要把这个项目搞到IDEA ...

  4. 如何利用多核CPU来加速你的Linux命令

    原文出处: rankfocus   译文出处: 外刊IT评论 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或在里面搜索,或其它操作——一些无法并行的操作.数据专家们,我是在对你们说.你可能 ...

  5. 射频识别技术漫谈(1)——概念、分类

    现代社会智能卡已经渗透到生活的方方面面,公交卡.考勤卡.身份证.手机卡等等数不胜数.    智能卡按使用时是否和读卡器接触可分为接触式智能卡和非接触式智能卡,接触式智能卡上有6-8个触点,使用时插在卡 ...

  6. 单独批次性任务采用MySQL定时器解决需求

    有时候我们在开发的时候会遇到一些需求是在某个固定的时间段实现某些特殊功能,只做一次或者有规律的每分钟一次每小时一次,那么这个时候我们可以启用MySQL的定时器来帮忙解决该问题. 比如,有一个场景是要求 ...

  7. SQL 触发器(学生,课程表,选修表)

    SQL 触发器(学生,课程表,选修表) 触发器是一种特殊类型的存储过程,它不由用户通过命令来执行,而是在用户对表执行了插入,删除或修改表中数据等操作时激活执行.可以这样形容:存储过程像一个遥控炸弹,我 ...

  8. .编写Java应用程序。首先,定义一个Print类,它有一个方法void output(int x),如果x的值是1,在控制台打印出大写的英文字母表;如果x的值是2,在 控制台打印出小写的英文字母表。其次,再定义一个主类——TestClass,在主类 的main方法中创建Print类的对象,使用这个对象调用方法output ()来打印出大 小写英文字母表。

    package com.homework.zw; //类Print部分 public class Print1 { int x; void output() { if(x==1) { for(int ...

  9. in和exists的区别与SQL执行效率

    in和exists的区别与SQL执行效率最近很多论坛又开始讨论in和exists的区别与SQL执行效率的问题,本文特整理一些in和exists的区别与SQL执行效率分析 SQL中in可以分为三类: 1 ...

  10. JavaScript Source Map 详解

    源码地址: http://www.ruanyifeng.com/blog/2013/01/javascript_source_map.html 上周,jQuery 1.9发布. 这是2.0版之前的最后 ...