【问题标题】:how to reference function parameter in query?如何在查询中引用函数参数?
【发布时间】:2014-03-28 18:24:45
【问题描述】:

在声明的函数定义中,我想在选择查询的 WHERE 子句中使用函数参数 item_account_id 的值。

CREATE OR REPLACE 
FUNCTION UPDATE_CURRENT_BALANCE( item_account_id IN item_account.item_account_id%TYPE)                               
RETURN boolean 
AS 
BEGIN        
    if item_data_table_id = 10 then
       select current_balance_curr_id 
       from BANK_ACCOUNT 
       where item_account_id = item_account_id;        
    end if;
  RETURN true;
END UPDATE_CURRENT_BALANCE;

【问题讨论】:

    标签: oracle plsql stored-functions


    【解决方案1】:

    您有一个范围问题,因为参数名称与列相同。 Oracle 名称解析的工作方式,这两个item_account_id = item_account_id 都将识别表列。即使我们添加了一个表别名并在相等操作的一侧使用它,Oracle 仍会将其评估为1 = 1

    解决方法很简单:重命名参数。使用前缀很流行:

    CREATE OR REPLACE FUNCTION UPDATE_CURRENT_BALANCE
        ( p_item_account_id IN item_account.item_account_id%TYPE)   
    RETURN boolean 
    AS 
    BEGIN        
        if item_data_table_id = 10 then -- the posted code has no source for this? perhaps it's another parameter?
           select current_balance_curr_id 
           from BANK_ACCOUNT 
           where item_account_id = p_item_account_id;        
        end if;
      RETURN true;
    END UPDATE_CURRENT_BALANCE;   
    

    我认为item_data_table_id 是另一个在转录过程中丢失的参数。您也应该为其添加前缀:一致性在命名约定中是一件好事。

    【讨论】:

    • 如果函数执行进入 IF-THEN 逻辑块并且输入参数不匹配,则函数将从 NO DATA FOUND 错误中出错。您还应该加强函数对输入值及其条件的更多变化的响应。
    猜你喜欢
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2020-11-27
    • 2011-03-16
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多