如何覆盖 CRecordSet 的 SQL 然后重新查询?
我有一个 CRecordSet (Visual C++ 6.0),并且使用 ODBC 连接到 postgresql 8.0.8。
__
问题:我在带有序列 id(自动增量)的表中插入一行,并且我想在插入后检索该 id。
我无法使用 RETURNING 关键字,该版本的 PGsql 不支持该关键字,但我可以使用 currval('sequence')。
但是,在我的代码中,我需要执行默认查询,然后覆盖它以使用 currval() 执行查询。
据我了解,我只能通过再次调用 CRecordSet::Open() 来覆盖查询,这将创建一个新会话,使 currval() 无用。
__
那么:我如何覆盖 SQL,然后通过 Requery() 而不是通过 Open() 执行这个新查询?
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()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,currval() 不是特定于连接的。 您应该在新连接上获得正确的值。 您还可以尝试在 INSERT 语句末尾附加“select currval()”。
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.
使用 currval()。 这是特定于会话的并且并发安全的。 语法如上面 Imraan Parker 的答案所示。
但事实是,对于新会话,没有安全的方法来获取此信息。
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.