Oracle函数从select

时间:2018-01-17 21:56:46

标签: oracle

我正在尝试将此函数添加到oracle db,但它不断抛出以下错误:

  

10/72 PLS-00049:错误的绑定变量'COMMITID'

     

12/90 PLS-00049:错误的绑定变量'COMMITID'

     

14/76 PLS-00049:错误的绑定变量'COMMITID'

     

17/16 PL / SQL:ORA-00933:SQL命令未正确结束

CREATE OR REPLACE FUNCTION GetLatestProfileChangeDateTime(commitId IN NUMBER) 
   RETURN DATE 
   AS 
   testing DATE;
   BEGIN 
        select max(a) as dateOfChange
        INTO testing
        from
        (
        select max(created_date) a from image_set where reference_id = :commitId and created_date is not null
        union
        select max(date_of_change) a from preferred_agent_info_history where commit_id = :commitId and date_of_change is not null
        union
        select max(date_of_change) a from commit_history where commit_id = :commitId and date_of_change is not null
        )

        RETURN testing; 
    END;

内部select语句工作正常,但是当我尝试在函数中实现它时,我无法接受它。我甚至尝试在select语句中删除参数绑定作为起始位置,但它会抛出不同的错误。

1 个答案:

答案 0 :(得分:1)

您不需要在函数中绑定变量;您正在函数的SQL部分中使用函数参数,因此您可以通过其名称简单地引用它。 例如:

SQL> create or replace function f1(p IN number) return number is
  2      retVal number;
  3  begin
  4      select :p * 2 into retVal from dual;
  5      return retVal;
  6  end;
  7  /

Warning: Function created with compilation errors.

SQL> sho err
Errors for FUNCTION F1:

LINE/COL ERROR
-------- -----------------------------------------------------------------
4/12     PLS-00049: bad bind variable 'P'

正确的方式:

SQL> create or replace function f1(p IN number) return number is
  2      retVal number;
  3  begin
  4      select f1.p * 2 into retVal from dual;
  5      return retVal;
  6  end;
  7  /

Function created.

SQL> select f1(3) from dual;

     F1(3)
----------
         6
相关问题