开发者

number of rows affected by rollback

开发者 https://www.devze.com 2022-12-31 06:54 出处:网络
I need to find out the number o开发者_如何学编程f rows affected by a rollback. How can I get this ? Please help.Consider a table fred with two columns (id, value) with two rows.

I need to find out the number o开发者_如何学编程f rows affected by a rollback. How can I get this ? Please help.


Consider a table fred with two columns (id, value) with two rows. The first row is (1,'Blue') and the second is (2,'Blue')

I issue the following statements

INSERT INTO fred VALUES (1,'Red'); [inserts 1 row]
UPDATE fred SET value = 'Blue';    [updates 3 rows but the value on 2 doesn't change]
UPDATE fred SET id = 3 WHERE id = 1; [updates 1 row]
ROLLBACK;

Both records originally in the table have been updated. 1 was updated twice. One row was inserted and then updated. Then all those changes were rolled back. The question is, what number do you want ? The number of records updated, or the total number of updates performed to records.

The easiest answer to get, from a technical point of view, is the statistic number of undo records applied. But you'd have to measure this before and after. Actually, it can get very confusing because with an UPDATE statement that hits concurrent activity, a statement may be stopped part way through, rolled back and restarted. Ref AskTom


Actually, the number of rows affected by a rollback is zero. That's because, technically, those rows aren't changed until a commit occurs (the A in ACID). And, if you're rolling back, the commit doesn't happen.


I dont know of a way to do this with oracle, but you could potentially keep track of your created/altered/deleted rows using SQL%ROWCOUNT so you know what will be affected in a rollback


declare
  i number:=0;
begin
  INSERT INTO fred VALUES (1,'Red'); [inserts 1 row]
  i := i + sql%rowcount;
  UPDATE fred SET value = 'Blue';    [updates 3 rows but the value on 2 doesn't change]
  i := i + sql%rowcount;  
  UPDATE fred SET id = 3 WHERE id = 1; [updates 1 row]
  i := i + sql%rowcount;
  if <condition> then
      COMMIT;
      dbms_output.PUT_LINE(i || ' rows COMMITED';
      i := 0;
  else
      ROLLBACK;
      dbms_output.PUT_LINE(i || ' rows ROLLBACK';
      i := 0;
  end if;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消