Answer: Your answer should look similar to the following:
CREATE OR REPLACE FUNCTION instructor_status (i_first_name IN instructor.first_name%TYPE, i_last_name IN instructor.last_name%TYPE) RETURN VARCHAR2 AS v_instructor_id instructor.instructor_id%TYPE; v_section_count NUMBER; v_status VARCHAR2(100); BEGIN SELECT instructor_id INTO v_instructor_id FROM instructor WHERE first_name = i_first_name AND last_name = i_last_name;
SELECT COUNT(*) INTO v_section_count FROM section WHERE instructor_id = v_instructor_id;
IF v_section_count >= 3 THEN v_status := 'The instructor '||i_first_name||' '|| i_last_name||' is teaching '||v_section_count|| ' and needs a vaction.'; ELSE v_status := 'The instructor '||i_first_name||' '|| i_last_name||' is teaching '||v_section_count|| ' courses.'; END IF; RETURN v_status; EXCEPTION WHEN NO_DATA_FOUND THEN -- note that either of the SELECT statements can raise -- this exception v_status := 'The instructor '||i_first_name||' '|| i_last_name||' is not shown to be teaching'|| ' any courses.'; RETURN v_status; WHEN OTHERS THEN v_status := 'There has been in an error in the function.'; RETURN v_status; END;
Test the function as follows:
SELECT instructor_status(first_name, last_name) FROM instructor; \
|
No comments:
Post a Comment