1

我正在准备 Oracle SQL 程序供我的团队使用。其他团队最终也可以通过更改很少的 where 条件来使用它。我们都在使用 Toad for Oracle 来运行查询。所以我在查询中添加了变量。请参阅下面的示例代码。

DECLARE  v_lob    VARCHAR(2);BEGIN  v_lob := 't' ;END;
select :v_lob  as Test from dual

我的问题是当 Toad 有一个弹出窗口选项来绑定变量时。由于我的团队以任何方式使用 v_lob := 't',我希望他们不要在每次查询时都输入它。如何删除弹出窗口选项并使用代码中的变量值“t”?

4

1 回答 1

0

您可能希望使用 aCONTEXT来保存大量会话变量。此解决方案应适用于任何 IDE。

一个用户,一次,必须创建上下文并创建一个用于设置上下文值的包(基于此 oracle-base 文章):

--Create a package on a shared schema.
create or replace package context_api is
    procedure set_parameter(p_name varchar2, p_value varchar2);
end;
/

create or replace package body context_api is
    procedure set_parameter(p_name varchar2, p_value varchar2) is
    begin
        dbms_session.set_context('my_context', p_name, p_value);
    end;
end;
/


--Create a context on a shared database.
--(Note that contexts are global objects and don't belong to a specific schema.)
create or replace context my_context using context_api;

然后,每个用户在他们的工作表中都需要这样的代码:

--At the top of the worksheet, set the variables like this:
begin
    context_api.set_parameter('v_lob', 'asdf');
end;
/


--Call the variables with SYS_CONTEXT:
select sys_context('my_context', 'v_lob') as test from dual;

TEST
----
asdf
于 2020-03-27T01:19:44.747 回答