创建存储过程,使用游标更新表信息
必然会产生行锁啊。
举个例子:
DROP TABLE t;
CREATE TABLE t
(
c1 NUMBER(4),
c2 VARCHAR2(10)
);
INSERT INTO t VALUES(1, 'A');
INSERT INTO t VALUES(2, 'B');
INSERT INTO t VALUES(3, 'C');
COMMIT;
SELECT * FROM t;
--创建存储过程,目标是将每行中的c2由一个字母,变成相同的2个字母
CREATE OR REPLACE PROCEDURE MyProc AS
--指定for update必须会产生行锁
CURSOR cur IS SELECT c1, c2 FROM t FOR UPDATE;
BEGIN
FOR r IN cur LOOP
UPDATE t SET c2 = r.c2 || r.c2 WHERE CURRENT OF cur;
END LOOP;
COMMIT;
END;
--测试
BEGIN
MyProc;
end;