我无法解决与pl / SQL相关的函数我有代码应该检查房间容量,但我想我已经在某处声明了变量错误。老实说,不能看出问题所在。
CREATE OR REPLACE Function RoomCapacity
( name_in IN varchar2 )
RETURN varchar2
IS
R_value number(6);
RLevel varchar2(20);
cursor c1 is
SELECT room_capacity
FROM ROOMS
WHERE room_id = name_in;
BEGIN
open c1;
fetch c1 into R_value;
close c1;
IF R_value <= 10 THEN
RLevel := 'Low capacity';
ELSIF R_value > 10 and R_value <= 18 THEN
RLevel := 'Avg capacity';
ELSIF R_value > 18 and R_value <= 30 THEN
RLevel := 'Moderate capacity room';
ELSE
RLevel := 'High capacity room';
END IF;
RETURN RLevel;
END;
ROOM_ID
BUILDING_PREFIX
ROOM_FLOOR
ROOM_NUMBER
ROOM_TYPE
ROOM_CAPACITY
ROOM_EQUIPMENT
桌子ROOMS;如上所列。
答案 0 :(得分:3)
我无法看到任何错误
而且,我尝试了你的代码,它就像一个魅力,
看到这个小提琴:
http://sqlfiddle.com/#!4/0ae48/1
但是,您可以使用CASE表达式简化您的功能,只需:
RETURN
CASE WHEN R_value <= 10 THEN 'Low capacity'
WHEN R_value > 10 AND R_value <= 18 THEN 'Avg capacity'
WHEN R_value > 18 AND R_value <= 30 THEN 'Moderate capacity room'
ELSE 'High capacity room'
END;
答案 1 :(得分:1)
代码看起来不错,只有一个例外,即
的变量定义RLevel varchar2(20);
和:
ELSIF R_value > 18 and R_value <= 30 THEN
RLevel := 'Moderate capacity room';
该字符串长度为23个字符,如果输入容量在该范围内的房间,则会炸掉rLevel变量。