This script modifies the structure of a snapshot for Oracle replication without dropping and recreating the snapshot. (It's very useful but you have to be careful--it should be done by a DBA only.)
Suppose that we have a table in the schema scott/tiger:
create table TEST (col1 number constraint pk_c1 primary key ,col2 varchar2(20));
We then create a snapshot for the test:
create snapshot TEST_SNAP as select * from test;
Now we have to add another column to the TEST table:
ALTER TABLE TEST ADD COL3 VARCHAR2(50);
So, here is what we do to make TEST_SNAP recognize col3 without dropping and recreating the snapshot. First, alter the table that's created by the snpashot to reflect the same structure as the master table (VNAME and SOWNER are case sensitive):
ALTER TABLE TEST_SNAP ADD COL3 VARCHAR2(50); CONNECT SYS/****** set long 500000 set pagesize 1000 select QUERY_TXT ,QUERY_LEN from snap$ where vname='TEST_SNAP' and sowner='SCOTT';
Here is the result:
QUERY_TXT ------------------------------------- QUERY_LEN ---------- SELECT "TEST"."COL1" "COL1","TEST"."COL2" "COL2" FROM "TEST" "TEST" 67
update snap$ set QUERY_TXT='SELECT "TEST"."COL1" "COL1","TEST"."COL2" "COL2" ,"TEST"."COL3" "COL3" FROM "TEST" "TEST"' , QUERY_LEN=length('SELECT "TEST"."COL1" "COL1","TEST"."COL2" "COL2", "TEST"."COL3" "COL3" FROM "TEST" "TEST"') where vname='TEST_SNAP' and sowner='SCOTT'; commit;
Next, recompile the snapshot:
ALTER SNAPSHOT SCOTT.TEST_SNAP COMPILE; CONNECT SCOTT/TIGER exec dbms_snapshot.refresh('TEST_SNAP',X) ;
Where x='F' or 'C' depending if you have a snapshot log or not.
Make sure to try the SQL statement before you update the SNAP$ table. Note: It's usually better to make a backup on the row that you are going to change in the SNAP$ table:
set long 50000 set copycommit 1 set arraysize 500 copy to sys/*****@ORACLE_SID - replace snap_bk - using select QUERY_TXT ,QUERY_LEN from snap$ where vname='TEST_SNAP' and sowner='SCOTT';
Debra M. writes: "This is good stuff--will save time. Thanks!!"
For More Information
- What do you think about this tip? E-mail the Editor at firstname.lastname@example.org with your feedback.
- The Best Oracle Web Links: tips, tutorials, scripts, and more.
- Have an Oracle tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
- Ask your technical Oracle questions--or help out your peers by answering them--in our live discussion forums.
- Check out our Ask the Experts feature: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.