views:

81

answers:

2

What's PLSQL (Oracle) equivalent of this SQL server snippet?

BEGIN TRAN
INSERT INTO mytable(content) VALUES ("test") -- assume there's an ID column that is autoincrement
SELECT @@IDENTITY
COMMIT TRAN

In C#, you can call myCommand.ExecuteScalar() to retrieve the ID of the new row.

How can I insert a new row in Oracle, and have JDBC get a copy of the new id?

EDIT: BalusC provided a very good starting point. For some reason JDBC doesn't like named parameter binding. This gives "Incorrectly set or registered parameters" SQLException. Why is this happening?

        OracleConnection conn = getAppConnection();
        String q = "BEGIN INSERT INTO tb (id) values (claim_seq.nextval) returning id into :newId; end;" ;
        CallableStatement cs = (OracleCallableStatement) conn.prepareCall(q);
        cs.registerOutParameter("newId", OracleTypes.NUMBER);
        cs.execute();
        int newId = cs.getInt("newId");
+4  A: 

You can use Oracle's returning clause.

insert into mytable(content) values ('test') returning your_id into :var;

Check out this link for a code sample. You need Oracle 10g or later, and a new version of JDBC driver.

Pablo Santa Cruz
+4  A: 

Normally you would use Statement#getGeneratedKeys() for this (see also this answer for an example), but this is as far (still) not supported by the Oracle JDBC driver.

Your best bet is to either make use of CallableStatement with a RETURNING clause:

String sql = "BEGIN INSERT INTO mytable(id, content) VALUES (seq_mytable.NEXTVAL(), ?) RETURNING id INTO ?; END;";

Connection connection = null;
CallableStatement statement = null;

try {
    connection = database.getConnection();
    statement = connection.prepareCall(sql);
    statement.setString(1, "test");
    statement.registerOutParameter(2, Types.NUMERIC);
    statement.execute();
    int id = statement.getInt(2);
    // ...

Or fire SELECT sequencename.CURRVAL after INSERT in the same transaction:

String sql_insert = "INSERT INTO mytable(content) VALUES (?)";
String sql_currval = "SELECT seq_mytable.CURRVAL FROM dual";

Connection connection = null;
PreparedStatement statement = null;
Statement currvalStatement = null;
ResultSet currvalResultSet = null;

try {
    connection = database.getConnection();
    connection.setAutoCommit(false);
    statement = connection.prepareStatement(sql_insert);
    statement.setString(1, "test");
    statement.executeUpdate();
    currvalStatement = connection.createStatement();
    currvalResultSet = currvalStatement.executeQuery(sql_currval);
    if (currvalResultSet.next()) {
        int id = currvalResultSet.getInt(1);
    }
    connection.commit();
    // ...
BalusC
Do you mean "SELECT seq_mytable.CURRVAL from dual" instead of "SELECT CURRVAL(seq_mytable)"?
Patrick Marchand
@Patrick: Oh drat, I had PostgreSQL SQL syntax in mind, I'll update (previously, PostgreSQL used to have the same problem of not supporting [`Statement#getGeneratedKeys()`](http://stackoverflow.com/questions/1915166/jdbc-how-can-we-get-inserted-record-id-in-java/1915197#1915197) so that the same "workaround" was necessary, but since about a year ago they finally fixed their JDBC driver to support it).
BalusC
Hi BalusC, thanks for the help. Can you take a look at my edit see if you can solve that other mystery?
Haoest
You need to specify the placeholder index, not the column name. Replace `"newId"` by `2` in `registerOutParameter()`.
BalusC
In practice, I have a long insert statement with 15-some parameters, which I prefer having named binding if I could. JDBC will complain about having mixed binding if I use question mark. Do I must go with ordinal binding when out-parameter is used?
Haoest
For named binding, use an ORM like Hibernate/JPA. Basic JDBC doesn't support it.
BalusC