简体   繁体   English

PLSQL Procudure(Oracle)比较where子句中的变量

[英]PLSQL Procudure (Oracle) Comparing a variable in where clause

This is driving me crazy. 这真让我抓狂。 I want to do simple comparison of a column and a variable but it just doesn't work. 我想对列和变量进行简单的比较,但它不起作用。 The following line always counts all of the tuples while I only need those as conditioned by the where clause. 以下行总是计算所有元组,而我只需要那些由where子句条件限制的元组。

 SELECT count(*) INTO cnt from class where class.fid = fid;

It looks sooooo simple but I've been working on this for hours. 它看起来很简单,但我已经在这个工作了几个小时。 The complete sql proc is 完整的sql proc是

The big confusing thing is that if I replace fid with some hard coded ID (like 105) it gives a correct answer), but when I use fid it just doesn't work any more and returns count of all classes. 令人困惑的是,如果我用一些硬编码ID(如105)替换fid,它会给出一个正确的答案),但是当我使用fid时它就不再起作用了,并返回所有类的计数。 For some reason, always class.fid = fid. 由于某种原因,总是class.fid = fid。 When I use >, < or <>, 0 count is returned! 当我使用>,<或<>时,返回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;

Thanks 谢谢

I believe you need to rename or prefix your local variable fid as it unfortunately matches the column name in the table you are querying. 我相信你需要重命名或为你的本地变量fid前缀,因为它很遗憾地匹配你正在查询的表中的列名。 The SQL engine is simply comparing fid = fid for each row, which will always be true (excepting nulls, but that's another story). SQL引擎只是比较每一行的fid = fid ,它总是为真(除了空值,但这是另一个故事)。 Plus, it's harder to read your code when you have variables named the same as a column. 另外,如果变量名称与列相同,则更难以读取代码。

In PL/SQL there tends to be a convention to prefix local variables with a l_ (for local) so it's clear what the purpose is. 在PL / SQL中,通常会有一个约定,用l_ (对于本地)为局部变量加前缀,因此很清楚目的是什么。 However, any name other than a column name will suffice. 但是,列名以外的任何名称都可以。 Try: 尝试:

l_fid faculty.fid%type := 102;

And then... 接着...

SELECT count(*) INTO cnt from class where class.fid = l_fid;

(Plus other appropriate replacements.) (加上其他适当的替代品。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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