Thread: CREATE TABLE IF NOT EXIST

CREATE TABLE IF NOT EXIST

From
"Mike Gagnon"
Date:
Hi Postgres Experts,
 
I'm trying to create tables (if they don't exist) through the ODBC driver.  I've tried the following:
 
SELECT relname FROM pg_class  WHERE relname = 'mytable';
 
This works from the psql command line program when logged into my database, but not when connected to my database over ODBC ( I get SQL_NO_DATA back from SQLFetch)
 
Any ideas?  Are there other ways to find out if a table exists?
 
Many thanks,
Mike

Re: CREATE TABLE IF NOT EXIST

From
"Obe, Regina"
Date:
IF NOT EXISTS(SELECT tablename FROM pg_catalog.pg_tables WHERE tablename = 'mytable')
 
....
 
 
IF NOT EXISTS(SELECT table_name FROM information_schema.tables where table_name = 'mytable')
 
Maybe the problem you are having is a permissions problem though.  Are you using the same account in psql as you are in ODBC?
 
Hope that helps,
Regina


From: pgsql-odbc-owner@postgresql.org [mailto:pgsql-odbc-owner@postgresql.org] On Behalf Of Mike Gagnon
Sent: Friday, January 25, 2008 9:09 AM
To: pgsql-odbc@postgresql.org
Subject: [ODBC] CREATE TABLE IF NOT EXIST

Hi Postgres Experts,
 
I'm trying to create tables (if they don't exist) through the ODBC driver.  I've tried the following:
 
SELECT relname FROM pg_class  WHERE relname = 'mytable';
 
This works from the psql command line program when logged into my database, but not when connected to my database over ODBC ( I get SQL_NO_DATA back from SQLFetch)
 
Any ideas?  Are there other ways to find out if a table exists?
 
Many thanks,
Mike


The substance of this message, including any attachments, may be confidential, legally privileged and/or exempt from disclosure pursuant to Massachusetts law. It is intended solely for the addressee. If you received this in error, please contact the sender and delete the material from any computer.


Help make the earth a greener place. If at all possible resist printing this email and join us in saving paper.