Home » Java » Executing batch of DDL statements in one query java

Executing batch of DDL statements in one query java

Posted by: admin October 26, 2017 Leave a comment

Questions:

I have a query which batch of sql commands in one string,

 String SQLQuery =

"CREATE VOLATILE TABLE v1;
CREATE VOLATILE TABLE v2;

INSERT INTO v1;
//do stuff
INSERT INTO v2;
//do stuff
UPDATE xx;
//do stuff

DROP TABLE v1;
DROP TABLE v2;"

I tried to execute them with executeUpdate(), executeBatch() both of them fails with java jdbc client, which works in Teradata client.

 Error mesg:
 [TeraJDBC 15.10.00.35] [Error 3576] [SQLState 25000] Data definition not valid unless solitary.

Java code:

    PreparedStatement stmt10 = null;
    try {

        stmt10 = conn.prepareStatement(SQLQuery);
        //stmt10.executeUpdate();
        stmt10.executeBatch();
        //conn.commit();

    } catch (Exception ex) {
        throw new Exception(ex.getMessage());
    } finally {
        if (stmt10 != null) {
            stmt10.close();
        }
    }

how can i do this?

Answers:

In teradata you cannot execute DDL statements together. It’s not a problem with the JDBC or the java code – just the way you want to use teradata. That is what that error is basically saying:

http://teradataerror.com/3576-Data-definition-not-valid-unless-solitary.html

So you need to send the separate the DDL statements in separate transactions with a separate requests. If it was multiple updates and selects it would work just fine but create table cannot be executed like that.

I believe it is due to the fact that any DDL request MUST be commited before actually happening. And if you use it like that it is not. I think it is actually OK to have DDL in a multiple commands query if it is only one and it is the last statement .

Leave a Reply

Your email address will not be published. Required fields are marked *