Use function to initialize the package level variable : Package Variables « Function Procedure Packages « Oracle PL/SQL Tutorial






SQL>
SQL> CREATE TABLE keepcount (counter INTEGER);

Table created.

SQL>
SQL> INSERT INTO keepcount VALUES (1);

1 row created.

SQL>
SQL> CREATE OR REPLACE FUNCTION updcount RETURN INTEGER
  2  IS
  3  BEGIN
  4     UPDATE keepcount SET counter = counter + 1;
  5     RETURN 1;
  6  END;
  7  /

Function created.

SQL>
SQL> CREATE OR REPLACE PACKAGE demo
  2  AS
  3     PRAGMA SERIALLY_REUSABLE;
  4     global_x PLS_INTEGER := updcount;
  5  END;
  6  /

Package created.

SQL>
SQL> CREATE OR REPLACE PACKAGE demo2
  2  AS
  3     PRAGMA SERIALLY_REUSABLE;
  4     FUNCTION global_x RETURN PLS_INTEGER;
  5  END;
  6  /

Package created.

SQL> CREATE OR REPLACE PACKAGE BODY demo2
  2  AS
  3     PRAGMA SERIALLY_REUSABLE;
  4     g_global_x PLS_INTEGER := updcount;
  5     FUNCTION global_x RETURN PLS_INTEGER
  6        IS BEGIN RETURN g_global_x; END;
  7  END;
  8  /

Package body created.

SQL>
SQL> DROP TABLE keepcount;

Table dropped.

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