Problem in executing sql file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kunal15
    New Member
    • Oct 2007
    • 1

    #1

    Problem in executing sql file

    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
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    Originally posted by kunal15
    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='your table');

    Comment

    Working...