Oracle函数题
Examine this function:
CREATE OR REPLACE FUNCTION CALC_PLAYER_AVG (V_ID in PLAYER_BAT_STAT.PLAYER_ID%TYPE) RETURN NUMBER IS V_AVG NUMBER; BEGIN SELECT HITS / AT_BATS INTO V_AVG FROM PLAYER_BAT_STAT WHERE PLAYER_ID = V_ID; RETURN (V_AVG); END;
Which statement will successfully invoke this function in SQL *Plus?(哪种叙述在SQL*Plus中能成功调用这个函数?)
A. SELECT CALC_PLAYER_AVG(PLAYER_ID)
FROM PLAYER_BAT_STAT;
B. EXECUTE CALC_PLAYER_AVG(31);
C. CALC_PLAYER(‘RUTH’);
D. CALC_PLAYER_AVG(31);
E. START CALC_PLAYER_AVG(31)
Answer: A
Incorrect Answers(不正确的答案)
B. You can't call a function in this way, in this way you can call a procedure, because function must return a
你不能用这种方法调用一个函数,你能用这种方法调用一个过程,因为函数必须返回一个
value, to call a function using EXECUTE command you should declare a bind variable using the
值,调用函数使用EXECUTE(执行)命令 你可以声明一个绑定的变量使用这个
VARIABLE command then assign the value returned from the function to this variable, in the following
变量命令然后从返回的这个变量的函数分配这个值,用下面的方法
way:
SQL> VARIABLE v_get_value NUMBER
SQL> EXECUTE :v_get_value := CALC_PLAYER_AVG(31)
PL/SQL procedure successfully completed.
SQL> PRINT v_get_value
V_GET_VALUE
-----------
1
C. Again this way can't be use for calling a function in PL/SQL block because the function return a value
再次这个方法不能从PL/SQL块中调用函数因为这个函数返回一个值
and this values must be assigned to PL/SQL variable or to bind variable. Like this
并且这个值必须分配给PL/SQL或者绑定变量。想这样
DECLARE
v_get_from_fn NUMBER;
BEGIN
v_get_from := CALC_PLAYER_AVG(31);
END;
/
D. Same as C.(同C的回答)
E. START is use to execute a script.(START是用于执行一个脚本)