如何打印游标记录?
How to print cursor records?
我试过打印游标记录,但是不行。下面的代码,我试过了。
DECLARE
CURSOR CUR_EMP IS
SELECT *
FROM EMP
WHERE Deptno = &DNO;
V_REC CUR_EMP%ROWTYPE;
BEGIN
OPEN CUR_EMP;
LOOP
FETCH CUR_EMP INTO V_REC;
EXIT WHEN CUR_EMP%NOTFOUND;
DBMS_OUTPUT.PUT_LINE (
'Record No:'
|| CUR_EMP%ROWCOUNT
|| ' '
|| V_REC.ENAME
|| '
'
|| V_REC.SAL);
END LOOP;
CLOSE CUR_EMP;
END;
如果你运行像你那样:
SQL> declare
2 cursor cur_emp is select * from emp
3 where deptno = &dno;
4 v_rec cur_emp%rowtype;
5 begin
6 open cur_emp;
7 loop
8 fetch cur_emp into v_rec;
9 exit when cur_emp%notfound;
10
11 dbms_output.put_line('Record no: ' || cur_emp%rowcount ||' '|| v_rec.ename);
12 end loop;
13 close cur_emp;
14 end;
15 /
Enter value for dno: 10
old 3: where deptno = &dno;
new 3: where deptno = 10;
PL/SQL procedure successfully completed. --> there's no output
但是,如果您启用输出...
SQL> set serveroutput on
SQL>
... 和 运行 再次使用该代码:
SQL> /
Enter value for dno: 10
old 3: where deptno = &dno;
new 3: where deptno = 10;
Record no: 1 CLARK --> oh yes, here they are!
Record no: 2 KING
Record no: 3 MILLER
PL/SQL procedure successfully completed.
SQL>
因此,set serveroutput on
在 SQL*Plus(或 SQL 开发人员,甚至可能有更多支持它的工具)中,或在您的 GUI 中启用它。因为,该代码应该 运行 正确(当然,如果满足 WHERE
条件)。
此外,考虑使用游标 FOR
循环,它更易于维护,因为 Oracle 执行大部分 dirty 作业(声明游标变量、打开游标、关注退出循环,关闭光标)为你:
SQL> begin
2 for cur_emp in (select rownum rn, e.* from emp e where deptno = &dno) loop
3 dbms_output.put_line('Record no: ' || cur_emp.rn ||' '|| cur_emp.ename);
4 end loop;
5 end;
6 /
Enter value for dno: 10
old 2: for cur_emp in (select rownum rn, e.* from emp e where deptno = &dno) loop
new 2: for cur_emp in (select rownum rn, e.* from emp e where deptno = 10) loop
Record no: 1 CLARK
Record no: 2 KING
Record no: 3 MILLER
PL/SQL procedure successfully completed.
SQL>
我试过打印游标记录,但是不行。下面的代码,我试过了。
DECLARE
CURSOR CUR_EMP IS
SELECT *
FROM EMP
WHERE Deptno = &DNO;
V_REC CUR_EMP%ROWTYPE;
BEGIN
OPEN CUR_EMP;
LOOP
FETCH CUR_EMP INTO V_REC;
EXIT WHEN CUR_EMP%NOTFOUND;
DBMS_OUTPUT.PUT_LINE (
'Record No:'
|| CUR_EMP%ROWCOUNT
|| ' '
|| V_REC.ENAME
|| '
'
|| V_REC.SAL);
END LOOP;
CLOSE CUR_EMP;
END;
如果你运行像你那样:
SQL> declare
2 cursor cur_emp is select * from emp
3 where deptno = &dno;
4 v_rec cur_emp%rowtype;
5 begin
6 open cur_emp;
7 loop
8 fetch cur_emp into v_rec;
9 exit when cur_emp%notfound;
10
11 dbms_output.put_line('Record no: ' || cur_emp%rowcount ||' '|| v_rec.ename);
12 end loop;
13 close cur_emp;
14 end;
15 /
Enter value for dno: 10
old 3: where deptno = &dno;
new 3: where deptno = 10;
PL/SQL procedure successfully completed. --> there's no output
但是,如果您启用输出...
SQL> set serveroutput on
SQL>
... 和 运行 再次使用该代码:
SQL> /
Enter value for dno: 10
old 3: where deptno = &dno;
new 3: where deptno = 10;
Record no: 1 CLARK --> oh yes, here they are!
Record no: 2 KING
Record no: 3 MILLER
PL/SQL procedure successfully completed.
SQL>
因此,set serveroutput on
在 SQL*Plus(或 SQL 开发人员,甚至可能有更多支持它的工具)中,或在您的 GUI 中启用它。因为,该代码应该 运行 正确(当然,如果满足 WHERE
条件)。
此外,考虑使用游标 FOR
循环,它更易于维护,因为 Oracle 执行大部分 dirty 作业(声明游标变量、打开游标、关注退出循环,关闭光标)为你:
SQL> begin
2 for cur_emp in (select rownum rn, e.* from emp e where deptno = &dno) loop
3 dbms_output.put_line('Record no: ' || cur_emp.rn ||' '|| cur_emp.ename);
4 end loop;
5 end;
6 /
Enter value for dno: 10
old 2: for cur_emp in (select rownum rn, e.* from emp e where deptno = &dno) loop
new 2: for cur_emp in (select rownum rn, e.* from emp e where deptno = 10) loop
Record no: 1 CLARK
Record no: 2 KING
Record no: 3 MILLER
PL/SQL procedure successfully completed.
SQL>