Oracle函数ORA-00904无效的标识符

时间:2017-02-24 11:16:11

标签: oracle

我的Oracle函数遇到了问题,这给了我以下错误:

ORA-00904: "A": invalid identifier
ORA-06512: at "SCORING.F_BONITETA2", line 11
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:

显然,decode(a.boniteta, A, 1, 2)中的A导致此错误。我试图分别使用'A'和'A',但是当我尝试编译函数时,我在两种情况下都会出错。任何帮助表示赞赏。

这是我的功能:

create or replace FUNCTION F_BONITETA2
(
    dav in varchar2,
    tip in number
) 
RETURN number
IS
   sco number;
BEGIN
   BEGIN
     EXECUTE IMMEDIATE
     'select score 
     from sco_sif_score
     where sif_kat = 12
     and tip_pod = :tip
     and vrednost in
                  (select decode(a.boniteta, A, 1, 2)
                   from sco_boniteta a
                   inner join 
                      (select distinct a.par_davcna, a.par_reg
                       from scoring_gvin a) b
                   on a.maticna = b.par_reg
                   where b.par_davcna = :dav)'
     INTO sco USING tip, dav; 
     EXCEPTION 
           WHEN NO_DATA_FOUND THEN NULL;
   END;
   RETURN sco;
END F_BONITETA2;

1 个答案:

答案 0 :(得分:3)

如果我理解得很清楚,您需要在decode字符串上使用'A',问题在于转义动态代码中的引号。

重点可能是您根本不需要动态SQL,例如:

CREATE OR REPLACE FUNCTION F_BONITETA2(dav IN VARCHAR2, tip IN NUMBER)
    RETURN NUMBER IS
    sco                                     NUMBER;
BEGIN
    BEGIN
        SELECT score
          INTO sco
          FROM sco_sif_score
         WHERE     sif_kat = 12
               AND tip_pod = tip
               AND vrednost IN (SELECT DECODE(a.boniteta, 'A', 1, 2)
                                  FROM sco_boniteta a
                                       INNER JOIN (SELECT DISTINCT a.par_davcna, a.par_reg
                                                     FROM scoring_gvin a) b
                                           ON a.maticna = b.par_reg
                                 WHERE b.par_davcna = dav);
    EXCEPTION
        WHEN NO_DATA_FOUND
        THEN
            NULL;
    END;

    RETURN sco;
END F_BONITETA2;

另外,根据Boneist的评论,如果由于其他原因你不需要BEGIN ... END块,这可能是一种重写代码的方法:

CREATE OR REPLACE FUNCTION F_BONITETA2(dav IN VARCHAR2, tip IN NUMBER)
    RETURN NUMBER IS
    sco                                     NUMBER;
BEGIN
    SELECT score
      INTO sco
      FROM sco_sif_score
     WHERE     sif_kat = 12
           AND tip_pod = tip
           AND vrednost IN (SELECT DECODE(a.boniteta, 'A', 1, 2)
                              FROM sco_boniteta a
                                   INNER JOIN (SELECT DISTINCT a.par_davcna, a.par_reg
                                                 FROM scoring_gvin a) b
                                       ON a.maticna = b.par_reg
                             WHERE b.par_davcna = dav);
    RETURN sco;

EXCEPTION
    WHEN NO_DATA_FOUND
    THEN
        return NULL;   
END F_BONITETA2;

一般来说,通过将引号加倍或通过alternative quoting method的方式处理包含引号的字符串可能有两种方法:

SQL> select 'This is a quote '' and these are two quotes ''''.' from dual UNION ALL
  2  select q'[This is a quote ' and these are two quotes ''.]' from dual;

'THISISAQUOTE''ANDTHESEARETWOQUOTES''''.'
----------------------------------------------
This is a quote ' and these are two quotes ''.
This is a quote ' and these are two quotes ''.