1、游标分两种,隐式游标和显式游标,
在每执行一次select语句时都由ORACLE自动完成游标的打开和关闭,
显式游标包括申明、打开、提取和关闭四个过程,
an example
a、静态游标(test.sql)
create or replace procedure test
as
--auths是表名、salary是字段名、%type表示该变量的类型和salary字段一致
v_salary auths.salary%type;
cursor mycursor is select salary from auths; --定义静态游标
begin
open mycursor; --打开游标
loop
fetch mycursor into v_salary; --提取游标
exit when mycursor%notfound;
end loop;
close mycursor; --关闭游标
end test;
/
3、存储过程编译就是执行上述脚本@test.sql
执行存储过程 exec test;
在每执行一次select语句时都由ORACLE自动完成游标的打开和关闭,
显式游标包括申明、打开、提取和关闭四个过程,
an example
a、静态游标(test.sql)
create or replace procedure test
as
--auths是表名、salary是字段名、%type表示该变量的类型和salary字段一致
v_salary auths.salary%type;
cursor mycursor is select salary from auths; --定义静态游标
begin
open mycursor; --打开游标
loop
fetch mycursor into v_salary; --提取游标
exit when mycursor%notfound;
end loop;
close mycursor; --关闭游标
end test;
/
3、存储过程编译就是执行上述脚本@test.sql
执行存储过程 exec test;
SQL> desc auths;
名称 是否为空? 类型
----------------------------------------- -------- -----------
SALARY NUMBER(8,2)create or replace procedure mytest
as
type ref_cursor is ref cursor;
cursor_salary ref_cursor;
v_salary auths.salary%type;
begin
open cursor_salary for select salary from auths;
loop
fetch cursor_salary into v_salary;
exit when cursor_salary%notfound;
end loop;
close cursor_salary;
end mytest;
/
1、%FOUND
当游标或游标变量(即动态游标)被打开且在执行FETCH语句之前时、%FOUND属性为NULL、
然后、如果最后的FETCH语句返回一行记录,则%FOUND为TRUE、如果FETCH没有返回任何记录,则%FOUND为FALSE
2、%ISOPEN
当游标被打开时%ISOPEN为TRUE,反之为FALSE
我们可以用这个属性来判断游标是否被打开
如:IF MYCURSOR%ISOPEN THEN
3、%NOTFOUND和%FOUND的意义相反
4、%ROWCOUNT用来返回从游标中提取出来的记录数,在游标没有被打开并没有被执行FETCH之前,%ROWCOUNT为0、静态游标友一种特殊的游标叫 select for update游标
时为了方便处理数据,自己看看书吧