PL/SQL 函数值的间隔精度
通常,当您指定函数时,返回数据类型的小数位数/精度/大小是未定义的。
例如,您输入 FUNCTION show_price RETURN NUMBER
或 FUNCTION show_name RETURN VARCHAR2
。
不允许使用 FUNCTION show_price RETURN NUMBER(10,2)
或 FUNCTION show_name RETURN VARCHAR2(20)
,并且函数返回值不受限制。 这是已记录的功能。
现在,如果我将 9999 小时(大约 400 天)推入以下内容,则会出现精度错误 (ORA-01873)。该限制是因为 默认天数精度为 2< /a>
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。
Generally, when you specify a function the scale/precision/size of the return datatype is undefined.
For example, you say FUNCTION show_price RETURN NUMBER
or FUNCTION show_name RETURN VARCHAR2
.
You are not allowed to have FUNCTION show_price RETURN NUMBER(10,2)
or FUNCTION show_name RETURN VARCHAR2(20)
, and the function return value is unrestricted. This is documented functionality.
Now, I get an precision error (ORA-01873) if I push 9999 hours (about 400 days) into the following. The limit is because 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;
/
and it won't allow the precision to be specified directly as part of the datatype returned by the function.
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;
/
I can use a 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;
/
Any drawbacks to the subtype approach ?
Any alternatives (eg some place to change a default precision) ?
Working with 10gR2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到的没有真正的缺点。 会更清楚一些。
我认为如果将工作变量声明为子类型的实例,例如:共享和享受,
No real drawbacks that I can think of. I think it would be a bit more clear if the working variables were declarred as instances of the subtype, e.g.:
Share and enjoy.
Oracle 为此目的提供了一些内置子类型,请参阅避免使用日期和时间子类型的截断问题
它们是:
Oracle provides some build-in subtypes for that purpose, see Avoiding Truncation Problems Using Date and Time Subtypes
They are: