Small examples of using cursors, dynamic SQL, and bound variables in PL/SQL

Source: Internet
Author: User

Requirements: Query and output employee information for Department 30th

Mode one: Use Loop...fetch

1 SETServeroutput on;2 DECLARE3   CURSORC_emp is 4     SELECT *  fromEmpWHEREDeptno=  -;5V_emp EMP%RowType; 6 BEGIN7   OPENc_emp;8 Loop9       FetchC_emp intov_emp;Ten       Exit  whenC_emp%NotFound; OneDbms_output.put_line (v_emp.empno||'  '||V_emp.ename||'  '||V_emp.job||'  '||V_emp.mgr||'  '||V_emp.hiredate||'  '||V_emp.sal||'  '||V_emp.comm||'  '||V_emp.deptno); A     EndLoop; -   Closec_emp; - END; the /

Mode two: Using a cursor for loop

1 SETServeroutput on;2 DECLARE3   CURSORC_emp is4     SELECT *  fromEmpWHEREDeptno=  -;5V_emp EMP%RowType; 6 BEGIN7    forEinchC_emp Loop8Dbms_output.put_line (e.empno||'  '||E.ename||'  '||E.job||'  '||E.mgr||'  '||E.hiredate||'  '||E.sal||'  '||E.comm||'  '||E.deptno);9   EndLoop;Ten END; One /

Method Three: Using dynamic SQL and binding variables

1 SETServeroutput on;2 DECLARE3TYPE Emp_cursor isREFCURSOR;4 c_emp emp_cursor;5V_emp EMP%RowType;6 BEGIN7   OPENC_emp for 'SELECT * from emp where Deptno =:n'USING -;8 Loop9     FetchC_emp intov_emp;Ten       Exit  whenC_emp%NotFound; OneDbms_output.put_line (v_emp.empno||'  '||V_emp.ename||'  '||V_emp.job||'  '||V_emp.mgr||'  '||V_emp.hiredate||'  '||V_emp.sal||'  '||V_emp.comm||'  '||V_emp.deptno); A   ENDLoop; -   Closec_emp; - END; the /

Small examples of using cursors, dynamic SQL, and bound variables in PL/SQL

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.