简体   繁体   中英

Oracle get last inserted row id using OCCI and C++

Given the code above, how can I grab the ID of the row inserted into database. I´m using C++ and oracle OCCI interface:

    std::stringstream sqlStream("");
    sqlStream << "INSERT INTO MYTABLE (COL1, COL2, COL3) VALUES (1, 2, 3) RETURNING ID INTO :my_id_param";
    std::string sql(sqlStream.str());
    std::cout << sql << std::endl;

    std::unique_ptr<oracle::occi::Statement> stmt(connection->createStatement());
    stmt->execute(sql.c_str());

    //??? How can I access here the my_id_param ?

Thanks for helping.

You can use anonymous PL/SQL and register an OUT parameter to capture the resulting ID. This works with a sequence directly, I've not tried with a sequence used in a trigger.

std::stringstream sqlStream("");
sqlStream << "BEGIN INSERT INTO MYTABLE (COL1, COL2, COL3) VALUES (1, 2, 3) RETURNING ID INTO :1; END;";
std::string sql(sqlStream.str());
std::cout << sql << std::endl;

std::unique_ptr<oracle::occi::Statement> stmt(connection->createStatement(sql.c_str()));
stmt->registerOutParam(1, oracle::occi::OCCIINT);
stmt->executeUpdate();
int id = stmt->get(1);
// Use id...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM