代码之家  ›  专栏  ›  技术社区  ›  fire frost

创建包SQL developer时出现符号错误

  •  0
  • fire frost  · 技术社区  · 7 年前

    我的目标是为我创建的sql函数创建一个包。 我正在使用EMP和DEPT表( https://livesql.oracle.com/apex/livesql/file/content_O5AEB2HE08PYEPTGCFLZU9YCV.html )

    以下是功能(有效):

    create or replace function PRENOM_EMP (nom in EMPLOYEES.LAST_NAME%type)
    return EMPLOYEES.FIRST_NAME%type
    is prenom EMPLOYEES.FIRST_NAME%type;
    begin
        select FIRST_NAME
        into prenom
        FROM EMPLOYEES
        WHERE last_name = nom;
    return prenom;
    
    EXCEPTION
        when too_many_rows then
        return ('l ordre sql ramene plus d une ligne');
    
    end;
    
    Select PRENOM_EMP ('King') from dual; /* Example of use */
    

    以下是我如何将此函数放入包中的方法:

    create or replace package PKG_EMPLOYES as
    
    function PRENOM_EMP (nom in EMPLOYEES.LAST_NAME%type)
        return EMPLOYEES.FIRST_NAME%type IS
        prenom EMPLOYEES.FIRST_NAME%type;
    begin
        select FIRST_NAME
        into prenom
        FROM EMPLOYEES
        WHERE last_name = nom;
    return prenom;
    
    EXCEPTION
        when too_many_rows then
        return ('l ordre sql ramene plus d une ligne');
    
    end PRENOM_EMP;
    
    end PKG_EMPLOYES;
    

    错误发生在第5行

    prenom EMPLOYEES.FIRST_NAME%type;
    

    SQL开发人员说:“Erreur(34,4):PLS-00103:在预期以下情况之一时遇到符号“PRENOM”:语言”

    我遵循了如何使用此网站上的软件包的示例( https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_6006.htm )但我找不到解决办法。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Kris Rice    7 年前

    您必须将其分解为包规范和包体。

    所以

    create or replace package PKG_EMPLOYES as
    
    function PRENOM_EMP (nom in EMPLOYEES.LAST_NAME%type)
        return EMPLOYEES.FIRST_NAME%type;
    
    end PKG_EMPLOYES;
    /
    

    和主体:

    create or replace package body PKG_EMPLOYES as
    
    function PRENOM_EMP (nom in EMPLOYEES.LAST_NAME%type)
        return EMPLOYEES.FIRST_NAME%type IS
        prenom EMPLOYEES.FIRST_NAME%type;
    begin
        select FIRST_NAME
        into prenom
        FROM EMPLOYEES
        WHERE last_name = nom;
    return prenom;
    
    EXCEPTION
        when too_many_rows then
        return ('l ordre sql ramene plus d une ligne');
    
    end PRENOM_EMP;
    
    end PKG_EMPLOYES;
    /