【问题标题】:PLSQL Prodecure to Select a single row from a table produces error PLS-00103PLSQL Prodecure 从表中选择单行会产生错误 PLS-00103
【发布时间】:2020-01-15 19:22:45
【问题描述】:

我的代码目标是造成幻影行。我理解这个过程,但我正在努力让我的第一个 sql 脚本运行

CREATE OR REPLACE PROCEDURE PHANTOM

IS

DECLARE 
ENAME CHAR;
ENAME EMPLOYEE%ROWTYPE;

BEGIN

SELECT * INTO ENAME
FROM EMPLOYEE
WHERE NAME = 'Albert';

DBMS_OUTPUT.PUT_LINE(ENAME.NAME);

DBMS_LOCK.SLEEP(15);

DBMS_OUTPUT.PUT_LINE(ENAME.NAME);

 END;
 /

这是我的第一个脚本,其中声明了一个 char 类型的变量 ENAME,并从名称等于 Albert 的表 EMPLOYEE 中获取 NAME 的值。然后在 DMBS 语句中调用该变量以返回名称。我的第二个脚本仅包含更新和提交以完成幻像过程。

当我尝试编译这个程序时,我得到了两个错误

错误 1

Error(5,1): PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following:     begin function pragma procedure subtype type <an identifier>    <a double-quoted delimited-identifier> current cursor delete    exists prior external language The symbol "begin" was substituted for "DECLARE" to continue. 

错误 2

Error(21,5): PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:     ( begin case declare end exception exit for goto if loop mod    null pragma raise return select update while with    <an identifier> <a double-quoted delimited-identifier>    <a bind variable> << continue close current delete fetch lock    insert open rollback savepoint set sql execute commit forall    merge pipe purge json_exists json_value json_query    json_object json_array 

我该如何解决这个问题?谢谢你

【问题讨论】:

    标签: sql plsql procedure


    【解决方案1】:

    你离这里并不远。一个问题是您如何编写 SQL 脚本和如何编写 PL/SQL 过程之间的语法混淆。

    如果你想要一个独立的脚本,即不存储代码供以后重复使用,它只运行一次,执行一个动作,然后消失,它是

    DECLARE
    count_of_apples NUMBER;
    BEGIN
    ...
    END;
    /
    

    如果你想定义一个可以重复运行多次的过程,它是

    CREATE OR REPLACE PROCEDURE phantom IS
    -- don't need to say DECLARE, it's implied
    count_of_apples NUMBER;
    BEGIN
    ...
    END phantom; -- good practice to add the name again here
    /
    

    然后你会

    exec phantom;
    

    运行该程序。

    另一个问题是你已经两次声明了同一个变量(ENAME),有两种不同的类型,你不能这样做。不过,您不需要 CHAR 行,因此您可以删除该行,就可以了。

    【讨论】:

    • 对于触发器,您也使用声明,顺便说一句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多