如何在函数内使用select语句?

时间:2013-11-25 09:44:19

标签: oracle

我正在尝试构建一个从表中返回值的函数

    create or replace 
FUNCTION TO_LOCAL_CURRENCY 
(
  CURR IN VARCHAR2  
, VAL IN VARCHAR2  
) RETURN NUMBER AS 
BEGIN

  RETURN (select EXRATE 
  from exchangerates 
  where CURR = 'EUR' ) * VAL;
END TO_LOCAL_CURRENCY;

基本上我输入货币和价值,它从表交换中查找汇率,并将EXRATE乘以VAL并返回结果。

我收到以下错误

Error(9,11): PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:     ( - + case mod new not null <an identifier>    <

1 个答案:

答案 0 :(得分:3)

SELECT结果INTO是一个局部变量,并返回:

create or replace 
FUNCTION TO_LOCAL_CURRENCY 
(
  CURR IN VARCHAR2  
, VAL IN VARCHAR2  
) RETURN NUMBER AS 
  EXCHANGE_RATE NUMBER;
BEGIN

  select EXRATE 
  into EXCHANGE_RATE 
  from exchangerates 
  where CURR = 'EUR';

  return EXCHANGE_RATE * VAL;

END TO_LOCAL_CURRENCY;