1

我写了以下包和包体:

create or replace package discounts
is
g_id number := 7839;
discount_rate number := 0.0;
procedure display_price(p_price number);
end;
/
create or replace package body discounts
is
procedure display_price(p_price number)
is
  begin
     dbms_output.put_line('Discount rate 1:'||discount_rate); 
     dbms_output.put_line('Discounted '||to_char(p_price * nvl(discount_rate,0)));
     dbms_output.put_line('Discount rate 2:'||discount_rate); 
  end;
begin
  dbms_output.put_line('Discount rate 3:'||discount_rate); 
  discount_rate := 0.10;
  dbms_output.put_line('Discount rate 4:'||discount_rate); 
end;
/

上面写着“在会话中第一次调用包时,discount_rate 的值设置为 0.10”。我没有完全理解这一点,这就是为什么我每次都检查贴现率的值。我键入以下内容来调用:

SQL> execute discounts.display_price(1000);
Discount rate 3:0
Discount rate 4:.1
Discount rate 1:.1
Discounted 100
Discount rate 2:.1

然后我再次调用了变量:

  begin
    dbms_output.put_line('Discount rate :'||discounts.discount_rate);
  end;
  SQL> /
  Discount rate :.1

然后我键入“exit”关闭 SQL *PLUS。我再次打开 SQL *PLUS 并输入相同的代码:

  begin
    dbms_output.put_line('Discount rate :'||discounts.discount_rate);
  end;

我认为它不会初始化变量,但我得到了错误:

  ERROR at line 3:
  ORA-06550: line 3, column 1:
  PLS-00103: Encountered the symbol "END" when expecting one of the following:
  := . ( % ;
  The symbol ";" was substituted for "END" to continue.

错误是什么?我是准备认证考试的 PL/SQL 新手。

4

1 回答 1

1

抱歉,我无法重现您显示的错误:

C:\>sqlplus user/password

SQL*Plus: Release 11.2.0.2.0 Production on Sat Mar 23 11:31:19 2013

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production

SQL> set serveroutput on
SQL> execute discounts.display_price(1000);
Discount rate 3:0
Discount rate 4:.1
Discount rate 1:.1
Discounted 100
Discount rate 2:.1

PL/SQL procedure successfully completed.

SQL> begin
  2    dbms_output.put_line('Discount rate :'||discounts.discount_rate);
  3  end;
  4  /
Discount rate :.1

PL/SQL procedure successfully completed.

SQL> exit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production

C:\>sqlplus user/password

SQL*Plus: Release 11.2.0.2.0 Production on Sat Mar 23 11:31:50 2013

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production

SQL> set serveroutput on
SQL> begin
  2    dbms_output.put_line('Discount rate :'||discounts.discount_rate);
  3  end;
  4  /
Discount rate 3:0
Discount rate 4:.1
Discount rate :.1

PL/SQL procedure successfully completed.

SQL>
于 2013-03-23T11:34:52.390 回答