oracle 如何从函数内部确定 PL/SQL 函数的模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1034731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:27:15  来源:igfitidea点击:

How to determine a PL/SQL function's schema from within the function

oracleplsql

提问by Jeremy Bourque

I have a PL/SQL package in an Oracle 10g database and I want to write a function that returns the name of the schema that the package (and hence the function) is defined in. Anyone know how to do this?

我在 Oracle 10g 数据库中有一个 PL/SQL 包,我想编写一个函数来返回定义包(以及函数)的模式的名称。有人知道怎么做吗?

采纳答案by Gary Myers

create function xcurr return varchar2 is
  v_curr varchar2(32);
begin
  SELECT SYS_CONTEXT ('USERENV', 'CURRENT_USER') into v_curr from dual;
  return v_curr;
end;

This will work as long as the PL/SQL object doesn't have AUTHID CURRENT_USER.

只要 PL/SQL 对象没有 AUTHID CURRENT_USER,这就会起作用。

回答by Tom W.

From Oracle 10g CURRENT_USER, as used in Gary Myers' answer, is deprecated. Oracle recommends that you use the SESSION_USER parameter instead, which:

从 Oracle 10g CURRENT_USER 开始,如Gary Myers 的回答中所用,已被弃用。Oracle 建议您改用 SESSION_USER 参数,它:

For enterprises users, returns the schema. For other users, returns the database user name by which the current user is authenticated. This value remains the same throughout the duration of the session.

对于企业用户,返回架构。对于其他用户,返回对当前用户进行身份验证的数据库用户名。该值在整个会话期间保持不变。

I would use CURRENT_SCHEMA. There are subtle differences between the two as CURRENT_SCHEMA changes if an ALTER SESSION SET CURRENT_SCHEMAstatement is issued.

我会使用 CURRENT_SCHEMA。如果发出ALTER SESSION SET CURRENT_SCHEMA语句,CURRENT_SCHEMA 会发生变化,因此两者之间存在细微差别。

There's also no need to SELECT from dual; you can assign the return value of SYS_CONTEXT directly to the variable.

也不需要从双重选择;您可以将 SYS_CONTEXT 的返回值直接分配给变量。

DECLARE
    v_current_schema varchar2(30);
BEGIN
    v_current_schema := SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA');
    dbms_output.put_line('I am using the schema [' || v_current_schema || ']');
END;

回答by darreljnz

There is probably an easier way but you could use dbms_utility.format_call_stackand parse the results to get the schema name. This works in Oracle 9i.

可能有一种更简单的方法,但您可以使用dbms_utility.format_call_stack并解析结果来获取架构名称。这适用于 Oracle 9i。