简体   繁体   中英

PL/SQL block with %ROWCOUNT compiles but doesn't output result

I am trying to print the first 3 records with the highest credit limit. My code compiles, but doesn't print anything despite having the SET SERVEROUTPUT ON SIZE 100000 and I am a bit puzzled by that. The table shows up with no problem. Also, when I have only

EXIT WHEN high_credit%ROWCOUNT > 4;

the script goes into infinite loop.

The code:

SET SERVEROUTPUT ON SIZE 1000000

DECLARE
   CURSOR high_credit IS
   SELECT first_name, last_name, employee_id, credit_limit FROM employees
      ORDER BY credit_limit DESC;   
   emp_fname employees.first_name%TYPE;
   emp_lname employees.last_name%TYPE;
   emp_empno employees.employee_id%TYPE;
   emp_crd_lmt employees.credit_limit%TYPE;
BEGIN
   DBMS_OUTPUT.PUT_LINE('First_Name ' || 'Last_Name ' || 'Employee_Id '
 || 'Credit_Limit');
   OPEN high_credit;
   LOOP
      FETCH high_credit INTO emp_fname, emp_lname, emp_empno, emp_crd_lmt;
      EXIT WHEN (high_credit%ROWCOUNT > 4) OR (high_credit%NOTFOUND);     
      DBMS_OUTPUT.PUT_LINE(emp_fname || ' ' || emp_lname || ' ' || emp_empno
 || ' ' || emp_crd_lmt);
   END LOOP;
   CLOSE high_credit;
END;

脚本输出

CREATE TABLE employees (
    employee_id NUMBER(3) NOT NULL,
    first_name VARCHAR2(8) NOT NULL,
    last_name VARCHAR2(15) NOT NULL,
    credit_limit NUMBER(4,2) NOT NULL,    
    CONSTRAINT employee_id_pk PRIMARY KEY (employee_id)    
);

INSERT INTO employees VALUES (201, 'Susan', 'Brown', 30);
INSERT INTO employees VALUES (202, 'Jim', 'Kern', 25);
INSERT INTO employees VALUES (203, 'Martha', 'Woods', 25);
INSERT INTO employees VALUES (204, 'Ellen', 'Owens', 15);
INSERT INTO employees VALUES (205, 'Henry', 'Perkins', 25);

餐桌员工

The code works after the COMMIT; on the INSERT... as suggested:

代码正在运行

Apart from invalid use of SQL%ROWCOUNT (as OldProgrammer said) (although, it doesn't affect your problem), it looks that you

  • created the table and inserted rows in SQL Developer
  • did NOT commit
  • executed your PL/SQL script in SQL*Plus
  • as inserts weren't committed, SQL*Plus session doesn't see those rows (but it sees the table as CREATE TABLE is a DDL)

Therefore

  • COMMIT in SQL Developer
  • then run your code in SQL*Plus

About the task you mentioned in the comment: see if this helps (I don't have your tables so I'm using Scott's EMP ):

SQL> set serveroutput on;
SQL> declare
  2    cursor c1 is
  3      select * from emp
  4      order by sal desc;
  5    c1r c1%rowtype;
  6  begin
  7    open c1;
  8    loop
  9      fetch c1 into c1r;
 10      exit when c1%notfound or c1%rowcount > 3;
 11
 12      dbms_output.put_line(c1%rowcount ||': '|| c1r.ename ||': '|| c1r.sal);
 13    end loop;
 14    close c1;
 15  end;
 16  /
1: KING: 5001
2: FORD: 3000
3: SCOTT: 3000

PL/SQL procedure successfully completed.

SQL>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM