I try to do the following :
CREATE OR REPLACE PACKAGE my_package AS
-- Declare the global variable
my_variable VARCHAR(255);
END my_package;
/
CREATE OR REPLACE PACKAGE BODY my_package AS
-- Assign a value to the global variable
my_variable := 'test';
END my_package;
/
But this does highlight :=
and throw the following error :
LINE/COL ERROR
--------- -------------------------------------------------------------
3/15 PLS-00103: Encountered the symbol "=" when expecting one of the following: constant exception <an identifier> <a double-quoted delimited-identifier> table columns long double ref char time timestamp interval date binary national character nchar The symbol "<an identifier>" was substituted for "=" to continue.
Errors: check compiler log
How can I create a package with in it a global variable so I can use it as follow :
my_package.my_variable
CodePudding user response:
You are missing a BEGIN block:
CREATE OR REPLACE PACKAGE BODY my_package AS
BEGIN
-- Assign a value to the global variable
my_variable := 10;
END my_package;
/
CodePudding user response:
If you want to use it in SQL then create a function to wrap the variable:
CREATE OR REPLACE PACKAGE my_package AS
FUNCTION get_my_variable
RETURN VARCHAR2;
END my_package;
/
CREATE OR REPLACE PACKAGE BODY my_package AS
-- Declare the package variable
-- It can be public, if declared in the package specification
-- Or private, if declared in the package body
my_variable VARCHAR(255);
FUNCTION get_my_variable
RETURN VARCHAR2
IS
BEGIN
RETURN my_variable;
END;
BEGIN
-- Assign a value to the global variable
my_variable := 'test';
END my_package;
/
Then you can use it in SQL:
SELECT my_package.get_my_variable() FROM DUAL;
Outputs:
MY_PACKAGE.GET_MY_VARIABLE() |
---|
test |