MySQL equivalent session variable for Oracle

▼魔方 西西 提交于 2019-12-07 15:48:35

问题


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?


回答1:


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



回答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.




回答3:


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.



来源:https://stackoverflow.com/questions/529537/mysql-equivalent-session-variable-for-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!