如何动态引用另一个数据库用户?
我有一个案例,我需要引用另一个数据库用户。 我必须在引用数据库用户名时对其进行硬编码。
SELECT * FROM eg001t3.DUAL; // example.
有没有办法从视图动态或基于数据库设置引用该数据库用户(例如001t3)?
I've a case in which I need to refer to another database user. I've to hard code database user name in view while referring to it.
SELECT * FROM eg001t3.DUAL; // example.
Is there a way to refer to that db user (eg001t3) from view dynamically or based on a database setup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 pl/sql 中,您将使用 EXECUTE IMMEDIATE 或 DBMS_SQL 来动态引用对象。
以 EXECUTE IMMEDIATE 为例:
您还可以使用动态构建的 REF CURSOR:
如图所示,您可以使用 DBMS_ASSERT 来验证您的输入。
In pl/sql you would use EXECUTE IMMEDIATE or DBMS_SQL to dynamically reference objects.
Exemple with EXECUTE IMMEDIATE:
You can also use dynamically built REF CURSOR:
As shown you can use DBMS_ASSERT to validate your input.
我添加一个新答案来演示 jva 建议的另一种方法。 所有表必须共享一个公共结构(以便 Oracle 在编译时能够知道视图列的数据类型)。
设置:
然后,您将在包含架构名称的包中定义全局变量,然后查询视图:
I add a new answer to demonstrate another method suggested by jva. All the tables must share a common structure (so that Oracle will be able to know the datatype of the columns of the view at compile time).
Setup:
You would then define the global variable in the package containing the schema name and then query the view:
可能有更优雅的选项,但您可以动态创建同义词或使用动态 SQL / EXECUTE IMMEDIATE。
There may be more elegant options, but you could either create synonyms on the fly or use Dynamic SQL / EXECUTE IMMEDIATE.
另一个可能适合您的选项(取决于您需要执行此操作的应用程序环境)是暂时将命名空间更改为感兴趣的模式:
set current_schema 不会以任何方式绕过 Oracle 权限模型 - 您至少仍然需要在其他模式的表上进行 SELECT出于兴趣。
Another option that might work for you (depending on the application environment from which you need to do this) is to temporarily change your namespace to the schema of interest:
set current_schema doesn't bypass the Oracle privilege model in any way - you'll still need at least SELECT on the other schema's tables of interest.