如何获取 PostgreSQL 中删除的行数?

发布于 2024-08-20 20:22:54 字数 603 浏览 7 评论 0原文

我正在寻找一种方法来返回 PostgreSQL 中受 DELETE 子句影响的行数。 文档指出;

成功完成后,删除 命令返回命令标签 表格

删除计数

计数是行数 已删除。如果 count 为 0,则没有行 符合条件(这不是 被认为是一个错误)。

如果 DELETE 命令包含 RETURNING 子句,结果将是 类似于 SELECT 语句 包含列和值 在 RETURNING 列表中定义, 计算删除的行 命令。

但我很难找到一个很好的例子。谁能帮我解决这个问题,如何找出删除了多少行?


编辑: 我想提出一个我后来找到的替代方案。它可以在此处找到,在38.5.5下解释。获取结果状态 标题。

I am looking for a way to return the number of rows affected by a DELETE clause in PostgreSQL. The documentation states that;

On successful completion, a DELETE
command returns a command tag of the
form

DELETE count

The count is the number of rows
deleted. If count is 0, no rows
matched the condition (this is not
considered an error).

If the DELETE command contains a
RETURNING clause, the result will be
similar to that of a SELECT statement
containing the columns and values
defined in the RETURNING list,
computed over the row(s) deleted by
the command.

But I am having trouble finding a good example of it. Can anyone help me with this, how can I find out how many rows were deleted?


EDIT:
I wanted to present an alternative that I have found later. It can be found in here, explained under 38.5.5. Obtaining the Result Status title.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

讽刺将军 2024-08-27 20:22:54

您可以使用 RETURNING 子句:

DELETE FROM table WHERE condition IS TRUE RETURNING *;

之后您只需检查返回的行数。您可以使用 CTE 简化它:

WITH deleted AS (DELETE FROM table WHERE condition IS TRUE RETURNING *) SELECT count(*) FROM deleted;

这应该只返回已删除的行数。

You can use RETURNING clause:

DELETE FROM table WHERE condition IS TRUE RETURNING *;

After that you just have to check number of rows returned. You can streamline it with CTE:

WITH deleted AS (DELETE FROM table WHERE condition IS TRUE RETURNING *) SELECT count(*) FROM deleted;

This should return just the number of deleted rows.

于我来说 2024-08-27 20:22:54

GET DIAGNOSTICS 用于显示修改/删除的记录数。

示例代码

CREATE OR REPLACE FUNCTION fnName()
  RETURNS void AS
$BODY$
        declare
         count numeric;
       begin
              count := 0;
            LOOP
             -- condition here update or delete;
             GET DIAGNOSTICS count = ROW_COUNT;
             raise notice 'Value: %', count;
             end loop;
        end;
$BODY$a

GET DIAGNOSTICS is used to display number of modified/deleted records.

Sample code

CREATE OR REPLACE FUNCTION fnName()
  RETURNS void AS
$BODY$
        declare
         count numeric;
       begin
              count := 0;
            LOOP
             -- condition here update or delete;
             GET DIAGNOSTICS count = ROW_COUNT;
             raise notice 'Value: %', count;
             end loop;
        end;
$BODY$a
大姐,你呐 2024-08-27 20:22:54

这在 Java 中应该很简单。

Statement stmt = connection.createStatement();
int rowsAffected = stmt.executeUpdate("delete from your_table");
System.out.println("deleted: " + rowsAffected);

请参阅 java.sql.语句

This should be simple in Java.

Statement stmt = connection.createStatement();
int rowsAffected = stmt.executeUpdate("delete from your_table");
System.out.println("deleted: " + rowsAffected);

See java.sql.Statement.

幸福不弃 2024-08-27 20:22:54

在使用 psycopg2 的 Python 中,可以使用 rowcount 属性。这是一个示例,用于找出删除了多少行......

cur = connection.cursor()
try:
    cur.execute("DELETE FROM table WHERE col1 = %s", (value,))
    connection.commit()
    count = cur.rowcount
    cur.close()
    print("A total of %s rows were deleted." % count)
except:
    connection.rollback()
    print("An error as occurred, No rows were deleted")

in Python using psycopg2, the rowcount attribute can be used. Here is an example to find out how many rows were deleted...

cur = connection.cursor()
try:
    cur.execute("DELETE FROM table WHERE col1 = %s", (value,))
    connection.commit()
    count = cur.rowcount
    cur.close()
    print("A total of %s rows were deleted." % count)
except:
    connection.rollback()
    print("An error as occurred, No rows were deleted")
公布 2024-08-27 20:22:54

这适用于函数。它也可以与 INSERT 等其他操作一起使用。

DECLARE _result INTEGER;
...
DELETE FROM mytable WHERE amount = 0;  -- or whatever other operation you desire
GET DIAGNOSTICS _result = ROW_COUNT;
IF _result > 0 THEN
    RAISE NOTICE 'Removed % rows with amount = 0', _result;
END IF;

This works in functions. It works with other operations like INSERT as well.

DECLARE _result INTEGER;
...
DELETE FROM mytable WHERE amount = 0;  -- or whatever other operation you desire
GET DIAGNOSTICS _result = ROW_COUNT;
IF _result > 0 THEN
    RAISE NOTICE 'Removed % rows with amount = 0', _result;
END IF;
暗恋未遂 2024-08-27 20:22:54

您需要 PQcmdTuples 来自 libpq。例如,在 PHP 中,它被包装为 pg_affected_rows

You need the PQcmdTuples function from libpq. Which in PHP for example is wrapped as pg_affected_rows.

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