How do I declare a session variable in PL/SQL - one that will persist for the duration of the session only, without me having to store it in the database itself?
You create a package level variable. This is a minimal example:
CREATE OR REPLACE PACKAGE my_package
AS
FUNCTION get_a RETURN NUMBER;
END my_package;
/
CREATE OR REPLACE PACKAGE BODY my_package
AS
a NUMBER(20);
FUNCTION get_a
RETURN NUMBER
IS
BEGIN
RETURN a;
END get_a;
END my_package;
/
If you do this you should read up on (and handle correctly) ORA-04068
errors. Each database session will have it's own value for a. You can try this with:
SELECT my_package.get_a FROM DUAL;