存储过程更新数据库数据

背景:测试环境update数据库表数据没有什么问题,如果是线上数据库,修改表数据得时候,正好生产环境使用到该表数据得话,则会锁住表,更新会很慢。

解决:

CURSOR request_list IS
SELECT r.CUSTOMER_ID ,r.FIRST_NAME ,r.LAST_NAME ,r.MIDDLE_NAME, r.SEX ,r.ADDRESS, b.CITY AS POSTAL_CODE
FROM T_table r
JOIN T_table2 c ON c.CUSTOMER_ID = r.CUSTOMER_ID
LEFT JOIN T_table3  b ON c.BRANCH_CODE = b.BRANCH_CODE
WHERE r.status = 1 ;
commit_count number := 0;
total_count number := 0;
BEGIN
FOR kycr IN request_list LOOP
total_count := total_count + 1;
commit_count := commit_count + 1;
UPDATE T_table2 c
SET 
    FIRST_NAME = kycr.FIRST_NAME,
    LAST_NAME = kycr.LAST_NAME,
    MIDDLE_NAME = kycr.MIDDLE_NAME,
    SEX = kycr.SEX,
    ADDRESS = kycr.ADDRESS,
    POSTAL_CODE = kycr.POSTAL_CODE
WHERE c.CUSTOMER_ID = kycr.CUSTOMER_ID;
if commit_count > 100 then
commit;
commit_count := 0;
dbms_output.put_line('Total Count:' || total_count);
end if;
END LOOP;
    COMMIT;
    dbms_output.put_line('Total Count:' || total_count);
END;

你可能感兴趣的:(数据库)