How to create a count PL SQL Function that accepts multiple parameters?

时间:2018-04-20 00:37:28

标签: sql oracle

My professor has assigned me to create a PL SQL function for a university style database. The function should return a count of the number of courses a professor is teaching in a particular year. However my current code is just returning a count of every record in the table and I'm not sure why. FacSSN is simply a faculty identifier, I know it's dumb but the professor mandated it, it's the same value as FacID from the main Faculty table.

CREATE OR REPLACE FUNCTION Get_Tload( FacSSN IN NUMBER, OffYear IN NUMBE R)
    RETURN NUMBER IS Total_Course_Load NUMBER;
BEGIN 

    SELECT
        COUNT(*) INTO Total_Course_Load
    FROM
        OFFERING; 

    RETURN Total_Course_Load;

End; 

BEGIN
    DBMS_OUTPUT.PUT_LINE( Get_Tload( 9002, 2017 ) );
END; 

1 个答案:

答案 0 :(得分:0)

my current code is just returning a count of every record in the table and I'm not sure why.

Your SELECT expression doesn't perform any filtering or grouping. I'm curious why you'd think your function would magically filter without adding a WHERE clause.

SELECT
    COUNT(*) INTO Total_Course_Load
FROM
    OFFERING
WHERE
    OFFERINGA.FacSSN = FacSSN
    AND
    OFFERING.OffYear = OffYear;
相关问题