PL / SQL- cursor

The cursor can be considered as "movable pointer" points to a row in a query result, and then the specific operation by the PL / SQL procedure on the data line. As the cursor can only read one line of data, for many records, you need to read again and again, until the cursor is read far less data.
Presented here only two kinds of cursors:

  • Explicit cursor: declared by the user and a cursor operation, an operation is generally the result set.
  • Implicit cursor: when executing SQL statements automatically created by Oracle.

Explicit Cursor

Question :
In the emp table, the output " ename number is empno, salary is SAL ."
Analysis :
EMP table 14 has records, the problem is the output line 14. Each row format is " ename number is empno, salary SAL ", to use multiple rows of data output.
Step :

  • Declare the cursor [declare]
  cursor emp_cur(emp_job in varchar2:='SALESMAN')  --声明游标和对应的结果集
  is select empno,ename,sal from emp where job=emp_job;
  • Open the cursor [begin]
open emp_cur('MANAGER');  --打开游标
  • Read the cursor [begin]
fetch emp_cur into var_emp;  --指向第一行

Then execute once, then point to the next line.

  • Close the cursor [begin]
close emp_cur;  --关闭游标

Examples :

-- ename的编号是empno,工资是sal
set serveroutput on
declare
  cursor emp_cur(emp_job in varchar2:='SALESMAN')  --声明游标和对应的结果集
  is select empno,ename,sal from emp where job=emp_job;
  type emp_record is record(   --构造一个record类型
  var_empno emp.empno%type,
  var_ename emp.ename%type,
  var_sal emp.sal%type
  );
  var_emp emp_record;   --声明一个record类型的变量
begin
  open emp_cur('MANAGER');  --打开游标
  fetch emp_cur into var_emp;  --指向第一行
  while emp_cur%found loop   -- emp_cur%found:判断指针指向的行是否有数据,有的话返回true,没有的话返回false
    dbms_output.put_line(var_emp.var_ename||'的编号是'||var_emp.var_empno||',工资是'||var_emp.var_sal);
    fetch emp_cur into var_emp;    --指向下一行
  end loop;
  close emp_cur;  --关闭游标
end;
/

Explicit cursor attributes

  • [% Found cursor% found cursor line data is data referred Returns true, false if no data] [boolean]
  • % Notfound% found the opposite to the [] [] boolean
  • [% Rowcount Returns the number of rows affected by the statement sql] [int]
  • % Isopen [cursor open was true, the cursor is closed and false boolean] []
dbms_output.put_line('游标emp_cur所对应的结果集有'||emp_cur%rowcount||'行。');

Implicit Cursor

Implicit cursor statement without, Open, Read, Close, its properties can be used directly.
In PL / SQL programming, commonly used in the case of data rows affected by the update to reflect and delete.
Cain cursor result set corresponding to a nearest sql result set.

set serveroutput on
begin
  update emp set sal=sal*1.2 where job='MANAGER';  --3行数据行被更新
  if sql%notfound then
    dbms_output.put_line('没有员工需要涨工资');
  else
    dbms_output.put_line('有'||sql%rowcount||'个员工工资上涨20%');
  end if;
end;
/

Implicit cursor attributes

Implicit cursor default cursor named sql

  • [Sql% found% found cursor line data is data referred Returns true, false if no data] [boolean]
  • %notfound【sql%notfound 与%found相反】【boolean】
  • % Rowcount [sql% rowcount Returns the number of rows affected by the statement sql] [int]
  • % Isopen [sql% isopen cursors open was true, the cursor is closed and false boolean] []

to sum up

Above talk so much, the most important is to know when you need a cursor. I.e. when it is necessary to repeatedly read a set of data outputs, to the use of the cursor.

Published 23 original articles · won praise 8 · views 4118

Guess you like

Origin blog.csdn.net/weixin_36522099/article/details/103751102