快速判断ORACLE表中某个字段是否存在的方法

发布于 2024-08-21 13:53:10 字数 440 浏览 4 评论 0原文

我正在寻找一个快速的 SQL 语句来确定表中何时存在某个字段。

其实我用这句话

Select 1 
   from dual
   where exists (select 1 
                   from all_tab_columns 
                  where table_name = 'MYTABLE' 
                    and column_name = 'MYCOLUMN')

我想一定有一个最快的方法来判断ORACLE中是否存在某个列。

更新

我正在优化一个更大的软件系统,该系统会多次调用此查询,我无法修改源代码;(,只有我可以修改存储在外部文件中的查询。

表all_tab_columns 拥有超过一百万条记录。

I am looking for a fast sql sentence for determine when a field exist or not in a table .

actually i am using this sentence

Select 1 
   from dual
   where exists (select 1 
                   from all_tab_columns 
                  where table_name = 'MYTABLE' 
                    and column_name = 'MYCOLUMN')

I think there must be a fastest way to determine whether or not a column exist in ORACLE.

UPDATE

I'm optimizing a larger software system that makes multiple calls to this Query, I can not modify the source code ;( , only i can modify the query which is stored in an external file.

the Table all_tab_columns has over a million of records.

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

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

发布评论

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

评论(6

如果没结果 2024-08-28 13:53:10

all_tab_columns 的主键是 owner、table_name、column_name,因此查找特定所有者会更快(或使用 user_tab_columns)。

the primary key of all_tab_columns is owner, table_name, column_name so looking for a particular owner will be faster (or use user_tab_columns).

因为看清所以看轻 2024-08-28 13:53:10

我建议阅读这篇 AskTom 文章。它解释说,最快的检查方法就是根本不检查。

http://asktom.oracle .com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:698008000346356376

I suggest reading this AskTom article. It explains that the fastest way to check is not to check at all.

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:698008000346356376

风蛊 2024-08-28 13:53:10

查询 Oracle 数据字典 - 正如您的示例所示,可能是最快的方法。

数据字典缓存在内存中,应该能够很快满足查询。如果您知道表的实际架构所有者,您可能会获得稍微更快的结果 - 这样您就不会产生针对所有架构进行搜索的成本。

Querying the Oracle data dictionary - as you example indeed does, is probably the fastest way.

The data dictionary is cached in memory and should be able to satisfy the query pretty quickly. You may be able to get slightly faster results if you know the actual schema owner of the table - so that you don't incur the cost of searching against all schemas.

亽野灬性zι浪 2024-08-28 13:53:10

此查询就足够了:

 SELECT null
  FROM user_tab_columns
 WHERE table_name = 'MYTABLE' and column_name = 'MYCOLUMN'

唯一最快的方法是直接从内部表查询,这不是推荐的方法,并且您需要对 sys 对象进行授权:

select null
from sys.col$ c
   , sys.obj$ o
   , sys.obj$ ot
where o.name = 'MYTABLE'
  and c.name = 'MYCOLUMN'
  and o.obj# = c.obj#
  and o.owner# = userenv('SCHEMAID')
  and ot.type#(+) = 13
  and (o.type# in (3, 4)                                    
       or
       (o.type# = 2 
        and
        not exists (select null
                      from sys.tab$ t
                     where t.obj# = o.obj#
                       and (bitand(t.property, 512) = 512 or
                            bitand(t.property, 8192) = 8192))))

此查询取自USER_TAB_COLUMNS 定义,并且它可以在不同版本中更改(我的情况是 10gR2)。在此查询中,我删除了对您未请求的信息的引用。

无论如何,你为什么要检查这个?

This query is enough:

 SELECT null
  FROM user_tab_columns
 WHERE table_name = 'MYTABLE' and column_name = 'MYCOLUMN'

The only fastest way is to query directly from the internal tables which is not a recommended way and you need grants over sys objects:

select null
from sys.col$ c
   , sys.obj$ o
   , sys.obj$ ot
where o.name = 'MYTABLE'
  and c.name = 'MYCOLUMN'
  and o.obj# = c.obj#
  and o.owner# = userenv('SCHEMAID')
  and ot.type#(+) = 13
  and (o.type# in (3, 4)                                    
       or
       (o.type# = 2 
        and
        not exists (select null
                      from sys.tab$ t
                     where t.obj# = o.obj#
                       and (bitand(t.property, 512) = 512 or
                            bitand(t.property, 8192) = 8192))))

This query is taken from the USER_TAB_COLUMNS definition and it can change over different releases (10gR2 on my case). On this query I've cut the references to information not requested by you.

Anyway, why do you want to check this?

空城缀染半城烟沙 2024-08-28 13:53:10

此 SQL 查询将为用户“DSGIDEV”提供具有列“NAVIGATION_ID”的所有表的名称

select * from all_tab_cols where column_name = 'NAVIGATION_ID' andowner = 'DSGIDEV'

因此,更改列名称包含您要搜索的列和所有者以及您的所有者 ID 名称。

This SQL Query will give name of all the table having column 'NAVIGATION_ID' for the user 'DSGIDEV'

select * from all_tab_cols where column_name = 'NAVIGATION_ID' and owner = 'DSGIDEV'

So, Change the column name with column you want to search and owner with your owner Id name.

浮生未歇 2024-08-28 13:53:10

嗯,最快的方法就是创建这样的函数:

  Create function exist(v_table in varchar2, v_col in  varchar2) 
 Return integer is
 Res integer:= 0;
 Begin
   Begin
      Execute immediate 'select ' || v_col || ' from '|| v_table;         
      Res:=1;
      Exception when other then null;
   End;
Return (res);
End;

Ez, fastest way is just create function like this:

  Create function exist(v_table in varchar2, v_col in  varchar2) 
 Return integer is
 Res integer:= 0;
 Begin
   Begin
      Execute immediate 'select ' || v_col || ' from '|| v_table;         
      Res:=1;
      Exception when other then null;
   End;
Return (res);
End;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文