Use of Oracle Advanced explicit cursor

Source: Internet
Author: User

1. parameterized cursor
Purpose: Open the same cursor multiple times and return different result sets. The parameters in the declaration part of the cursor must correspond to the real parameters provided in the OPEN statement (quantity and data type ).
 
Example 1:
DECLARE
V_empno emp1.empno % TYPE;
V_ename emp1.ename % TYPE;
CURSOR emp_cursor
(P_deptno NUMBER, p_job VARCHAR2) IS -- you do not need to specify the precision when defining parameters.
SELECT empno, ename
FROM emp1
WHERE deptno = p_deptno
AND job = p_job;
BEGIN
OPEN emp_cursor (30, 'salesman'); -- a cursor with Parameters
LOOP
FETCH emp_cursor INTO v_empno, v_ename;
Exit when emp_cursor % notfound or emp_cursor % notfound is null;
DBMS_OUTPUT.PUT_LINE ('empno is '| v_empno | 'and' | 'ename is' | v_ename );
End loop;
CLOSE emp_cursor;
END;
 

2. Record + FOR + cursor Parameters
 
DECLARE
CURSOR emp_c (p_job VARCHAR2)
Is select ename, sal FROM emp
WHERE job = p_job for update nowait;
BEGIN
FOR e_record IN emp_c (UPPER ('& job') -- Use Substitution variable
LOOP
Dbms_output.put_line (e_record.ename | '''s sal: '| e_record.sal );
End loop;
END;
/
 
Enter value for jobs: manager
Old 6: for e_record in emp_c (upper ('& jobs '))
New 6: for e_record in emp_c (upper ('manager '))
JONES's sal: 2975
BLAKE's sal: 2850
CLARK's sal: 2450

  • 1
  • 2
  • Next Page

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.