PL/SQL: How do I declare session variables?

前端 未结 3 543
眼角桃花
眼角桃花 2021-02-04 05:18

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?

3条回答
  •  时光说笑
    2021-02-04 05:27

    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;
    

提交回复
热议问题