두 곳에서 예외 섹션을 사용하여 학생 번호가 유효하지 않은 경우 프로그램에서 오류를 인쇄하도록해야합니다. 하나는 주 절차에, 다른 하나는 하위 프로그램에 있습니다.PLSQL 예외 처리
하위 프로그램의 예외는 실행되지만 주 프로그램은 실행되지 않도록 오류를 만드는 방법은 무엇입니까?
CREATE OR REPLACE PROCEDURE validate_student(
p_snum IN students.snum%TYPE,
p_student_error out VARCHAR2) AS
v_snum students.snum%TYPE;
BEGIN
--count the number of student with id p_snum and assign it to v_snum
SELECT count(snum) INTO v_snum
FROM students
WHERE snum = p_snum;
--if count is 0 then the student does not exist in the database, return error.
IF v_snum = 0 THEN
p_student_error := 'Student number ' || p_snum || ' is invalid. ';
END IF;
END;
/
--This procedure checks if course id is valid
CREATE OR REPLACE PROCEDURE validate_course(
p_callnum IN enrollments.callnum%TYPE,
p_course_error out VARCHAR2) AS
v_callnum enrollments.callnum%TYPE;
BEGIN
--count the number of course with id p_callnum and assign it to v_callnum
SELECT count(callnum) INTO v_callnum
FROM schclasses
WHERE callnum = p_callnum;
--if count(callnum) = 0 then course does not exists in database, return error.
IF v_callnum = 0 THEN
p_course_error := 'Call number '|| p_callnum ||' is invalid .';
END IF;
END;
/
--This function check the current amount of students enrolled in the course
CREATE OR REPLACE FUNCTION course_capacity(
p_callnum enrollments.callnum%TYPE)
RETURN VARCHAR2 IS
v_current_enrolled NUMBER(3);
v_capacity NUMBER(3);
v_capacity_error VARCHAR2(200);
BEGIN
--count number of students enrolled and assign it to v_current_enrolled
SELECT count(snum) INTO v_current_enrolled
FROM enrollments
WHERE callnum = p_callnum
AND grade is null;
--check maximum capacity of the course and assign it to v_capacity
SELECT capacity INTO v_capacity
FROM schclasses
WHERE callnum = p_callnum;
--if current amount of students enrolled < maximum capacity then class is not full
--else class is full.
IF v_current_enrolled < v_capacity THEN
v_capacity_error := NULL;
ELSE
v_capacity_error := 'Course number ' || p_callnum || ' is full. ';
END IF;
RETURN v_capacity_error;
END;
/
--This function check the units a student currently enrolls.
CREATE OR REPLACE FUNCTION student_unit_limit(
p_snum students.snum%TYPE,
p_callnum schclasses.callnum%TYPE)
RETURN VARCHAR2 IS
v_student_crhr NUMBER(2);
v_course_crhr courses.crhr%TYPE;
v_crhr_error VARCHAR2(200);
BEGIN
--count the current units the student has, and assign it to v_student_crhr
SELECT nvl(sum(crhr),0) INTO v_student_crhr
FROM courses c INNER JOIN schclasses sc ON c.dept = sc.dept
AND c.cnum = sc.cnum
INNER JOIN enrollments e ON sc.callnum = e.callnum
WHERE e.snum = p_snum
AND grade IS NULL;
--find the credit hour of the course
SELECT crhr INTO v_course_crhr
FROM courses c INNER JOIN schclasses sc ON c.dept = sc.dept
AND c.cnum = sc.cnum
WHERE sc.callnum = p_callnum;
--if current credit hour of student + the credit of the class <= 15 then
--student hasn't reached maximum allowed units and can enroll in the course.
IF v_student_crhr + v_course_crhr <= 15 THEN
v_crhr_error := NULL;
ELSE
v_crhr_error := 'Max units allowed is exceeded. ';
END IF;
RETURN v_crhr_error;
END;
/
--This procedure combine all 4 of procedures and functions above,
CREATE OR REPLACE PROCEDURE addme(
p_snum students.snum%TYPE,
p_callnum schclasses.callnum%TYPE) AS
v_error_text VARCHAR2(200);
v_error_msg VARCHAR2(200);
BEGIN
validate_student(
p_snum,
v_error_text);
v_error_msg := v_error_text;
validate_course(
p_callnum,
v_error_text);
v_error_msg := v_error_msg || v_error_text;
IF v_error_msg IS NOT NULL THEN
dbms_output.put_line(v_error_msg);
ELSE
v_error_msg := course_capacity(p_callnum) || student_unit_limit(p_snum, p_callnum);
IF v_error_msg IS NOT NULL THEN
dbms_output.put_line(v_error_msg);
ELSE
INSERT INTO enrollments VALUES (p_snum, p_callnum, NULL);
COMMIT;
dbms_output.put_line('You have successfully enrolled in course number ' || p_callnum);
END IF;
END IF;
END;
/
AddMe 프로 시저가 주 프로그램이고 나머지는 서브 프로그램입니다.
의 사용 가능한 복제 [응용 프로그램 오류를 올립니다 - 오라클 (https://stackoverflow.com/questions/36295233/raise-application-error-oracle) –