Test unit for package scopes : Package Variables « Function Procedure Packages « Oracle PL/SQL Tutorial






SQL>
SQL>
SQL> create or replace package SCOPES as
  2  gv_scope                              varchar2(80) :=
  3    'I''m a global (or package spec) variable';
  4
  5  FUNCTION my_scope_is_global
  6  return                                varchar2;
  7
  8  PROCEDURE my_scope_is_global;
  9
 10  end SCOPES;
 11  /

Package created.

SQL>
SQL>
SQL> create or replace package body SCOPES as
  2  iv_scope varchar2(80) := 'an instance variable';
  3
  4
  5  FUNCTION my_scope_is_instance
  6  return varchar2 is
  7  v_answer_1 varchar2(3) := 'Yes';
  8  begin
  9    dbms_output.put_line(chr(9)||gv_scope);
 10    return v_answer_1;
 11  end my_scope_is_instance;
 12
 13
 14  FUNCTION my_scope_is_global
 15  return varchar2 is
 16  v_answer_2 varchar2(3) := 'Yes';
 17  begin
 18    dbms_output.put_line(chr(9)||iv_scope);
 19    return v_answer_2;
 20  end my_scope_is_global;
 21
 22
 23  PROCEDURE my_scope_is_instance is
 24  v_answer_3 varchar2(3) := 'Yes';
 25  begin
 26    dbms_output.put_line(chr(9)||gv_scope);
 27    dbms_output.put_line(v_answer_3);
 28  end my_scope_is_instance;
 29
 30
 31  PROCEDURE my_scope_is_global is
 32  v_answer_4 varchar2(3) := 'Yes';
 33  begin
 34    dbms_output.put_line(chr(9)||iv_scope);
 35    dbms_output.put_line(v_answer_4);
 36  end my_scope_is_global;
 37
 38
 39  end SCOPES;
 40  /

Package body created.

SQL>
SQL>
SQL>
SQL> declare
  2      v_scope varchar2(40) := 'I''m a local variable';
  3      FUNCTION my_scope_is_local
  4      return varchar2 is
  5          v_answer_0 varchar2(3) := 'Yes';
  6      begin
  7        return v_answer_0;
  8      end my_scope_is_local;
  9
 10      PROCEDURE my_scope_is_local is
 11          v_answer varchar2(3) := 'Yes';
 12      begin
 13        dbms_output.put_line(v_answer);
 14      end my_scope_is_local;
 15  begin
 16
 17    dbms_output.put_line(v_scope);
 18
 19    dbms_output.put_line(SCOPES.gv_scope);
 20
 21    dbms_output.put_line(my_scope_is_local());
 22
 23    dbms_output.put_line(SCOPES.my_scope_is_global());
 24
 25    my_scope_is_local();
 26
 27    SCOPES.my_scope_is_global();
 28
 29
 30  end;
 31  /
I'm a local variable
I'm a global (or package spec) variable
Yes
        an instance variable
Yes
Yes
        an instance variable
Yes

PL/SQL procedure successfully completed.

SQL>
SQL>








27.12.Package Variables
27.12.1.Package constant variable
27.12.2.Cursor variable in a package
27.12.3.Serially Reusable Packages
27.12.4.Test unit for package scopes
27.12.5.Define constant in a package
27.12.6.Package level cursor variable
27.12.7.Use function to initialize the package level variable
27.12.8.Pre-filled table collection of varchars in a package
27.12.9.Private field
27.12.10.Use package to define variable and use across code blocks
27.12.11.Demonstrate using a packaged ref cursor for passing sets