1

I want to execute a SQL script in a client using JDBC (not the postgreSQL pslq client). In this script I would like to do something like that:

skip errors on;

alter table foo ...;

skip errors off;

Is there a way to do this with PostgreSQL >= 9.1?

5
  • Not like you describe. Just run one statement per transaction. If you can't do that, you need to use subtransactions in PL/PgSQL DO blocks. Commented Jun 11, 2015 at 11:47
  • So which SQL client is that? Commented Jun 11, 2015 at 11:58
  • I'm using SQuirreL or Flyway for sql code execution. Commented Jun 11, 2015 at 12:08
  • Does squirrel support using savepoints around statements? If yes, then enable that. Commented Jun 11, 2015 at 12:09
  • Thank's for your answers! One wish: Can someone please rate my question/answer? I need reputation to rate others here...:-) Commented Jun 12, 2015 at 6:30

1 Answer 1

0

I found this thread with a good solution usind DO blocks and error codes: How to continue sql script on error?

And creating a function for my problem:

create or replace function continueOnError(v_sqlStatement text)
  returns void
  language plpgsql
  as ' 
  BEGIN
    execute v_sqlStatement;
  EXCEPTION
    WHEN invalid_schema_name
      OR undefined_table 
      OR undefined_column 
    THEN RAISE WARNING ''Continued on error sql statement: %'', v_sqlStatement;
  END;
';

...use it:

select continueOnError('alter table test add constraint fk_test_valueid foreign key (valueid) references value(id)');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.