确定删除了多少行

时间:2018-02-16 11:05:12

标签: plsql

我使用表来驱动另一个表的删除。我希望能够跟踪驱动删除的表中每条记录删除的行数。使用SQL%ROWCOUNT对我的用途似乎没有用,因为它只返回DELETE语句中删除的总行数。还有其他方法可供我使用吗?为了说明

DELETE 
FROM my_schema.tbl_companies A 
WHERE EXISTS (
        SELECT *
        FROM my_schema.table_that_includes_companies_to_be_deleted RMV 
        WHERE RMV.company = A.company
    );

如果我的table_that_includes_companies_to_be_deleted表中有两条记录,并且这些记录中的每一条映射到tbl_companies表中的20条记录,那么这将导致SQL%ROWCOUNT返回值40.我希望能够确切地查看table_that_includes_companies_to_be_deleted中第一条记录的删除记录数以及table_that_includes_companies_to_be_deleted中的第二条记录

1 个答案:

答案 0 :(得分:1)

根据@ Plirkee的评论,您可以使用游标和for循环来删除它们。或者你可以这样做:

DECLARE
  l_tab SYS.KU$_VCNT; -- This is a pre-existing collection type
BEGIN
  DELETE 
  FROM my_schema.tbl_companies A 
  WHERE EXISTS (
          SELECT *
          FROM my_schema.table_that_includes_companies_to_be_deleted RMV 
          WHERE RMV.company = A.company
      )
  RETURNING company BULK COLLECT INTO l_tab;

  for r in (select column_value, count(*) cnt from table(l_tab) group by column_value)
  loop
    dbms_output.put_line('company '||r.column_value||' deleted '||r.cnt);
  end loop;
END;