21

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>

kindly let me know the best way to check whether a table exists using 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.

2
  • I hope you're not responsible for a db design that calls for multiple tables with a date in their name ;) Does each table have a different structure? Commented Dec 9, 2010 at 4:36
  • Don't know, if there has been some change in the table name, but it should be user_tables, not user_table. Just in case, one wants to use the approach from the question. Commented Jan 21 at 7:46

6 Answers 6

36

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';
Sign up to request clarification or add additional context in comments.

3 Comments

+1 because he asked for Oracle. Also, the object_name must be upper-cased.
@Thilo: no, object names in Oracle don't have to be uppercase. They usually are, but they don't have to be.
They almost always are. And unless you created a table with the name in quotes (to make it case-sensitive), ALL_OBJECTS will have the table name uppercased.
1

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'

Comments

1

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-'

Comments

1

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.

Comments

1

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

Comments

-1

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

Something like

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.