i want to update a table with the value of another table. But: I need a commit after every 50000 rows. I don't want to discuss why and I know the tip to create a new table instead of update, but this is not an option. I need only help with the query.
For update after x rows I found this:
DECLARE
i NUMBER := 0;
CURSOR G1 IS SELECT * FROM test_new
FOR UPDATE;
BEGIN
FOR c1 IN S1 LOOP
UPDATE test SET col1 = 'somevalue1'
WHERE CURRENT OF G1;
i := i + 1; -- Commits after every X number of records
IF i > 1000 THEN
COMMIT;
i := 0;
END IF;
END LOOP;
COMMIT;
END;
To Update a table with another table this code works:
DECLARE
l_r_id test_new.id_customer%type;
l_r_name test_new.name%type;
i NUMBER := 0;
CURSOR CUR is select tnw.id_customer, tnw.name
from test_new tnw
, test tes
where tnw.id_customer = tes.id_customer
FOR UPDATE;
BEGIN
OPEN cur;
LOOP
FETCH cur
INTO l_r_id, l_r_name;
UPDATE test
set name = l_r_name
where test.id_customer = l_r_id;
i := i+1;
EXIT WHEN cur%notfound;
END LOOP;
commit;
END;
But I don't know how to get
IF i > 50000 THEN
COMMIT;
i := 0;
END IF;
into the code. It seems there is a problem with FETCH and Commit. I get the Error Message from Oracle:
2) If the cursor has been opened with the FOR UPDATE clause, fetching after a COMMIT has been issued will return the error.
Does anybody have an idea? I know there have been a way to join without "FETCH" but I don't know how. Like I said earlier, please only help with the code, no discussion about update and commit.