By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,334 Members | 2,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,334 IT Pros & Developers. It's quick & easy.

Problem in executing sql file

P: 1
Hi all,
I am tring to execute sql scipts written in a seprate .sql file. In the begining i drop all the tables and create the new one. But while working with a completly new database there wont be any tabel to drop, so my drop query fails. Because of which furthor sql statements does not execute.So my table creation fails.I wants to execuet create statements even if drop statements fails. this sql files works fine on other databases. problem is only with the postgres.

regards
kunal
Oct 30 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 700
Hi all,
I am tring to execute sql scipts written in a seprate .sql file. In the begining i drop all the tables and create the new one. But while working with a completly new database there wont be any tabel to drop, so my drop query fails. Because of which furthor sql statements does not execute.So my table creation fails.I wants to execuet create statements even if drop statements fails. this sql files works fine on other databases. problem is only with the postgres.

regards
kunal
You can prepare the drop statement to behave as drop table if exists.
define a function

CREATE FUNCTION exec(TEXT) RETURNS VOID AS'
DECLARE
BEGIN
EXECUTE $1;
END;
LANGUAGE plpgsql;

and do
SELECT exec('DROP TABLE yourtable') WHERE EXISTS(SELECT * FROM pg_table WHERE tablename='yourtable');
Oct 30 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.