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?

Quassnoi
  • 413,100
  • 91
  • 616
  • 614
Joshua
  • 26,234
  • 22
  • 77
  • 106

3 Answers3

6
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
  • 413,100
  • 91
  • 616
  • 614
2

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.

WW.
  • 23,793
  • 13
  • 94
  • 121
Thomas Jones-Low
  • 7,001
  • 2
  • 32
  • 36
0

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
  • 1,547
  • 1
  • 19
  • 31