通常,指定函数时,返回数据类型的比例/精度/大小未定义.
例如,您说FUNCTION show_price RETURN NUMBER或FUNCTION show_name RETURN VARCHAR2.
您不能拥有FUNCTION show_price RETURN NUMBER(10,2)或FUNCTION show_name RETURN VARCHAR2(20),并且函数返回值不受限制. This is documented functionality.
现在,如果我将9999小时(约400天)推入以下,我会得到一个精度误差(ORA-01873).限制是因为the default days precision is 2
DECLARE v_int INTERVAL DAY (4) TO SECOND(0); FUNCTION hhmm_to_interval return INTERVAL DAY TO SECOND IS v_hhmm INTERVAL DAY (4) TO SECOND(0); BEGIN v_hhmm := to_dsinterval('PT9999H'); RETURN v_hhmm; -- END hhmm_to_interval; BEGIN v_int := hhmm_to_interval; end; /
并且它不允许将精度直接指定为函数返回的数据类型的一部分.
DECLARE v_int INTERVAL DAY (4) TO SECOND(0); FUNCTION hhmm_to_interval return INTERVAL DAY (4) TO SECOND IS v_hhmm INTERVAL DAY (4) TO SECOND(0); BEGIN v_hhmm := to_dsinterval('PT9999H'); RETURN v_hhmm; -- END hhmm_to_interval; BEGIN v_int := hhmm_to_interval; end; /
我可以使用SUBTYPE
DECLARE subtype t_int is INTERVAL DAY (4) TO SECOND(0); v_int INTERVAL DAY (4) TO SECOND(0); FUNCTION hhmm_to_interval return t_int IS v_hhmm INTERVAL DAY (4) TO SECOND(0); BEGIN v_hhmm := to_dsinterval('PT9999H'); RETURN v_hhmm; -- END hhmm_to_interval; BEGIN v_int := hhmm_to_interval; end; /
子类型方法的任何缺点?
任何替代方案(例如某些地方可以改变默认精度)?
使用10gR2.
没有我能想到的真正的缺点.我认为将工作变量声明为子类型的实例会更加清楚,例如:
DECLARE subtype t_int is INTERVAL DAY (4) TO SECOND(0); v_int t_int; FUNCTION hhmm_to_interval return t_int IS v_hhmm t_int; BEGIN v_hhmm := to_dsinterval('PT9999H'); RETURN v_hhmm; END hhmm_to_interval; BEGIN v_int := hhmm_to_interval; DBMS_OUTPUT.PUT_LINE('v_int=' || v_int); end;
分享和享受.