oracle 获取已删除的记录数sql

x0fgdtte  于 2023-05-06  发布在  Oracle
关注(0)|答案(2)|浏览(324)

我想获取已删除记录的行数。下面的查询返回将受影响的记录数,但是

SELECT COUNT(*) FROM suppliers
WHERE EXISTS
  ( SELECT customers.customer_name
    FROM customers
    WHERE customers.customer_id = suppliers.supplier_id
    AND customer_id > 25 );

我需要这样的东西,行被删除后,我可以显示已被删除的记录在前端的数量。

SELECT COUNT(*) FROM suppliers(
DELETE from supplier(
    WHERE EXISTS
      ( SELECT customers.customer_name
        FROM customers
        WHERE customers.customer_id = suppliers.supplier_id
        AND customer_id > 25 ));

我不想运行两次查询,第一次是为了得到将要删除的记录数,如果它大于0,那么我运行第二次查询来删除记录。

7kjnsjlb

7kjnsjlb1#

您可以使用RETURNING INTO子句获取所有被删除的行--然后对这些行进行计数。
一个更简单的方法是PL/SQL代码中的SQL%ROWCOUNT。就像这样:

BEGIN
    DELETE from supplier(
        WHERE EXISTS
          ( SELECT customers.customer_name
            FROM customers
            WHERE customers.customer_id = suppliers.supplier_id
            AND customer_id > 25 );
    dbms_output.put_line(SQL%ROWCOUNT);
END;
b1payxdu

b1payxdu2#

你可以用

declare @countDelete int
set nocount on
delete tablename
where ..
select @@ROWCOUNT

相关问题