ORACLE游标
游标:容器,存放当前SQL语句影响的记录
所有DML语句都会用到游标
逐行处理影响的行数
游标
静态游标:游标与SQL语句在运行前关联
动态游标:游标与SQL语句在运行时关联
游标
隐式游标:自动声明、打开、关闭,其名SQL
显式游标:
REF游标:(动态游标)
显式游标操作过程
声明游标(关联SQL语句) cursor 名字 is/as sql语句(DML);
打开游标(执行SQL语句,填充游标) open 名字;
从游标中提取值,存放到行类型变量中 fetch 名字 into 行类型变量;
关闭游标 close 名字;
declare empR emp%rowtype; --声明游标(关联SQL语句) cursor empCursor is select * from emp; begin --打开游标(执行SQL语句,填充游标) open empCursor; loop --从游标中提取值,存放到行类型变量中 fetch empCursor into empR; exit when empCursor%notfound;--游标中没有数据时退出循环 dbms_output.put_line(empR.ename||' '||empR.sal); end loop; --关闭游标 close empCursor; end;
SQL> ed SQL> /
SMITH 800 ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 SCOTT 3000 KING 5000 TURNER 1500 ADAMS 1100 JAMES 950 FORD 3000 MILLER 1300
PL/SQL procedure successfully completed |
declare --声明游标(关联SQL语句) cursor empCursor is select * from emp; begin for empR in empCursor loop dbms_output.put_line(empR.ename||' '||empR.sal); end loop; end;
SQL> ed SQL> /
SMITH 800 ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 SCOTT 3000 KING 5000 TURNER 1500 ADAMS 1100 JAMES 950 FORD 3000 MILLER 1300
PL/SQL procedure successfully completed |
declare empR emp%rowtype; --声明游标(关联SQL语句) cursor empCursor(eno number) is select * from emp where empno=eno; begin --打开游标(执行SQL语句,填充游标) open empCursor(7788); loop --从游标中提取值,存放到行类型变量中 fetch empCursor into empR; exit when empCursor%notfound;--游标中没有数据时退出循环 dbms_output.put_line(empR.ename||' '||empR.sal); end loop; --关闭游标 close empCursor; end;
SQL> ed SQL> /
SCOTT 3000
PL/SQL procedure successfully completed |
declare empR emp%rowtype; --声明游标(关联SQL语句) cursor empCursor(eno number) is select * from emp where empno=eno; begin --打开游标(执行SQL语句,填充游标) open empCursor(7788); loop --从游标中提取值,存放到行类型变量中 fetch empCursor into empR; exit when empCursor%notfound;--游标中没有数据时退出循环 update emp set ename='hp2008' where current of empCursor; end loop; --关闭游标 close empCursor; end;
SQL> ed SQL> /
declare empR emp%rowtype; --声明游标(关联SQL语句) cursor empCursor(eno number) is select * from emp where empno=eno; begin --打开游标(执行SQL语句,填充游标) open empCursor(7788); loop --从游标中提取值,存放到行类型变量中 fetch empCursor into empR; exit when empCursor%notfound;--游标中没有数据时退出循环 update emp set ename='hp2008' where current of empCursor; end loop; --关闭游标 close empCursor; end;
ORA-06550: 第 15 行, 第 55 列: PLS-00404: 必须使用 FOR UPDATE 来说明游标 'EMPCURSOR',以便将其与 CURRENT OF 一起使用 ORA-06550: 第 15 行, 第 55 列: PL/SQL: ORA-00904: : 无效的标识符 ORA-06550: 第 15 行, 第 8 列: PL/SQL: SQL Statement ignored |
declare empR emp%rowtype; --声明游标(关联SQL语句) cursor empCursor(eno number) is select * from emp where empno=eno for update; begin --打开游标(执行SQL语句,填充游标) open empCursor(7788); loop --从游标中提取值,存放到行类型变量中 fetch empCursor into empR; exit when empCursor%notfound;--游标中没有数据时退出循环 update emp set ename='hp2008' where current of empCursor; end loop; --关闭游标 close empCursor; end;
SQL> ed SQL> /
PL/SQL procedure successfully completed
SQL> select empno,ename from emp;
EMPNO ENAME ----- ---------- 7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 hp2008 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER
14 rows selected |
declare empR emp%rowtype; --声明游标类型 type hpType is ref cursor; --声明游标 hpCursor hpType; begin --打开游标(关联SQL语句,执行SQL语句,填充游标) open hpCursor for select * from emp; loop --从游标中提取值,存放到行类型变量中 fetch hpCursor into empR; exit when hpCursor%notfound;--游标中没有数据时退出循环 dbms_output.put_line(empR.ename||' '||empR.sal); end loop; --关闭游标 close hpCursor; end;
SQL> ed SQL> /
SMITH 800 ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 hp2008 3000 KING 5000 TURNER 1500 ADAMS 1100 JAMES 950 FORD 3000 MILLER 1300
PL/SQL procedure successfully completed |
declare empR emp%rowtype; --声明游标类型 type hpType is ref cursor; --声明游标 hpCursor hpType; begin --打开游标(关联SQL语句,执行SQL语句,填充游标) open hpCursor for select * from emp; loop --从游标中提取值,存放到行类型变量中 fetch hpCursor into empR; dbms_output.put_line(empR.ename||' '||empR.sal); exit when hpCursor%notfound;--游标中没有数据时退出循环 end loop; --关闭游标 close hpCursor; end;
SQL> ed SQL> /
SMITH 800 ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 hp2008 3000 KING 5000 TURNER 1500 ADAMS 1100 JAMES 950 FORD 3000 MILLER 1300 MILLER 1300 |