17,086
社区成员
发帖
与我相关
我的任务
分享
SQL> set serveroutput on
SQL>
SQL> declare
2 cv_c sys_refcursor;
3 zero_row_in_cursor EXCEPTION;
4
5 begin
6 open cv_c for select 1 from dual where 1=0;
7 dbms_output.put_line(cv_c%rowcount);
8 if cv_c%rowcount=0 then
9 close cv_c;
10 RAISE zero_row_in_cursor;
11 end if;
12 exception
13 when zero_row_in_cursor then
14 rollback;
15 dbms_output.put_line('error,zero row find in cursor');
16 end;
17 /
0
error,zero row find in cursor
PL/SQL procedure successfully completed
SQL>
SQL> set serveroutput on
SQL>
SQL> declare
2 cv_c sys_refcursor;
3 zero_row_in_cursor EXCEPTION;
4
5 begin
6 open cv_c for select 1 from dual where 1=0;
7 dbms_output.put_line(cv_c%rowcount);
8 if cv_c%rowcount=0 then
9 close cv_c;
10 RAISE zero_row_in_cursor;
11 end if;
12 exception
13 when zero_row_in_cursor then
14 rollback;
15 dbms_output.put_line('error,zero row find in cursor');
16 end;
17 /
0
error,zero row find in cursor
PL/SQL procedure successfully completed
SQL>
SQL> set serveroutput on
SQL>
SQL> declare
2 cv_c sys_refcursor;
3 begin
4 open cv_c for select 1 from dual where 1=0;
5 dbms_output.put_line(cv_c%rowcount);
6 close cv_c;
7 end;
8 /
0
PL/SQL procedure successfully completed
SQL>
--一个游标操作的模版
declare
v_name varchar2(10);
cursor cur is select name from tb;
begin
open cur;
fetch cur into v_name ;
while cursor%found loop
dbms_output.put_line(v_name);
fetch cur into v_name;
end loop;
close cur;
end;
DECLARE
CURSOR your_cursor IS SELECT first_name || ' ' || last_name FROM employees WHERE salary > 10000;
fn VARCHAR2(30);
BEGIN
OPEN your_cursor;
LOOP
FETCH your_cursor INTO fn;
EXIT WHEN your_cursor%notfound;
DBMS_OUTPUT.PUT_LINE(fn);
END LOOP;
CLOSE your_cursor;
END;
-- 结果
Neena Kochhar
Lex De Haan
--给个例子
declare
cursor emp_cur is select empno,ename,sal from emp for update;
emp_row emp_cur%rowtype;
begin
open emp_cur;
loop
fetch emp_cur into emp_row;
exit when emp_cur%notfound;
if emp_row.sal<2000 then
--使用current of emp_cur时一定要for update;
update emp set sal=sal*1.1 where current of emp_cur;
end if;
end loop;
close emp_cur;
end;
cursor%notfound
then