ORA-00942:表或视图不存在(在单独的sql时有效,但在oracle函数内无效)

发布于 2024-11-19 17:15:37 字数 169 浏览 7 评论 0原文

当我有像 select * from table1 这样的 sql 语句时,它工作得很好,但是一旦我将它放入函数中,我就会得到:

ORA-00942: table or view does not exist 

如何解决这个问题?

When I have a sql statement like select * from table1, it works great, but as soon as I put it into a function, I get:

ORA-00942: table or view does not exist 

How to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

对你的占有欲 2024-11-26 17:15:37

从 table1 中进行选择的权限很可能已被授予某个角色,并且该角色已被授予您。即使用户已被授予角色,授予角色的权限也不可用于用户编写的 PL/SQL。

对于已被授予 sys 拥有的对象的 dba 角色的用户,您会经常看到这种情况。具有 dba 角色的用户将能够执行 SELECT * from V$SESSION 等操作,但无法编写包含 SELECT * FROM V$SESSION 的函数。

解决方法是直接向用户授予对相关对象的显式权限,例如,在上述情况下,SYS 用户必须GRANT SELECT ON V_$SESSION TO MyUser;

There's a strong chance that the privileges to select from table1 have been granted to a role, and the role has been granted to you. Privileges granted to a role are not available to PL/SQL written by a user, even if the user has been granted the role.

You see this a lot for users that have been granted the dba role on objects owned by sys. A user with dba role will be able to, say, SELECT * from V$SESSION, but will not be able to write a function that includes SELECT * FROM V$SESSION.

The fix is to grant explicit permissions on the object in question to the user directly, for example, in the case above, the SYS user has to GRANT SELECT ON V_$SESSION TO MyUser;

岁吢 2024-11-26 17:15:37

您可以查看以下几件事。根据您的问题,函数所有者似乎与表所有者不同。

1) 通过角色授予:为了在另一个用户的对象上创建存储过程和函数,您需要直接访问对象(而不是通过角色访问)。

2)

默认情况下,存储过程和 SQL 方法使用
其所有者的权限,而不是当前用户的权限。

如果您在模式 A 中创建了表并在模式 B 中创建了函数,则应查看 Oracle 的调用者/定义者权限概念,以了解可能导致问题的原因。

http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/subprograms.htm#LNPLS00809

There are a couple of things you could look at. Based on your question, it looks like the function owner is different from the table owner.

1) Grants via a role : In order to create stored procedures and functions on another user's objects, you need direct access to the objects (instead of access through a role).

2)

By default, stored procedures and SQL methods execute with the
privileges of their owner, not their current user.

If you created a table in Schema A and the function in Schema B, you should take a look at Oracle's Invoker/Definer Rights concepts to understand what might be causing the issue.

http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/subprograms.htm#LNPLS00809

-残月青衣踏尘吟 2024-11-26 17:15:37

确保该函数与表位于相同的数据库架构中。

Make sure the function is in the same DB schema as the table.

烛影斜 2024-11-26 17:15:37

要么您没有该架构/表的权限,要么表确实存在。
大多数情况下,如果您在存储过程中使用其他架构表,就会出现此问题。
例如。如果您从用户/模式 ABC 运行存储过程,并且在同一 PL/SQL 中,存在来自用户/模式 XYZ 的表。在这种情况下,ABC应该具有GRANT,即XYZ表的权限

Grant All On To ABC;

Select * From Dba_Tab_Privs Where Owner = 'XYZ'and Table_Name = <Table_Name>;

Either u dont have permission to that schema/table OR table does exist.
Mostly this issue occurred if you are using other schema tables in your stored procedures.
Eg. If you are running Stored Procedure from user/schema ABC and in the same PL/SQL there are tables which is from user/schema XYZ. In this case ABC should have GRANT i.e. privileges of XYZ tables

Grant All On To ABC;

Select * From Dba_Tab_Privs Where Owner = 'XYZ'and Table_Name = <Table_Name>;
并安 2024-11-26 17:15:37

一个非常简单的解决方案是添加数据库名称和表名称,例如如果您的数据库名称是 DBMS,表是 info,那么它将是 DBMS.info< /code> 任何查询。

如果您的查询是,

select * from STUDENTREC where ROLL_NO=1;

它可能会显示错误,但

select * from DBMS.STUDENTREC where ROLL_NO=1; 

它不会,因为现在实际上找到了您的表。

A very simple solution is to add the database name with your table name like if your DB name is DBMS and table is info then it will be DBMS.info for any query.

If your query is

select * from STUDENTREC where ROLL_NO=1;

it might show an error but

select * from DBMS.STUDENTREC where ROLL_NO=1; 

it doesn't because now actually your table is found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文