CREATE OR REPLACE
PROCEDURE ug
IS
v_sql VARCHAR2(5000);
t_count NUMBER;
BEGIN
FOR c IN
(SELECT * FROM groups) LOOP
SELECT COUNT(*) INTO t_count FROM USER_TABLES WHERE table_name='EMAILS_' || c.id;
IF t_count=1 THEN
v_sql :='UPDATE GROUPS SET EMAIL_COUNT = (SELECT COUNT(*) FROM EMAILS_'||c.id||') WHERE id='||c.id;
EXECUTE IMMEDIATE V_SQL;
COMMIT;
END IF;
END LOOP;
END;