oracle plsql过程在游标循环中

时间:2016-10-11 07:48:25

标签: plsql

我想更新所有具有ABC列的表。需要跳过没有数据的表。我在检查游标循环中表的计数时遇到问题。 PLSQL代码

create or replace procedure testp is

  CURSOR c_testp
IS

    SELECT table_name,
      column_name
    FROM all_tab_columns
    WHERE column_name IN('ABC') 
    ORDER BY table_name;

c int;

BEGIN

  FOR table_rec IN c_testp

  LOOP

    BEGIN

      SELECT COUNT(*)
      INTO c
      FROM table_rec.table_name;
      IF(c>0) THEN
        query := 'update '||table_rec.table_name||' set '||table_rec.column_name ||'= xyz';
       EXECUTE IMMEDIATE query;
       COMMIT;
      END IF;
    EXCEPTION
    WHEN no_data_found THEN
      dbms_output.put_line('data not found');
    WHEN OTHERS THEN
      dbms_output.put_line('others');
    END;
  END LOOP;

END;

1 个答案:

答案 0 :(得分:0)

在您的代码中,使用此:

EXECUTE IMMEDIATE 'SELECT count(*) FROM ' || table_rec.table_name INTO c;

而不是:

 SELECT COUNT(*)
      INTO c
      FROM table_rec.table_name; 

但是,如评论中所述 - 实际上不需要执行该条件检查,因为当update为空时不会执行table