如何覆盖 CRecordSet 的 SQL 然后重新查询?

发布于 2024-07-14 07:01:32 字数 413 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从此见与不见 2024-07-21 07:01:32

据我了解,currval() 不是特定于连接的。 您应该在新连接上获得正确的值。 您还可以尝试在 INSERT 语句末尾附加“select currval()”。

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

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');
归属感 2024-07-21 07:01:32

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文