Tutorial :How do I override the SQL for a CRecordSet and then requery?


I have a CRecordSet (Visual C++ 6.0) and I'm using ODBC to connect to postgresql 8.0.8.


The problem: I insert a row into a table with a serial id (autoincrement), and I would like to retrieve the id after my insert.

I can't use the RETURNING keyword, not supported on this version of PGsql, but I can use currval('sequence').

However, in my code, I need to execute the default query and then override it to execute the query with currval().

From what I understand, I can only override the query by calling CRecordSet::Open() again, which would create a new session, rendering currval() useless.


So: How can I override the SQL, then execute this new query via a Requery() and not via Open()?


As far as I understand, currval() is not connection specific. You should get the correct value back on the new connection. You could also try and append the 'select currval()' at the end of your INSERT statement.

INSERT INTO mytable VALUES (1); SELECT currval('mysequence');  


Use currval(). This is session-specific and concurrency safe. The syntax is above in Imraan Parker's answer.

The fact though is that with a new session there is no safe way to get this info.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »