使用游标逐行更新数据
DECLARE
--select t.*, t.rowid from b_test t;
-- Local variables here
CURSOR UPDATE_CURSOR IS
SELECT T.*, T.ROWID FROM B_TEST T FOR UPDATE OF T.TABLE_NAME;
T_ROW UPDATE_CURSOR%ROWTYPE;
STR VARCHAR2(2000);
V_P VARCHAR2(20);
V_N VARCHAR2(20);
V_R VARCHAR2(20);
BEGIN
OPEN UPDATE_CURSOR;
LOOP
FETCH UPDATE_CURSOR
INTO T_ROW;
EXIT WHEN UPDATE_CURSOR%NOTFOUND;
V_P := T_ROW.POT_CDE;
V_R := T_ROW.ROWID;
STR := ' UPDATE B_TEST T SET T.TABLE_NAME = ''' || T_ROW.TABLE_NAME ||
'_B'' where Current of UPDATE_CURSOR and T.ROWID = ' V_R;
DBMS_OUTPUT.PUT_LINE(STR);
--EXECUTE IMMEDIATE STR;
--COMMIT;
END LOOP;
CLOSE UPDATE_CURSOR;
END;
请高手给看看。
我的本意是想根据每行记录的ROWID更新对应的每一行,为什么不行?是我的思想有问题吗?应该怎么办?非常感谢!!!
------解决方案--------------------
SQL code
create table t(table_name varchar2(20));
insert into t select 'name'||rownum from dual connect by rownum <= 50;
commit;
BEGIN
for i in (select t.rowid, t.* from t t) loop
update t set table_name = i.table_name||'_B' where rowid = i.rowid;
END LOOP;
commit;
END;
/