Home Database Mysql Tutorial Oracle显示游标的使用详解

Oracle显示游标的使用详解

Jun 07, 2016 pm 05:25 PM
oracle cursor

Oracle显示游标就是在声明单元明确定义的SELECT子句,并同时指定一个名字通过这个名字规范化地引用游标属性,同时在OPEN,FETCH,C

Oracle显示游标就是在声明单元明确定义的SELECT子句,并同时指定一个名字
通过这个名字规范化地引用游标属性,同时在OPEN,FETCH,CLOSE语句中通过名字来引用显示游标
INSERT,UPDATE,DELETE或者MERGE都没有显示游标这一说     

      ㈠ 声明显示游标

      语法:
      CURSOR cursor_name [ ( [parameter [,parameter...] ) ]  [RETURN return_specification]
      IS
        SELECT_statement
      这里Think建议,大家把游标放在包中定义
      这是一处声明,到处使用的方式
      以后要改善维护这个查询也就变得更加容易,而且能最小化查询语句的解析次数,同样可从性能上获益
      不过千万注意,在包级别定义,游标的生命是持续于整个会话范围
      这也意味着一个包级别的游标会一直保持打开状态,除非我们显示关闭该游标或者杀掉session
      下面对可选部分的参数列表和RETURN作详尽说明
     
      ⑴ 为什么要使用RETURN这个语句呢?
      首先,使用RETURN语句,实际上相当于公开声明了每次FETCH操作会返回的数据结构
      也就是,游标返回什么样的记录,返回的顺序是什么,都包含了哪些列等这些信息都已大白天下
      这就带来一个巧妙的地方,即我们可以把游标头和游标体分隔开来,比如:
      PACKAGE emp_info
      IS
        CURSOR emp_cur (name_in IN emp.name%TYPE) RETURN emp%ROWTYPE;
      END;
     
      PACKAGE BODY emp_info
      IS
        CURSOR emp_cur (name_in IN emp.name%TYPE) RETURN emp%ROWTYPE
        IS
          SELECT * FROM EMP WHERE name LIKE name_in;
      END;
      显然,包体的游标实际上是个黑盒子
      这带来两点好处:
      --隐藏信息,包体如何实现就可以变得非常神秘
      --最小化重编译,我们可以按需求的变化修改包体内游标的实现而不会影响到包头的游标规范
        这也意味着所有依赖于这个包的程序都不会被置成无效状态,自然也无须重编译
      游标的RETURN语句,可以由下面任意一种数据类型组成:
      ▲ table_name%ROWTYPE:基于某个数据库表定义的记录类型
      ▲ cursor_name%ROWTYPE:基于某个已经定义好的游标定义的记录类型
      ▲ record_type%ROWTYPE:基于程序员自定义的记录类型
      这里还有一个需要注意的是,SELECT 列表和RETURN的返回记录,他俩:
      --列的数量要相匹配
      --记录的数据类型也须要相互匹配
     
      ⑵ 啥时需要把我们的游标参数化呢?
      如果我们要在多个地方使用一个游标,每次只是WHERE子句的值不同,我就可以创建一个带参的游标,,比如:
      DECLARE
        CURSOR cursor_name (par_in IN VARCHAR2)
        IS
          SELECT emp_id,emp_name FROM emps
            WHERE emp_name=UPPER(par_in);
      游标中最常见的会使用参数的地方就是WHERE子句,不过也可以在SELECT语句中的任何地方使用参数,比如:
      DECLARE
        CURSOR cursor_name (par_in IN VARCHAR2)
        IS
          SELECT emp_name,par_in,job FROM emps
            WHERE emp_name=UPPER(par_in);
      游标参数的作用范围受限于游标,我们无法在游标关联的SELECT语句之外引用游标参数,如,执行单元处引用则无法编译通过
      游标参数只能是一个IN型参数,游标是不能通过参数列表把值传出去的
      游标参数化大抵有两个好处:
      ① 避免WHERE过滤条件是硬编码,让游标更好的重用
      ② 避免游标作用范围的问题,我们可在外层块定义游标,然后在内层块用局部变量调用这个游标
     
      ㈡ 打开显示游标
     
      语法:
      OPEN cursor_name [ ( parameter [,parameter...] ) ];
      Think认为,这里最重要的是关注Oracle的读一致性
      当我们打开一个游标时,PL/SQL会执行这个游标的查询语句,并标识出活跃数据集--符合WHERE过滤的记录
      但是OPEN不会真正提取出任何一行数据,这个动作是由FETCH来完成
      然而,无论我们什么时候开始第一次FETCH数据,Oracle都会保证所有的FETCH反映的都是游标打开那一刻的数据状态
      也就是,从打开游标的那一刻直到游标关闭的那一刻,通过游标获取的数据会自动忽略掉游标打开以后
      其他活动会话所进行的插入、更新、删除等操作
      如果我们使用FOR UPDATE子句,所有的活跃数据集在游标打开那时刻都会被锁定
      这就是Oracle的读一致性
      Oracle利用SCN来实现这个理论,开始查询时,会确定一个SELECT SCN,这样就保证了事务槽里所有的SCN都小于SELECT SCN

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL: From Small Businesses to Large Enterprises MySQL: From Small Businesses to Large Enterprises Apr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

How does MySQL index cardinality affect query performance? How does MySQL index cardinality affect query performance? Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

See all articles