The requirements are as follows:
Create a PL/SQL function called findtotalcarmodels to return the total number of cars belonging to a particular model. The function should have a single IN parameter as model_name. You should then use an explicit cursor to count the number of cars belonging to that car model and return the final count. You must NOT use any implicit cursors, table joins, subqueries, set operators, group functions or SQL functions (such as COUNT) to create this function.
The code I have come up with so far is:
CREATE OR REPLACE Function findtotalcarmodels
(model_name IN varchar2)
RETURN NUMBER
IS
CURSOR car_count_cur IS
SELECT model_name FROM i_car;
Rec_car_details car_count_cur%ROWTYPE;
BEGIN
OPEN car_count_cur;
LOOP
FETCH car_count_cur INTO Rec_car_details;
EXIT WHEN car_count_cur%NOTFOUND;
END LOOP;
CLOSE car_count_cur;
RETURN Rec_car_details;
END;
I am getting the following errors:
Errors for FUNCTION FINDTOTALCARMODELS:
LINE/COL ERROR
15/1 PL/SQL: Statement ignored
15/8 PLS-00382: expression is of wrong type
What am I doing wrong here?
COUNT()is probably going to be in C/C++, be looking at indices/table statistics, etc. Note that I do think there is value in doing 'base' implementations of some common library functions (like, say,ArrayListin Java) for lessons... it's just that the SQL stuff is so integral to it, it feels like trying to implement an integer class. Now, an implementation ofBigIntegerI could understand... at least the exception list shows the prof knows this is a strange way to do this.