I am new to Postgresql/coding in general, so please forgive me for this question. Yes, I have searched SO for the answer, as this is seemingly a common error message, but I keep coming up empty-handed.
I am having issues trying to create a table and procedure to track university students and the number of courses they have taken to verify minimum graduation requirements. I know a function would work better for this, but I have specifically been asked to create a procedure, so here we are.
First, I created a table (student_course_nums), then wrote a procedure that accepts a student ID as input (stuCourseNumsProc).
The procedure is supposed to calculate the total number of course sections attended by that student, and adds a tuple to the student_course_nums table consisting of the student’s ID number, name, and total courses attended - attributes named: ID, name, and tot_courses.
If the student already has an entry in the table, then the procedure would make sure the total number of courses attended in the student_course_nums table was up-to-date. Finally I need to call the procedure. This is really where I'm having issues.
This is my database schema:
course (course_id, title, dept_name, credits)
student (id, name, dept_name, tot_cred)
takes (id, course_id, sec_id, semester, year, grade)
Here is my code:
CREATE TABLE student_course_nums (ID VARCHAR(5), name VARCHAR(20), tot_courses INTEGER);
CREATE OR REPLACE PROCEDURE stuCourseNumsProc (IN student_id VARCHAR(5), INOUT ID VARCHAR(5),
name VARCHAR(20), tot_courses INTEGER)
LANGUAGE plpgsql
AS
$$
DECLARE
name VARCHAR(20) := '';
course_count INTEGER := 0;
BEGIN
-- determine total courses - course_count
SELECT COUNT(t.course_id) INTO course_count
FROM takes AS t INNER JOIN student AS s ON t.ID = s.ID
WHERE t.ID = stuCourseNumsProc.s_ID;
-- determine student name - into name
SELECT s.name INTO name
FROM student AS s
WHERE s.ID = stuCourseNumsProc.s_ID;
IF EXISTS (SELECT ID
FROM student_course_nums
WHERE ID = stuCourseNumsProc.s_ID)
THEN -- update record
UPDATE student_course_nums
SET tot_courses = course_count
WHERE ID = stuCourseNumsProc.s_ID;
ELSE -- insert new tuple
INSERT INTO student_course_nums (ID, name, tot_courses)
VALUES (stuCourseNumsProc.s_ID, name, course_count);
END IF;
END;
$$
CALL stuCourseNumsProc('89765');
I run the procedure first, and then I get the following error when trying to call my procedure:
ERROR: procedure stucoursenumsproc(unknown) does not exist
LINE 1: CALL stuCourseNumsProc('89765');
^
HINT: No procedure matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 6
I see the 'stuCourseNumsProc' under procedures in my database schema, so I am struggling to figure out why I am getting this error. I'm not sure if my code is correct for the rest of my problem, but I was hoping to troubleshoot that after getting the code to work.
Any suggestions/tips would be very greatly appreciated. Thanks in advance!