views:

1033

answers:

3

In MySQL, I can create an access a session variable by using a single @. Example initialization:

set @myVar = true;

Some trigger containing this code:

if (@myVar is not true) then
  execute something

What is the equivalent in Oracle 10g?

+2  A: 

A package global variable would probably do the same trick.

CREATE OR REPLACE PACKAGE foo as
  myVar BOOLEAN;
END foo;


CREATE OR REPLACE PACKAGE BODY foo AS
  BEGIN
    MyVar := true;
END foo;


BEGIN
  If foo.myVar THEN 
    dbms_output.put_line ('MyVar is True');
  end if;
END;

An advantage of using the package over SYS_CONTEXT is that you get some encapsulation.

Thomas Jones-Low
+5  A: 
SQL> EXEC DBMS_SESSION.SET_CONTEXT('CLIENTCONTEXT', 'myvar', 'myvalue');

PL/SQL procedure successfully completed

SQL> SELECT SYS_CONTEXT('CLIENTCONTEXT', 'myvar') FROM dual;

SYS_CONTEXT('CLIENTCONTEXT','M
--------------------------------------------------------------------------------
myvalue
Quassnoi
Behaves like a Thread Local in Java right?
JamesC
A: 

Why not just use bind variables? In SQL Plus:

variable SOME_NUMBER number
exec :SOME_NUMBER := 10

PL/SQL procedure successfully completed

if :SOME_NUMBER = 10 then
   do something;
end if;
/

Works for any kind of Oracle datatype.

ropable
the variable needs to be accessible from outside the PL\SQL in a trigger elsewhere.
Joshua