【问题标题】:What actually PROMPT<?> syntax do in oracle procedure?PROMPT<?> 语法在 oracle 过程中的实际作用是什么?
【发布时间】:2022-11-21 16:20:13
【问题描述】:

那么,在这段代码中,PROMPT 语法到底做了什么?

PROMPT create or replace procedure abc (p_name, p_id)
AUTHID CURRENT_USER
as
begin
 dbms_output.put_line('hi');
end;

【问题讨论】:

    标签: oracle stored-procedures plsql oracle-sqldeveloper


    【解决方案1】:

    如果您在 SQL*Plus 中运行一些代码,PROMPT 就有意义(现在没有多少人这样做)。它显示该关键字后面的文本。

    SQL> help prompt
    
     PROMPT
     ------
    
     Sends the specified message or a blank line to the user's screen.
    
     PRO[MPT] [text]
    
    
    SQL> prompt hello there!
    hello there!
    SQL>
    

    在您的情况下,它会产生不需要的结果,因为它显示 create procedure(而不是创建它):

    SQL> PROMPT create or replace procedure abc (p_name, p_id)
    create or replace procedure abc (p_name, p_id)
    SQL> AUTHID CURRENT_USER
    SP2-0734: unknown command beginning "AUTHID CUR..." - rest of line ignored.
    SQL> as
    SP2-0042: unknown command "as" - rest of line ignored.
    SQL> begin
      2   dbms_output.put_line('hi');
      3  end;
      4  /
    hi
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    得到结果,但就像纯粹的意外一样

    begin
      dbms_output.put_line('hi');
    end;
    

    是一个有效的 PL/SQL 块。


    您发布的代码(没有prompt)无效:

    SQL> create or replace procedure abc (p_name, p_id)
      2  AUTHID CURRENT_USER
      3  as
      4  begin
      5   dbms_output.put_line('hi');
      6  end;
      7  /
    
    Warning: Procedure created with compilation errors.
    
    SQL> show err
    Errors for PROCEDURE ABC:
    
    LINE/COL ERROR
    -------- -----------------------------------------------------------------
    1/22     PLS-00103: Encountered the symbol "," when expecting one of the
             following:
             in out <an identifier> <a double-quoted delimited-identifier>
             ... long double ref char time timestamp interval date binary
             national character nchar
    
    3/1      PLS-00103: Encountered the symbol "AS" when expecting one of the
             following:
             with authid cluster order deterministic parallel_enable
             pipelined result_cache
    
    6/4      PLS-00103: Encountered the symbol "end-of-file" when expecting
             one of the following:
             end not pragma final instantiable order overriding static
             member constructor map
    
    SQL>
    

    这是什么意思?过程的参数必须具有数据类型:

    SQL> create or replace procedure abc (p_name in varchar2, p_id in number)
      2  AUTHID CURRENT_USER
      3  as
      4  begin
      5   dbms_output.put_line('hi');
      6  end;
      7  /
    
    Procedure created.
    
    SQL> exec abc(null, null);
    hi
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    【讨论】:

      【解决方案2】:

      PROMPT 不是 pl/sql,它是一个 sqlplus 命令,也可以在其他 oracle 客户端工具(如 sql developer 和 sqlcl)中实现。 我个人在带有许多命令/匿名 pl/sql 块的长脚本中使用它来指示当前正在执行脚本的哪一部分。如果脚本出错,我可以很容易地看到出错的地方。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        • 1970-01-01
        • 2019-05-31
        • 2019-05-04
        • 1970-01-01
        • 1970-01-01
        • 2019-05-03
        相关资源
        最近更新 更多