Using a ref cursor in Oracle PL/SQL(转)

CREATE OR REPLACE PACKAGE GetRefCursors IS

-- ***********************************************************************
-- ** Author: Alf A. Pedersen www.databasedesign-resource.com
-- ** Version: : Apr. 10 2006
-- **
-- ***********************************************************************
-- ** General global cursor for all functions returning result sets.

TYPE csGetResultSet is REF CURSOR;

-- ***********************************************************************
-- ** Get all accounts for a given interval
-- ** In parameters:
-- ** First account
-- ** Last account
-- ** Returns:
-- ** Ref Cursor for the given account interval.
-- ***********************************************************************

function sfGetAccountInterval
( pFirstAccount in ACCOUNTS.ACCOUNT_NO%type
,pLastAccount in ACCOUNTS.ACCOUNT_NO%type)
return csGetResultSet;

end GetRefCursors;
/

CREATE OR REPLACE package body GetRefCursors is
-- ***********************************************************************
-- ** Author: Alf A. Pedersen www.databasedesign-resource.com
-- ** Version: : Apr. 10 2006
-- **
-- ***********************************************************************

-- ***********************************************************************
-- ** Get all accounts for a given interval
-- ** In parameters:
-- ** First account
-- ** Last account
-- ** Returns:
-- ** Ref Cursor for the given account interval.
-- ***********************************************************************

function sfGetAccountInterval
( pFirstAccount in ACCOUNTS.ACCOUNT_NO%type
,pLastAccount in ACCOUNTS.ACCOUNT_NO%type)
return csGetResultSet is

csGetAccounts csGetResultSet;

begin

open csGetAccounts for

SELECT accounts.account_no,accounts.name
FROM accounts
WHERE accounts.account_no BETWEEN pFirstAccount AND pLastAccount
ORDER BY accounts.account_no;

return csGetAccounts;

end sfGetAccountInterval;

end GetRefCursors;
/

The global cursour csGetResultSet has to be specified in the Package Specification, but is redirected to the local cursor csGetAccounts so it can be returned to the calling module from within that function.

This can be used as a template to build a framework for handling all SELECT access to your tables through such a mechanism. This way you will deliver a generic and consistent interface for all applications and/or modules that need to access data within your different tables.

Not to mention: The DBA can easily tune the various SELECT statements without even touching the application(s), no matter how many different modules that are accessing a given table.

Time saved, and secure, identical and (hopefully) error-free code is delivered to anyone from just one place.

If you need to learn how you can use a ref cursor in Oracle using Java, read here: Oracle ref cursor in Java

Other important resources on this site:

Oracle DBA
SQL Server DBA
Application Architecture
Software tools
Programming tips
Database fundamentals

Return from Ref cursor to Programming

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值