这真让我抓狂。我想对列和变量进行简单的比较,但它不起作用。以下行总是计算所有元组,而我只需要那些受 where 子句限制的元组。
SELECT count(*) INTO cnt from class where class.fid = fid;
它看起来很简单,但我已经为此工作了几个小时。完整的 sql 过程是
令人困惑的是,如果我用一些硬编码的 ID(如 105)替换 fid,它会给出正确的答案),但是当我使用 fid 时,它就不再起作用并返回所有类的计数。出于某种原因,总是 class.fid = fid。当我使用 >、< 或 <> 时,返回 0 个计数!
create or replace PROCEDURE pro_report2
AS
CURSOR c_dept IS select deptid, dname from department;
TYPE cur_typ IS REF CURSOR;
c1 cur_typ;
query_str1 VARCHAR2(200);
fid faculty.fid%type := 102;
fname faculty.fname%type;
cnt NUMBER;
BEGIN
FOR dept_row in c_dept LOOP
DBMS_OUTPUT.PUT_LINE('Dept.Name: ' || dept_row.dname);
DBMS_OUTPUT.PUT_LINE('Faculty Name' || chr(9)|| chr(9) || '0 Class' || chr(9) || chr(9) || '1 Class' || chr(9) || chr(9) || '2 Classes' || chr(9) || '>2 Classes');
DBMS_OUTPUT.PUT_LINE('-------------------------------------------------------------------------------');
--find all faculty in this department
query_str1 := 'select fid, fname from faculty where faculty.deptid = ' || to_char(dept_row.deptid);
open c1 for query_str1;
LOOP
FETCH c1 into fid, fname;
exit when c1%notfound;
DBMS_OUTPUT.PUT_LINE(fname);
SELECT count(*) INTO cnt from class where class.fid = fid;
DBMS_OUTPUT.PUT_LINE(to_char(cnt) || ' ' || to_char(fid));
END LOOP;
-- Spaces between departments
DBMS_OUTPUT.PUT_LINE(chr(10));
DBMS_OUTPUT.PUT_LINE(chr(10));
END LOOP;
END;
谢谢