【问题标题】:Symbol error while creating a package SQL developer创建包 SQL 开发人员时出现符号错误
【发布时间】:2018-02-23 00:39:07
【问题描述】:

我的目标是为我创建的 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) 中的包的示例进行操作,但找不到解决方案。

【问题讨论】:

    标签: sql oracle-sqldeveloper packages


    【解决方案1】:

    你必须把它分解成一个包规范和包体。

    所以..

    规格:

    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;
    /
    

    【讨论】:

    • 所以你先创建规范然后再创建正文?顺便说一句,感谢您的快速回答,它有效
    • 是的,规范是其他人可见的。胆量不是这样,因此那里可能存在永远无法从包外部调用的私有函数。
    • 最后一件事,在网站上,它说要使用包中的函数,我需要执行 PACK_NAME.FUNCT_NAME('...')。如果我在新的 sql 表中执行此操作,它会显示未知命令。我应该如何/在哪里称呼它?
    • 别着急,我找到了答案,我需要做一些pl/sql来使用包
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 2021-11-26
    • 2016-11-04
    • 2019-06-15
    • 2018-04-30
    相关资源
    最近更新 更多