Tip

How to modify the snapshot structure without dropping the table

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

Then:

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

    Requires Free Membership to View

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';

Reader Feedback

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 tdichiara@techtarget.com 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.

This was first published in May 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.