Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF EXISTS for multiple data sources/databases

Tags:

sql

Is there a standard way to check if a table exists in a database using only pure SQL and nothing that is database specific? Basically an equivalent of IF EXISTS that will work when used on any datasource (SQL compliant of course).

UPDATE

How can I make the examples given work in this statement?

IF(SELECT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'clusterTable200m' 
AND TABLE_TYPE='BASE TABLE')) 
BEGIN  
SELECT 'Exists' 
END 
ELSE  
BEGIN  CREATE TABLE clusterTable200m(CLUSTER_ID int NOT NULL, CELL_GEOM   geometry NOT NULL, CELL_CENTROID geometry NOT NULL , CLUSTER_CENTROID   geometry , CLUSTER_EXTENT  geometry , NUM_POINTS  int , FEATURE_PK  int , PRIMARY KEY (CLUSTER_ID)) SELECT 'Complete'  
END

UPDATE

The above code is a minds eye of how I want the SQL to work but it has been pointed out that I cannot have the IF without a stored procedure or similar and this is out of the question. I also cannot reference for example sysobjects from ms sql server as this is specific to that data source.

I know it is a tricky one but if anyone can offer a solution it would be greatly appreciated.

UPDATE

Ok so as it stands the only work around I have thought of is as follows:

        string sqlQuery = "SELECT * FROM " + tableName;

        OleDataBaseConnection oleDataBaseConnection = new OleDataBaseConnection();

        bool tableExists = true;

        try
        {
            oleDataBaseConnection.OleExecutePureSqlQuery(sqlQuery);
        }
        catch (Exception exc)
        {
            if (exc.Message.ToUpper().Contains("EXIST"))
            {
                tableExists = false;
            }
            else
            {
                throw;
            }
        }

This code will attempt to return all items from the table(will be changed to just attempt a return of the first record to prevent performance impact) and if it fails it checks the error message for the word exists (so as not to supress other exceptions) If the word exists is present I assume that the table is not in the database. Then further down my code if the table does not exist then I run the pure SQL to create the new table. This is the only work around I have though of so far and I welcome any input.

like image 561
CSharpened Avatar asked Mar 13 '26 13:03

CSharpened


1 Answers

Using INFORMATION_SCHEMA, for those systems that comply:

SELECT EXISTS
       ( SELECT *
         FROM INFORMATION_SCHEMA.TABLES
         WHERE TABLE_CATALOG = 'CatalogName'
           AND TABLE_SCHEMA = 'SchemaName' 
           AND TABLE_NAME = 'TableName'
       ) AS answer
FROM dual              --- this may be required in some systems.

Information_schema docs:

  • SQL-Server

  • PostgreSQL

  • MySQL

  • DB2

like image 168
ypercubeᵀᴹ Avatar answered Mar 15 '26 06:03

ypercubeᵀᴹ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!