2

Probably missing something trivial here but I have a function in my Java application running on Oracle 10g which generates a bunch of insert and delete statements into a BEGIN...; END; block.

When I execute the statement, it runs without error yet the values are not inserted/deleted to the database. If I copy the SQL that is to be run into SQL developer and execute it, it works as expected.

Example SQL...

BEGIN
  INSERT INTO tablea_archive (col1,col2,col3)
  SELECT col1,col2,col3 FROM tablea;
  DELETE FROM tablea;
  INSERT INTO tableb_archive (col1,col2,col3)
  SELECT col1,col2,col3 FROM tableb;
  DELETE FROM tableb;
END;

I have tried running the code via prepared,callabale and normal Statements with execute() and executeUpdate() and no joy.

Can anyone point out what I'm doing wrong?

4
  • 2
    Missing commit; maybe. In SQL Develeoper is AutoCommit on maybe. Commented Jun 8, 2011 at 21:05
  • I run the commit via the connection object after the update runs. I have also tried this with autocommit on and adding commit at the end of the block. Commented Jun 8, 2011 at 21:16
  • Never mind. I completely missread your SQL, deleting my answer. Commented Jun 8, 2011 at 21:45
  • Was this questioned answered if so mark it as such. Thanks. Commented Jul 13, 2015 at 11:26

2 Answers 2

1

I'd try something like

BEGIN
  INSERT INTO tablea_archive (col1,col2,col3)
  SELECT col1,col2,col3 FROM tablea;
  RAISE_APPLICATION_ERROR(-20001,'Inserted '||sql%rowcount||' rows');
END;

Your error handling should give you some form of message saying how many rows the INSERT though were being inserted. If you don't have error logging, look at recording errors on the database end

I'd suspect either the wrong database or wrong schema.

Sign up to request clarification or add additional context in comments.

Comments

0

This code snippet works. May be it can help you:

String  plsql = "BEGIN :myresult := dbms_random.random ; END;";
OracleDriver oracledrv = new OracleDriver();
Connection   con = oracledrv.connect(connstr, new Properties());

for (int i = 0 ; i < 1000 ; i++ ) {
    CallableStatement cb = con.prepareCall(plsql);
    cb.registerOutParameter("myresult", Types.INTEGER);
    cb.execute();
    System.out.println("random ->" +cb.getInt("myresult"));
    cb.close();
}
con.close();

1 Comment

cb.execute(); should be cb.executeQuery();, otherwise you'll get java.sql.SQLException: Incorrectly set or registered parameters.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.