Oracle 10g Release2新功能之Ref Cursor
欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入 Ref Cursor就是我们定义在服务器端的结果集的reference。 当我们打开一个Ref Cursor的时候,没有任何的数据返回到客户端,相反,数据在服务器上的地址将会被返回到客户端。这样用户就可以自己决定什
欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入
Ref Cursor就是我们定义在服务器端的结果集的reference。 当我们打开一个Ref Cursor的时候,没有任何的数据返回到客户端,相反,数据在服务器上的地址将会被返回到客户端。这样用户就可以自己决定什么时间和以那种方式通过Ref Cursor去取数据。
在以前版本的ODP.NET中,我们可以通过Ref Cursor取数据,但是我们不能把Ref Cursor作为一个Input参数传递给PL/SQL的存储过程和存储函数。但是在Oracle Database 10g Release2,我们能够很简单的把Ref Cursor作为Input参数传递给PL/SQL的存储过程和存储函数。这是Oracle Database 10g Release2的新功能。
我们接下来就以例程的方式来向你介绍这个新功能。
准备数据库
我们要在数据库中生成一个表和一个包,我们接下来的例子会用到。
请用HR用户登录数据库,然后运行下面的脚本。
create table processing_result
(
status varchar2(64)
);
create or replace package cursor_in_out as
type emp_cur_type is ref cursor return employees%rowtype;
procedure process_cursor(p_cursor in emp_cur_type);
end;
/
create or replace package body cursor_in_out as
procedure process_cursor(p_cursor in emp_cur_type) is
employee employees%rowtype;
begin
loop
fetch p_cursor into employee;
exit when p_cursor%notfound;
insert into processing_result
values('Processed employee #' ||
employee.employee_id || ': ' ||
employee.first_name || ' ' ||
employee.last_name);
end loop;
end;
end;
/
创建.NET代码
数据库已经准备好了,接下来我们就准备创建.NET代码。
using System;
using System.Data;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
namespace CursorInCursorOut
{
///
/// Summary description for Class1.
///
class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
// create connection to database
// change for your environment
string constr = "User Id=hr; Password=hr; Data Source=oramag; Pooling=false";
OracleConnection con = new OracleConnection(constr);
con.Open();
// command and parameter objects to get ref cursor
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "begin open :1 for select * from employees where manager_id=101; end;";
OracleParameter p_rc = cmd.Parameters.Add("p_rc", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
// get the ref cursor
cmd.ExecuteNonQuery();
// clear parameters to reuse
cmd.Parameters.Clear();
// command and parameter objects to pass ref cursor
// as an input parameter
cmd.CommandText = "cursor_in_out.process_cursor";
cmd.CommandType = CommandType.StoredProcedure;
OracleParameter p_input = cmd.Parameters.Add("p_input", OracleDbType.RefCursor, p_rc.Value, ParameterDirection.Input);
// process the input cursor
cmd.ExecuteNonQuery();
// clean up objects
p_input.Dispose();
p_rc.Dispose();
cmd.Dispose();
con.Dispose();
}
}
}
运行上面的代码,这个程序本身没有输出,但是我们可以通过SQL*PLUS很容易可以看到下面的输出。
SQL> select * from processing_result;
STATUS
----------------------------------------
Processed employee #108: Nancy Greenberg
Processed employee #200: Jennifer Whalen
Processed employee #203: Susan Mavris
Processed employee #204: Hermann Baer
Processed employee #205: Shelley Higgins
5 rows selected.
我这里只是给大家一个很简单的例子,希望大家充分应用Oracle Database的新特性,使你的项目更加的稳定,效率更高。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Early this morning, Apple officially released iPadOS18. This system not only has the classic functions of iOS18, but also adds some unique functions, such as supporting mathematical note calculators, etc., which further improves the experience of iPad users. Friends who are interested Come and take a look. This time iPadOS18 not only perfectly inherits the core functions of iOS18, such as the personalized control center design, which allows users to freely adjust the order and layout of control items according to personal preferences, and the highly anticipated game mode, providing gamers with smoother and more The immersive gaming experience also incorporates a number of unique features specifically targeting the iPad’s large screen advantages and the creative uses of Apple Pencil, further expanding the iPad’s productivity.

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

The function in Oracle to calculate the number of days between two dates is DATEDIFF(). The specific usage is as follows: Specify the time interval unit: interval (such as day, month, year) Specify two date values: date1 and date2DATEDIFF(interval, date1, date2) Return the difference in days

The Oracle database startup sequence is: 1. Check the preconditions; 2. Start the listener; 3. Start the database instance; 4. Wait for the database to open; 5. Connect to the database; 6. Verify the database status; 7. Enable the service (if necessary ); 8. Test the connection.

The INTERVAL data type in Oracle is used to represent time intervals. The syntax is INTERVAL <precision> <unit>. You can use addition, subtraction, multiplication and division operations to operate INTERVAL, which is suitable for scenarios such as storing time data and calculating date differences.

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.

To find the number of occurrences of a character in Oracle, perform the following steps: Get the total length of a string; Get the length of the substring in which a character occurs; Count the number of occurrences of a character by subtracting the substring length from the total length.

The method of replacing strings in Oracle is to use the REPLACE function. The syntax of this function is: REPLACE(string, search_string, replace_string). Usage steps: 1. Identify the substring to be replaced; 2. Determine the new string to replace the substring; 3. Use the REPLACE function to replace. Advanced usage includes: multiple replacements, case sensitivity, special character replacement, etc.
