http://www.itpub.net/thread-1499223-20-1.html
191楼
为了鼓励访问者越来越多地使用我们的网站,我们针对他们的活动给予积分。我们的积分计算方法为基本点数和系数的乘积。如果活动发生在 08:00 之前,系数为3; 如果发生在 16:00 之前,系数为2; 其他的系数为1. 这个公式以如下函数实现:
CREATE OR REPLACE FUNCTION plch_multiplier
RETURN PLS_INTEGER
IS
c_hour CONSTANT PLS_INTEGER
:= TO_NUMBER (TO_CHAR (SYSDATE, 'HH24')) ;
BEGIN
RETURN CASE
WHEN c_hour < 8 THEN 3
WHEN c_hour < 16 THEN 2
ELSE 1
END;
END;
/
然后我用这个包里的函数为活动计算积分:
CREATE OR REPLACE PACKAGE plch_pkg
IS
FUNCTION points_for_activity (base_points_in IN INTEGER)
RETURN PLS_INTEGER;
END;
/
先不假设我们的应用程序是状态相关或者状态无关,哪些选项实现了包体,使得计算总是正确的?
(A)
CREATE OR REPLACE PACKAGE BODY plch_pkg
IS
g_multiplier PLS_INTEGER;
FUNCTION points_for_activity (base_points_in IN INTEGER)
RETURN PLS_INTEGER
IS
BEGIN
RETURN base_points_in * g_multiplier;
END;
BEGIN
g_multiplier := plch_multiplier();
END;
/
(B)
CREATE OR REPLACE PACKAGE BODY plch_pkg
IS
FUNCTION points_for_activity (base_points_in IN INTEGER)
RETURN PLS_INTEGER
IS
BEGIN
RETURN base_points_in * plch_multiplier();
END;
END;
/
(C)
CREATE OR REPLACE PACKAGE BODY plch_pkg
IS
g_multiplier PLS_INTEGER;
FUNCTION points_for_activity (base_points_in IN INTEGER)
RETURN PLS_INTEGER
IS
BEGIN
IF g_multiplier IS NULL
THEN
g_multiplier := plch_multiplier();
END IF;
RETURN base_points_in * g_multiplier;
END;
END;
/
(D)
CREATE OR REPLACE PACKAGE BODY plch_pkg
IS
g_multiplier PLS_INTEGER := plch_multiplier();
FUNCTION points_for_activity (base_points_in IN INTEGER)
RETURN PLS_INTEGER
IS
BEGIN
RETURN base_points_in * g_multiplier;
END;
END;
/
答案在194楼
2011-11-25答案B. 其他写法都是“静态”的,不能取到当前时间。