Possible Duplicate:
SQL Server: Check if table exists
Oracle: If Table Exists
I'm trying to create a table and insert some values, but before I do I must make sure that the table doesn't already exist. How do you check for this?
Possible Duplicate:
SQL Server: Check if table exists
Oracle: If Table Exists
I'm trying to create a table and insert some values, but before I do I must make sure that the table doesn't already exist. How do you check for this?
You can try simply SELECTing from it and capturing the error, otherwise you will need to write DB specific SQL to query their respective metadata tables and look there.
IF MySQL:
select count(*) from my_tables where table_name='table_1';
If count>0 then ...
Assuming SQL Server...Query sysobjects:
select * from sysobjects where xtype='U' and name ='tablename'
In MySQL, you can use the CREATE TABLE IF NOT EXISTS construct and run it before your INSERT query. This will create the table if it doesn't exist and will do nothing if the table is there.
CREATE TABLE IF NOT EXISTS myTable (
....
)
http://dev.mysql.com/doc/refman/5.1/en/create-table.html
http://docs.oracle.com/cd/E17952_01/refman-5.1-en/create-table.html
This simple query gives you details about the particular table which are created by users in oracle. Try it.
select * from user_tables where table_name = 'tablename';