本文章来介绍了关于在oracle中REF Cursor用法,有需要的朋友可以参考一下下。
本文章来介绍了关于在oracle中REF Cursor用法,有需要的朋友可以参考一下下。1,什么是 REF游标 ?
动态关联结果集的临时对象。即在运行的时候动态决定执行查询。
2,REF 游标 有什么作用?
实现在程序间传递结果集的功能,利用REF CURSOR也可以实现BULK SQL,从而提高SQL性能。
3,静态游标和REF 游标的区别是什么?
①静态游标是静态定义,REF 游标是动态关联;
②使用REF 游标需REF 游标变量。
③REF 游标能做为参数进行传递,而静态游标是不可能的。
4,什么是REF 游标变量?
REF游标变量是一种 引用 REF游标类型 的变量,指向动态关联的结果集。
5,怎么使用 REF游标 ?
①声明REF 游标类型,确定REF 游标类型;
⑴强类型REF游标:指定retrun type,REF 游标变量的类型必须和return type一致。
语法:Type REF游标名 IS Ref Cursor Return 结果集返回记录类型;
⑵弱类型REF游标:不指定return type,能和任何类型的CURSOR变量匹配,用于获取任何结果集。
语法:Type REF游标名 IS Ref Cursor;
②声明Ref 游标类型变量;
语法:变量名 已声明Ref 游标类型;
③打开REF游标,关联结果集 ;
语法:Open Ref 游标类型变量 For 查询语句返回结果集;
④获取记录,操作记录;
语法:Fatch REF游标名 InTo 临时记录类型变量或属性类型变量列表;
⑤关闭游标,完全释放资源;
语法:Close REF游标名;
例子:强类型REF游标
代码如下 | 复制代码 |
/*conn scott/tiger*/ Declare Type MyRefCurA IS REF CURSOR RETURN emp%RowType; Type MyRefCurB IS REF CURSOR RETURN emp.ename%Type; vRefCurA MyRefCurA; vRefCurB MyRefCurB; vTempA vRefCurA%RowType; vTempB vRefCurB.ename%Type; Begin Open vRefCurA For Select * from emp Where SAL > 2000; Loop Fatch vRefCurA InTo vTempA; Exit When vRefCurA%NotFound; DBMS_OUTPUT.PUT_LINE(vRefCurA%RowCount||' '|| vTempA.eno||' '||vTempA.ename ||' '||vTempA.sal) End Loop; Close vRefCurA; DBMS_OUTPUT.PUT_LINE('-------------------------------------------------------------------------------------------------------'); Open vRefCurB For Select ename from emp Where SAL > 2000; Loop Fatch vRefCurB InTo vTempB; Exit When vRefCurB%NotFound; DBMS_OUTPUT.PUT_LINE(vRefCurB%RowCount||' '||vTempB) End Loop; Close vRefCurB; DBMS_OUTPUT.PUT_LINE('-------------------------------------------------------------------------------------------------------'); Open vRefCurA For Select * from emp Where JOB = 'CLERK'; Loop Fatch vRefCurA InTo vTempA; Exit When vRefCurA%NotFound; DBMS_OUTPUT.PUT_LINE(vRefCurA%RowCount||' '|| vTempA.eno||' '||vTempA.ename ||' '||vTempA.sal) End Loop; Close vRefCurA; End; |
例子:弱类型REF游标
代码如下 | 复制代码 |
/*conn scott/tiger*/
|
6,怎样让REF游标作为参数传递?
代码如下 | 复制代码 |
--作为函数返回值 declare
--作为参数 declare |
REF CURSOR 示例包括下列三个 Visual Basic 示例,演示如何使用 REF CURSOR。
示例 说明
在 OracleDataReader 中检索 REF CURSOR 参数
此示例执行一个 PL/SQL 存储过程,返回 REF CURSOR 参数,并将值作为 OracleDataReader 读取。
使用 OracleDataReader 从多个 REF CURSOR 检索数据
此示例执行一个 PL/SQL 存储过程,返回两个 REF CURSOR 参数,并使用 OracleDataReader 读取值。
使用一个或多个 REF CURSOR 填充 DataSet
此示例执行一个 PL/SQL 存储过程,返回两个 REF CURSOR 参数,并使用返回的行填充 DataSet。
要使用这些示例,可能需要创建 Oracle 表,并且必须创建 PL/SQL 包和包正文。
创建 Oracle 表
这些示例使用 Oracle Scott/Tiger 架构中定义的表。大多数 Oracle 安装均包括 Oracle Scott/Tiger 架构。如果此架构不存在,可以使用 {OracleHome}rdbmsadminscott.sql 中的 SQL 命令文件创建供这些示例使用的表和索引。
创建 Oracle 包和包正文
这些示例要求服务器上存在以下 PL/SQL 包和包正文。在 Oracle 服务器上创建以下 Oracle 包
代码如下 | 复制代码 |
CREATE OR REPLACE PACKAGE BODY CURSPKG AS ELSE END IF; PROCEDURE OPEN_TWO_CURSORS (EMPCURSOR OUT T_CURSOR,
|
Oracle提供REF CURSOR,通过该功能可以实现在程序间传递结果集的功能,利用REF CURSOR也可以实现BULK SQL,从而提高SQL性能。
使用scott用户的emp表实现以下测试案例:
代码如下 | 复制代码 |
SQL> desc emp 使用ref cursor获得结果集输出:
PL/SQL procedure successfully completed.
|

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools