Consciousness

Oracle Drop and Recreate Existing Materialized View

Posted in Oracle by Personalife on the January 25th, 2010

Couldn’t find a script online that appropriately dropped and recreated an MV. We had an issue using impdp on 11g R1 where the MVs were imported under the right schema user, but for some reason, we couldn’t refresh the MV. The solution was to drop and recreate the MVs.


-- Drop and recreate MV for the current schema owner by Theo Chakkapark (http://suteki.nu)
DECLARE
	v_sql varchar2(32767);
 	TYPE mv_tables IS TABLE OF dba_mviews%rowtype INDEX BY PLS_INTEGER;

	mvrows mv_tables;
BEGIN

    	SELECT * BULK COLLECT INTO mvrows FROM user_mviews;

	IF(mvrows.count <> 0) THEN
	  FOR i IN mvrows.FIRST .. mvrows.LAST
	  LOOP
      v_sql := 'DROP MATERIALIZED VIEW ' || mvrows(i).owner || '.' || mvrows(i).mview_name;
      execute immediate (v_sql);
      dbms_output.put_line(v_sql);
      v_sql := 'CREATE MATERIALIZED VIEW ' || mvrows(i).owner || '.' || mvrows(i).mview_name || '
BUILD IMMEDIATE REFRESH COMPLETE AS ' || mvrows(i).query;
      execute immediate (v_sql);
      dbms_output.put_line(v_sql);
      END LOOP;
	END IF;

END;
/


No Comments »

No comments yet.

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

Powered by WordPress .::. Designed by SiteGround Web Hosting