获取 PostgreSQL 中受 INSERT 或 UPDATE 影响的记录数
我的 PostgreSQL 8/9 数据库驱动程序不会返回执行 INSERT
或 UPDATE
时受影响的记录计数。
PostgreSQL 提供非标准语法“RETURNING”,看起来像一个很好的解决方法。但语法可能是什么?该示例返回记录的 ID,但我需要一个计数。
插入分销商(did、dname)值(默认,“XYZ 小部件”) 返回了;
My database driver for PostgreSQL 8/9 does not return a count of records affected when executing INSERT
or UPDATE
.
PostgreSQL offers the non-standard syntax "RETURNING" which seems like a good workaround. But what might be the syntax? The example returns the ID of a record, but I need a count.
INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
RETURNING did;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我知道这个问题是 oooolllllld 并且我的解决方案可以说过于复杂,但这是我最喜欢的解决方案!
无论如何,我必须做同样的事情并让它像这样工作:
这些天我真的必须抽出时间为 PostgreSQL 的WITH子句写一首爱情十四行诗......
I know this question is oooolllllld and my solution is arguably overly complex, but that's my favorite kind of solution!
Anyway, I had to do the same thing and got it working like this:
One of these days I really have to get around to writing a love sonnet to PostgreSQL's WITH clause ...
我同意 Milen,您的司机应该为您做这件事。您使用什么驱动程序以及什么语言?但如果您使用 plpgsql,则可以使用 GET DIAGNOSTICS my_var = ROW_COUNT;
http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-语句-诊断
I agree w/ Milen, your driver should do this for you. What driver are you using and for what language? But if you are using plpgsql, you can use
GET DIAGNOSTICS my_var = ROW_COUNT;
http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS
您可以在更新后获取
ROW_COUNT
或使用以下代码插入:You can take
ROW_COUNT
after update or insert with this code:您可以将查询包装在事务中,它应该在您
ROLLBACK
或COMMIT
之前显示计数。示例:如果您运行上面的前两行,它应该会给您计数。如果您发现受影响的行数不是您所期望的,则可以
ROLLBACK
(撤消)插入。如果您对INSERT
的正确性感到满意,那么您可以运行相同的操作,但将第 3 行替换为COMMIT TRANSACTION;
。重要说明:运行任何
BEGIN TRANSACTION;
后,您必须ROLLBACK;
或COMMIT;< /code> 事务,否则事务将创建一个锁,如果您在生产环境中运行,该锁可能会减慢甚至瘫痪整个系统。
You could wrap your query in a transaction and it should show you the count before you
ROLLBACK
orCOMMIT
. Example:If you run the first 2 lines of the above, it should give you the count. You can then
ROLLBACK
(undo) the insert if you find that the number of affected lines isn't what you expected. If you're satisfied that theINSERT
is correct, then you can run the same thing, but replace line 3 withCOMMIT TRANSACTION;
.Important note: After you run any
BEGIN TRANSACTION;
you must eitherROLLBACK;
orCOMMIT;
the transaction, otherwise the transaction will create a lock that can slow down or even cripple an entire system, if you're running on a production environment.从你的问题中并不清楚你如何称呼该声明。假设您使用的是 JDBC 之类的东西,您可能会将其称为查询而不是更新。来自 JDBC 的 <代码>executeQuery:
因此,当您执行返回某些查询结果的语句(例如
SELECT
或INSERT ... RETURNING
)时,这是合适的。如果您正在对数据库进行更新,然后想知道有多少元组受到影响,则需要使用executeUpdate
返回:It's not clear from your question how you're calling the statement. Assuming you're using something like JDBC you may be calling it as a query rather than an update. From JDBC's
executeQuery
:This is therefore appropriate when you execute a statement that returns some query results, such as
SELECT
orINSERT ... RETURNING
. If you are making an update to the database and then want to know how many tuples were affected, you need to useexecuteUpdate
which returns:我可能会很晚才重播问题,但这会对像我这样寻求完整答案的人有所帮助,希望
I might be replaying to question very late, but it will help someone who will in search for the complete answer like me, in hope