среда, 28 января 2015 г.

ORACLE: How to add column in Materialized view.

-- 1. create materialized view with 1 column
SQL> create materialized view mv_dual
refresh force on demand
start with sysdate next sysdate + 1
as
select dummy from dual;

Materialized view created.

-- 2. first select
SQL> select * from mv_dual;

DUM                                                                          
---                                                                          
X                                                                            

-- 3. add 2d column to materialized view table
SQL> alter table mv_dual add dummy_the_second VARCHAR2(1);

Table altered.

-- 4. update 2 columns in system table SNAP$
SQL> update sys.snap$
set query_txt = 'select dummy, dummy dummy_the_second from dual',
    query_len = length('select dummy, dummy dummy_the_second from dual')
where vname='MV_DUAL';

1 row updated.

-- 5. commit
SQL> commit;

Commit complete.

-- 6. recompile materialized view
SQL> alter snapshot mv_dual compile;

Materialized view altered.

-- 7. refresh materialized view
SQL> exec dbms_mview.refresh('MV_DUAL','C');

PL/SQL procedure successfully completed.

-- 8. check result
SQL> select * from mv_dual;

DUM DUM                                                                      
--- ---                                                                      
X   X                                                                        

-- 9. drop
SQL> drop materialized view mv_dual;

Materialized view dropped.