Oracle -- plsql cursor creation and use

Source: Internet
Author: User
Cursor Is a workspace opened up by the Oracle System in the memory. It stores the query results returned by the SELECT statement ., PLSQL implicitly creates and automatically manages this game

Cursor Is a workspace opened up by the Oracle System in the memory. It stores the query results returned by the SELECT statement ., PL/SQL implicitly creates and automatically manages this game

Why Oracle cursor:

First look at this:

  • DECLARE
  • V_empno emp. empno % type;
  • V_ename emp. ename % type;
  • BEGIN
  • SELECT empno, ename
  • Into v_ename, v_empno
  • FROM emp;
  • Dbms_output.put_line (v_empno | ''| v_ename );
  • END;
  • This SELECT statement may return multiple records, so this assignment statement may be incorrect. Therefore, the cursor can be used.

    What is a cursor?

    Cursor Is a workspace opened up by the Oracle System in the memory. It stores the query results returned by the SELECT statement.

    Cursor category:

    Implicit cursor: PL/SQL implicitly creates and automatically manages this cursor. Implicit cursors are also called SQL cursors. You cannot explicitly execute OPEN, CLOSE, and FETCH statements for SQL cursors. However, you can use the cursor attribute to obtain information from the recently executed SQL statements.

    Explicit cursor: it is explicitly explained and controlled by the programmer. It is used to extract multiple rows of data from the table and process multiple rows of data with one row.

    Implicit cursor:

  • Internal declaration by Oracle
  • Auto-manage cursor by Oracle
  • You can use the cursor attribute to obtain information from the recently executed SQL statement.
  • Used to process DML statements and query that returns a single row
  • Implicit cursor attributes:

    Implicit cursor example:

  • VARIABLE rows_deleted VARCHAR2 (30)
  • DECLARE
  • V_deptno NUMBER: = 20;
  • BEGIN
  • Delete from emp
  • WHERE deptno = v_deptno;
  • : Rows_deleted: = (SQL % ROWCOUNT | 'rows deleted .');
  • END;
  • /
  • Returns the number of deleted rows.

    Explicit cursor: it is explicitly declared by the user, and multiple rows of records are returned by the query.

  • When a cursor is used, the query result of a select statement can be a single record, multiple records, or Zero record.
  • A pointer exists in the cursor workspace. In the initial state, it points to the first record of the query result.
  • To access all records of the query results, you can use the FETCH statement to move the pointer.
  • Use a cursor to define a cursor, open a cursor, extract data, and close the cursor.
  • For more information about Oracle, see the Oracle topic page? Tid = 12

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.