Using SQL query to determine if a table exists

前端 未结 6 1856
清歌不尽
清歌不尽 2020-12-31 06:06

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

  1. select count(*) from where rownum =1
  2. select
    相关标签:
    6条回答
    • 2020-12-31 06:30

      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:

      SELECT table_name FROM INFORMATION_SCHEMA.TABLES
        WHERE table_schema = 'db_name'
        AND table_name LIKE 'whatever'
      
      0 讨论(0)
    • 2020-12-31 06:31

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

      select * from sys.tables where name='your-table-name-'
      
      0 讨论(0)
    • 2020-12-31 06:32

      Used this in 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 0 or 1 if your table exists or not in the ALL_OBJECTS records.

      0 讨论(0)
    • 2020-12-31 06:32

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

      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

      0 讨论(0)
    • 2020-12-31 06:39

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

      Something like

      0 讨论(0)
    • 2020-12-31 06:46

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

      select count(*)
      from all_objects
      where object_type in ('TABLE','VIEW')
      and object_name = 'your_table_name';
      
      0 讨论(0)
    提交回复
    热议问题