Re: Getting result of odbc's INSERT INTO command



On Sat, 23 Apr 2005 16:23:43 +0200, Joe <joerider@xxxxxxxxx> wrote:

>Hi everybody,
>
>I am currently writing a library for adding/deleting/querying records of
>a database. In this special case I have used the ODBC-C-Functions.
>
>What I need is the resultset of an INSERT INTO operation. For example
>there should be the id-column returned, if I add a new record.
>
>What I've right now:
>std::string sql_string="INSERT INTO table1 (field1) VALUES ('value1')";
>sr=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
>sr=SQLExecDirect(hstmt,(SQLCHAR*) sql_string.data(),SQL_NTS);
>sr=SQLNumResultCols(hstmt, &nCols);
>
>In this situation I get no columns as result (nCols == 0)
>
>Can anybody make suggestions on how to solve a problem like this one?

INSERT INTO does not *return* any rows, thus the number of columns
returned is zero.

Assuming there is some unique way to identify the row besides the ID,
just SELECT id FROM table WHERE... to retrieve the just-inserted ID.
This is the most general solution, though some databases may have
other ways to do what you want.

Depending on the transaction isolation level, and assuming you're
using transactions and auto-increment IDs, you *may* be able to:

BEGIN TRANSACTION
INSERT INTO...
SELECT MAX(id) FROM table
END TRANSACTION

But I don't if this is failsafe under any specific conditions. The
same isolation levels that make this work may allow you to receive an
ID inserted by another connection, and not all databases support all
isolation levels.

--
Phillip Crews aka Severian
Microsoft MVP, Windows SDK
Posting email address is real, but please post replies on the newsgroup.
.



Relevant Pages

  • Re: Session ending shutdown problem
    ... massively long shutdown time, you will have to rethink what it means to "shut down". ... and start routing everything to it instead of the databases. ... this process starts gets appended to the transaction file instead. ... Of course I can detect the QueryEndSession and EndSession messages. ...
    (microsoft.public.vc.mfc)
  • Re: Why is database integrity so impopular ?
    ... When time comes to build transactional databases (as opposed to data ... normalizing data with high integrity mechanisms. ... What is wrong with modern DB design approaches? ... declarative model of some version of transaction interests me though ...
    (comp.databases.theory)
  • Re: Why is database integrity so impopular ?
    ... When time comes to build transactional databases (as opposed to data ... normalizing data with high integrity mechanisms. ... What is wrong with modern DB design approaches? ... I must say that a declarative model of some version of transaction interests me though I've never seen anybody try to explain one. ...
    (comp.databases.theory)
  • RE: MDBData Logs
    ... and although the advise to use an Exchange aware ... is dump the headers of the exchange databases of your ... Here you will see that the last transaction the server has committed is ... most likely (it schould be the case when a clean shutdown is ...
    (microsoft.public.exchange.admin)
  • Re: Application logic and Business logic
    ... During transaction some objects on the ... > programming languages, but when OO is applied to databases (and ... OODB failed to replace relational databases. ... You compare apples and oranges. ...
    (comp.object)

Loading