用户定义函数中的异常和一个用户定义函数中的两个 select 查询
exception in user defined functions and two select query in one user defined function
我实际上只想在导师没有评分时抛出异常。
所以我想我会计算他收到了多少评级,这样我也可以输出导师收到了多少评级,如果它是 0 或只有 1 评级,则会抛出无法计算平均值的异常。
我不能同时输出,它输出两行的平均值
我也无法抛出异常,是否有另一种方法可以做到这一点
CREATE OR REPLACE FUNCTION averageRating (tutorrating NUMBER)
RETURN NUMBER
IS
countRating NUMBER;
averages NUMBER;
BEGIN
SELECT AVG(NumberStars) INTO averages
FROM Rating
WHERE Tutor_ID = tutorrating ;
SELECT count(customer_ID) INTO countRating
FROM Rating
WHERE Tutor_ID = tutorrating ;
Return averages;
Return countRating;
---EXCEPTION too few data
END averageRating;
DECLARE
averages number;
countRating NUMBER;
BEGIN
averages := averageRating(1);
countRating := averageRating(1);
dbms_output.put_line('The averageRating is:' || averages);
dbms_output.put_line('The number of ratings is:' || countRating);
END;
您想要的函数输出不是 Oracle 函数的预期行为。尽管您可以像下面这样创建一个 -
CREATE OR REPLACE FUNCTION averageRating (tutorrating IN NUMBER, -- By default parameter type is IN.
total_count_Rating OUT NUMBER) -- Since you need to return count_rating along with average and function can return only 1 value,
-- So you need 1 additional Out parameter for total_count_rating.
RETURN NUMBER
IS
countRating NUMBER;
averages NUMBER;
too_few_data EXCEPTION; -- Declared EXCEPTION for checking too_few_count.
BEGIN
SELECT AVG(NumberStars), count(customer_ID)
INTO averages, countRating
FROM Rating
WHERE Tutor_ID = tutorrating;
IF countRating <= 1 THEN
RAISE too_few_data;
END IF;
total_count_Rating := countRating;
Return averages;
EXCEPTION
WHEN too_few_data THEN
DBMS_OUTPUT.PUT_LINE('Rating is too few.');
total_count_Rating := 0; -- When there is too_few_Rating, Only then you nned to return total_count_Rating as 0.
RETURN 0; -- Return 0 will ensure to retun average as 0 when there is too_few_data to calculate the average.
END averageRating;
然后你可以调用你的函数 -
DECLARE
averages number;
countRating NUMBER;
BEGIN
averages := averageRating(1, countRating);
dbms_output.put_line('The averageRating is:' || averages);
dbms_output.put_line('The number of ratings is:' || countRating);
END;
我实际上只想在导师没有评分时抛出异常。 所以我想我会计算他收到了多少评级,这样我也可以输出导师收到了多少评级,如果它是 0 或只有 1 评级,则会抛出无法计算平均值的异常。
我不能同时输出,它输出两行的平均值 我也无法抛出异常,是否有另一种方法可以做到这一点
CREATE OR REPLACE FUNCTION averageRating (tutorrating NUMBER)
RETURN NUMBER
IS
countRating NUMBER;
averages NUMBER;
BEGIN
SELECT AVG(NumberStars) INTO averages
FROM Rating
WHERE Tutor_ID = tutorrating ;
SELECT count(customer_ID) INTO countRating
FROM Rating
WHERE Tutor_ID = tutorrating ;
Return averages;
Return countRating;
---EXCEPTION too few data
END averageRating;
DECLARE
averages number;
countRating NUMBER;
BEGIN
averages := averageRating(1);
countRating := averageRating(1);
dbms_output.put_line('The averageRating is:' || averages);
dbms_output.put_line('The number of ratings is:' || countRating);
END;
您想要的函数输出不是 Oracle 函数的预期行为。尽管您可以像下面这样创建一个 -
CREATE OR REPLACE FUNCTION averageRating (tutorrating IN NUMBER, -- By default parameter type is IN.
total_count_Rating OUT NUMBER) -- Since you need to return count_rating along with average and function can return only 1 value,
-- So you need 1 additional Out parameter for total_count_rating.
RETURN NUMBER
IS
countRating NUMBER;
averages NUMBER;
too_few_data EXCEPTION; -- Declared EXCEPTION for checking too_few_count.
BEGIN
SELECT AVG(NumberStars), count(customer_ID)
INTO averages, countRating
FROM Rating
WHERE Tutor_ID = tutorrating;
IF countRating <= 1 THEN
RAISE too_few_data;
END IF;
total_count_Rating := countRating;
Return averages;
EXCEPTION
WHEN too_few_data THEN
DBMS_OUTPUT.PUT_LINE('Rating is too few.');
total_count_Rating := 0; -- When there is too_few_Rating, Only then you nned to return total_count_Rating as 0.
RETURN 0; -- Return 0 will ensure to retun average as 0 when there is too_few_data to calculate the average.
END averageRating;
然后你可以调用你的函数 -
DECLARE
averages number;
countRating NUMBER;
BEGIN
averages := averageRating(1, countRating);
dbms_output.put_line('The averageRating is:' || averages);
dbms_output.put_line('The number of ratings is:' || countRating);
END;