【发布时间】:2015-06-23 04:33:11
【问题描述】:
我有下表:
T_TYPE_ID T_TYPE T_TYPE_PRICE T_TYPE_START_DATE T_TYPE_END_DATE
1 student 10.95 01.04.2015 00:00:00 30.06.2015 00:00:00
2 Concession 5.5 01.04.2015 00:00:00 30.06.2015 00:00:00
我需要:
创建FUNC_get_ t_type_end_date function
此函数应包含以下输入参数:t_type_p 和以下输出参数:t_type_price_p 和 t_type_end_date_p。如果有一条t_type为t_type_p的记录,它应该返回1,否则返回0。此外,如果有一条记录,它应该将最新的t_type_end_date分配给t_type_end_date_p,将t_type_price分配给t_type_price_p。请注意t_type_end_date_p 可以为空;这意味着相关价格当前有效。
我写了以下代码:
CREATE OR REPLACE FUNCTION FUNC_get_t_type_end_date
( t_type_p IN VARCHAR2)
RETURN NUMBER
AS
cnum NUMBER;
CURSOR cr1 IS
SELECT t_type
FROM ticket_type
WHERE t_type = t_type_p;
BEGIN
OPEN cr1;
FETCH cr1 INTO cnum;
IF cr1%NOTFOUND THEN
cnum := 0;
END IF;
CLOSE cr1;
RETURN cnum;
END;
我不知道如何从一个函数返回多个值。我正在使用 oracle。
提示:
create or replace FUNCTION FUNC_get_ t_type_end_date (…)
return number
as
-- define a variable to return a number and assign 0 to it
-- define a cursor to obtain t_type_price, t_type_end_date of the given t_type_p. The t_type_end_date values should be sorted in descending order – to do so the first record will contain either null or the latest of t_type_end_date
BEGIN
-- open cursor
-- fetch the first record from the cursor to t_type_price_p and t_type_end_date_p
-- if (having a record) then …
-- close cursor
RETURN …
END;
【问题讨论】:
-
可能是你可以使用带有
out参数的过程来代替函数 -
讨厌你在那里做的那个名字前缀的事情。每个人最终都必须输入很多额外的字符。
-
另外,显式游标方法很慢,更难编写,更难维护,因此更容易出错。