如何使用 Oracle 廉价地验证另一模式中表中的列是否存在?

发布于 2024-08-09 14:02:47 字数 793 浏览 6 评论 0原文

环境是Oracle 9& 10. 我没有 DBA 级别的访问权限。

问题是验证另一个模式中的特定表中是否存在特定列。

有两种情况需要处理。

  1. 同一实例中的另一个模式
  2. 不同实例中的模式,使用 db_link

给定我的模式 FRED 和另一个模式 BARNEY,我尝试了类似的操作

SELECT 1
FROM BARNEY.USER_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE' 
  AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'

,结果是 [1]: (Error): ORA-00942: table or view不存在

经过一段时间的思考,我意识到 USER_TAB_COLS 并不是真正的表。这是一种观点。我一直从表中进行选择,但不是从视图中进行选择。

我对 db_link 尝试了同样的操作,并惊讶地看到数据返回。 db_link 中有一个嵌入的 schema_name/password,所以对我来说它的工作似乎是合理的,因为它有效地登录到其他模式,这应该使视图可访问。

在谷歌上到处搜索,并在 Oracle 文档山上耗尽了我的眼球, 我正在寻找有人指出我正确的方向,或者至少指出我所缺少的东西。

有哪些技术可用于从同一实例中的架构中获取与用户表相关的元数据,以验证特定列是否存在?

提前致谢。

邪恶的。

好的答案+1。 谢谢。

The environment is Oracle 9 & 10. I do not have DBA level access.

The problem is to verify that a specific column exists in a specific table, in another schema.

There are two cases to deal with.

  1. Another schema in the same instance
  2. A schema in a different instance, using a db_link

Given my schema FRED and another schema BARNEY, I tried something like this

SELECT 1
FROM BARNEY.USER_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE' 
  AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'

Which yielded [1]: (Error): ORA-00942: table or view does not exist

After vegging on this awhile, I realized that USER_TAB_COLS, is not really a table. It is a view. I have been selecting from tables all along, but not from a view.

I tried the same thing with my db_link, and was surprised to see data come back. A db_link has an embedded schema_name/password in it, so it seems reasonable to me that it worked, as it effectively logs in to the other schema, which should make the views reachable.

Having Googled around, and worn out my eyeballs on on the mountain of Oracle doc,
I am looking for someone to point me in the correct direction, or at least point out what I am missing.

What techniques are available for getting user table related metadata from a schema in the same instance in order to validate that a specific column exists?

Thanks in advance.

Evil.

+1 for good answers.
Thank you.

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

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

发布评论

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

评论(4

征棹 2024-08-16 14:02:47

您可以使用以下查询:

SELECT 1
FROM ALL_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE' 
  AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'
  AND OWNER = 'BARNEY';

(User_Tables 和 User_Tab_Cols 只是 all_tables 和 all_tab_coumns 上的视图,并附有 whereowner =

如果您被允许查看 Barney 的 some_table (即您已被授予至少 SELECT 权限),然后您就会知道该列是否存在。如果您没有该表的权限,您将无法获取其元信息。

You can use the following query:

SELECT 1
FROM ALL_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE' 
  AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'
  AND OWNER = 'BARNEY';

(User_Tables and User_Tab_Cols are just views on all_tables and all_tab_coumns with a where owner = <Current User> attached to it)

If you're allowed to see the Barney's some_table (i.e. you have been GRANTed at least SELECT privileges on it), then you'll know if the column is there. If you have no rights on the table, you won't be able to get meta information on it.

究竟谁懂我的在乎 2024-08-16 14:02:47

与其他回复一样,通常我使用 ALL_TAB_COLUMNS 进行这样的查询。但这只会显示您有 SELECT 的表中的列。它是在该列上进行选择的——万一他们已经为该表实现了列级权限,您可能可以看到该表,但看不到感兴趣的特定列。对于我们大多数人来说,这是极其罕见的。

DBA_TAB_COLUMNS 将显示所有列,但您需要选择由 DBA 授予您的架构的列。 (实际上,您需要获得 ALL_TAB_COLUMNS 的授权才能使用它,但这在大多数商店中很常见)。也可以使用 DBMS_METADATA PL/SQL 内置包,具有类似的限制,但我认为您会发现它更复杂。

当然,您也可以尝试从 barney.some_table.some_column@my_dblink (或您感兴趣的任何部分)中选择一条记录。然后处理异常。丑陋,在大多数情况下我不会推荐它。

As with the other replies, normally I use ALL_TAB_COLUMNS for a query like this. But that will only show columns in tables where you have SELECT. And it's select on that column -- in the unlikely event that they've implemented column-level privileges for that table, you may be able to see the table, but not see the specific column of interest. For most of us, that's extremely rare.

DBA_TAB_COLUMNS will show all columns, but you'll need select on it granted to your schema by your DBA. (Actually, you'll need a grant on ALL_TAB_COLUMNS to use it, but that's common in most shops). The DBMS_METADATA PL/SQL Built-in package can also be used, with similar limitations, but I think you'll find it more complicated.

Of course, you can also just try to select a record from barney.some_table.some_column@my_dblink (or whatever pieces of that you're interested in). And then handle the exception. Ugly, I wouldn't recommend it in most situations.

时光清浅 2024-08-16 14:02:47

您可以使用 all_tab_columns 来实现此目的。

但请注意,您只能看到允许看到的内容。

You would use all_tab_columns for that.

But beware that you'll only see what you are allowed to see.

凹づ凸ル 2024-08-16 14:02:47

相同的实例,不同的模式:

Select Count(*)
From   all_tab_cols
Where  owner       = 'BARNEY'               and
       table_name  = 'SOME_TABLE'           and
       column_name = 'SOME_SPECIFIC_COLUMN';

count(*) 的优点是始终返回值为 1 或 0 的单行,因此您不必处理 PL/SQL 中的 NO_DATA_FOUND 错误。

跨 DB Link,与您连接的架构相同:

Select Count(*)
From   user_tab_cols@MY_DB_LINK
Where  table_name  = 'SOME_TABLE'           and
       column_name = 'SOME_SPECIFIC_COLUMN';

跨 DB Link,与您连接的架构不同:

Select Count(*)
From   all_tab_cols@MY_DB_LINK
Where  owner       = 'BARNEY'               and
       table_name  = 'SOME_TABLE'           and
       column_name = 'SOME_SPECIFIC_COLUMN';

Same instance, different schema:

Select Count(*)
From   all_tab_cols
Where  owner       = 'BARNEY'               and
       table_name  = 'SOME_TABLE'           and
       column_name = 'SOME_SPECIFIC_COLUMN';

The count(*) has the advantage of always returning a single row with a value of either 1 or 0, so you do not have to deal with NO_DATA_FOUND errors in PL/SQL.

Across a DB Link, same schema as the one you connect as:

Select Count(*)
From   user_tab_cols@MY_DB_LINK
Where  table_name  = 'SOME_TABLE'           and
       column_name = 'SOME_SPECIFIC_COLUMN';

Across a DB Link, different schema than the one you connect as:

Select Count(*)
From   all_tab_cols@MY_DB_LINK
Where  owner       = 'BARNEY'               and
       table_name  = 'SOME_TABLE'           and
       column_name = 'SOME_SPECIFIC_COLUMN';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文