在PostgreSQL中计算受批量查询影响的行数 [英] Calculate number of rows affected by batch query in PostgreSQL

查看:382
本文介绍了在PostgreSQL中计算受批量查询影响的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,是的,我已经阅读了DO语句的文档:) http://www.postgresql.org/docs/9.1/static/sql -do.html

First of all, yes I've read documentation for DO statement :) http://www.postgresql.org/docs/9.1/static/sql-do.html

所以我的问题:

我需要执行一些包含UPDATE语句的动态代码块,并计算所有受影响行的数量.我正在使用Ado.Net提供程序.

I need to execute some dynamic block of code that contains UPDATE statements and calculate the number of all affected rows. I'm using Ado.Net provider.

在Oracle中,该解决方案将包含4个步骤:

In Oracle the solution would have 4 steps:

  1. 在命令中添加InputOutput参数"N"
  2. 添加 BEGIN ... END; 进行命令
  3. 在每个语句后
  4. 添加:N:=:N + sql%rowcount .
  5. 完成了!执行完后,我们可以从命令中读取N个参数.
  1. add InputOutput parameter "N" to command
  2. add BEGIN ... END; to command
  3. add :N := :N + sql%rowcount after each statement.
  4. It's done! We can read N parameter from command, after execute it.

我如何使用PostgreSQL?我正在使用npgsql提供程序,但如果有帮助,可以迁移到devard.

How can I do it with PostgreSQL? I'm using npgsql provider but can migrate to devard if it helps.

推荐答案

我的解决方案非常简单.在Oracle中,我需要使用变量来计算更新的行的总和,因为command.ExecuteNonQuery()仅返回受批次中最后一个UPDATE影响的行数.

My solution is quite simple. In Oracle I need to use variables to calculate the sum of updated rows because command.ExecuteNonQuery() returns only the count of rows affected by the last UPDATE in the batch.

但是,npgsql返回由所有UPDATE查询更新的所有行的总和.因此,我只需要调用command.ExecuteNonQuery()并获得没有任何变量的结果.比使用Oracle容易得多.

However, npgsql returns the sum of all rows updated by all UPDATE queries. So I only need to call command.ExecuteNonQuery() and get the result without any variables. Much easier than with Oracle.

这篇关于在PostgreSQL中计算受批量查询影响的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆