使用 SQL 查询确定表是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4385346/
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-01 08:31:21  来源:igfitidea点击:

Using SQL query to determine if a table exists

sqloracle

提问by Dead Programmer

Guys is there any other way to determine a table exists other than below

伙计们,除了下面之外,还有其他方法可以确定表是否存在

  1. select count(*) from <table> where rownum =1
  2. select * from user_table where table_name=<table>
  1. select count(*) from <table> where rownum =1
  2. select * from user_table where table_name=<table>

kindly let me know the best way to check whether a table exists using oracle sql.

请让我知道使用 oracle sql 检查表是否存在的最佳方法。

Thanks for the answer , my requirement is to check from the first date of current month ie 01/12/2010 with table name in the format suresh_20101201 exists in the database, if not then it should check for table suresh_20101202 and thereon till suresh_20101231 . is it possible to do in oracle sql query.

感谢您的回答,我的要求是从当月的第一个日期开始检查,即 01/12/2010,表名格式为suresh_20101201 存在于数据库中,如果没有,则应检查表suresh_20101202 并在其上检查suresh_20101231。是否可以在 oracle sql 查询中进行。

回答by Dainius

You can do this (in oracle, in mssql there is a bit different):

你可以这样做(在 oracle 中,在 mssql 中有点不同):

select count(*)
from all_objects
where object_type in ('TABLE','VIEW')
and object_name = 'your_table_name';

回答by wallyk

In most sql servers there is a system domain where you can query for a table's existence. It's highly implementation specific though. For example, in recent versions of MySql:

在大多数 sql 服务器中有一个系统域,您可以在其中查询表的存在。不过,它是高度特定于实现的。例如,在最新版本的MySql 中

SELECT table_name FROM INFORMATION_SCHEMA.TABLES
  WHERE table_schema = 'db_name'
  AND table_name LIKE 'whatever'

回答by UserControl

You need to ask your server's system catalog. Not sure what database you meant but for SQL Server it would be:

您需要询问服务器的系统目录。不确定您的意思是什么数据库,但对于 SQL Server,它将是:

select * from sys.tables where name='your-table-name-'

回答by vapcguy

Used this in Oracle SQL Developer:

在 Oracle SQL Developer 中使用这个:

SELECT COUNT(*) FROM DUAL WHERE EXISTS (
    SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OWNER = 'myschema' AND OBJECT_NAME = 'your_table_name')

This will return either a 0or 1if your table exists or not in the ALL_OBJECTSrecords.

这将返回 a 0or1如果您的表在ALL_OBJECTS记录中存在与否。

回答by Neeraj

Below query can be triggered to Oracle for checking whether any Table present in DB or not:

可以向 Oracle 触发以下查询以检查数据库中是否存在任何表:

SELECT count(*) count FROM dba_tables where table_name = 'TABLE_NAME'

Above query will return count 1 if table 'TABLE_NAME' is present in Database

如果数据库中存在表“TABLE_NAME”,则上述查询将返回计数 1

回答by brumScouse

Look in the schema, might event be able to use sys.objects and check for a type at the same time.....

查看模式,可能事件能够使用 sys.objects 并同时检查类型.....

Something like

就像是