If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > need way to automatically SET INTEGRITY on all tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-22-10, 14:01
craigmc craigmc is offline
Registered User
 
Join Date: Aug 2003
Location: austin,tx
Posts: 90
need way to automatically SET INTEGRITY on all tables

I am trying to automate re-setting the integrity on all user tables after an APPLY cycle. The problem we have is there are many clients running db2 Personal Edition on laptops. They connect in to synch periodically, which runs a replication capture-apply cycle. Depending on how long since their last synch, ASNLOAD may be invoked which leaves some tables in CHECK PENDING state.

Anyway, I had thought to use the SYSPROC.ADMIN_CMD procedure to execute commands I could dynamically generate. For example:
CALL SYSPROC.ADMIN_CMD('SET INTEGRITY FOR myschema.mytable IMMEDIATE CHECKED')

Then I could make a stored proc that determines which tables are in CHECK PENDING and execute the command on all of them.
SELECT * FROM syscat.tables WHERE OWNERTYPE = 'U' AND TYPE = 'T' AND STATUS = 'C' ORDER BY TABSCHEMA, TABLEID

This same type thing works for RUNSTATS and other commands, but for some reason not for SET INTEGRITY (or I am overlooking something).

Any idea why? Or better, anyone have a way to call SET INTEGRITY dynamically like this?

This is for DB2 UDB 9.7 on Windows by the way..

Thanks.

Last edited by craigmc; 02-22-10 at 14:12.
Reply With Quote
  #2 (permalink)  
Old 02-22-10, 14:12
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
You don't need ADMIN_CMD to execute SET INTEGRITY, because the latter is an SQL statement, which you can execute dynamically from your SP. You do realize that you must SET INTEGRITY in certain sequence, accounting for table dependencies, don't you?
Reply With Quote
  #3 (permalink)  
Old 02-22-10, 14:20
craigmc craigmc is offline
Registered User
 
Join Date: Aug 2003
Location: austin,tx
Posts: 90
I guess I am unfamiliar then with how to execute dynamic SQL from a DB2 Stored Proc (native sql). If I have the stmt I want to execute in a variable of type varchar, how to I invoke it? Or am I missing something?

As to the second part, yes I do realize that. In my case I always create the parent tables before child tables in any scripts I execute, so ordering by TABLEID should accomplish keeping the order straight, although I can see that it is possible to hit error cases there. I haven't explored a more precise way to determine the dependency ordering.. If you know a better way, I'd love to hear it.

Thanks for the reply.
Reply With Quote
  #4 (permalink)  
Old 02-22-10, 14:26
craigmc craigmc is offline
Registered User
 
Join Date: Aug 2003
Location: austin,tx
Posts: 90
looks like I found my answer..
SET toexec = 'SET INTEGRITY FOR ' || tablename || ' IMMEDIATE CHECKED ';
PREPARE stmt FROM toexec;
EXECUTE stmt ;
Reply With Quote
  #5 (permalink)  
Old 02-22-10, 14:40
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
You can write a recursive query to obtain the relationships or
you can run the set integrity on the tables in a loop till there are no tables in C state...

i prefer to adopt the latter option...
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #6 (permalink)  
Old 02-22-10, 14:57
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by sathyaram_s View Post
You can write a recursive query to obtain the relationships or
you can run the set integrity on the tables in a loop till there are no tables in C state...
These approaches may not work in all cases, as frequently there are cyclical dependencies between tables. In such circumstances the only option is to specify all dependent tables in a single SET INTEGRITY statement, but the SQL to determine such tables is not trivial. Lenny might be interested in lending a hand, unless he did win his jackpot already...
Reply With Quote
  #7 (permalink)  
Old 02-22-10, 15:43
craigmc craigmc is offline
Registered User
 
Join Date: Aug 2003
Location: austin,tx
Posts: 90
I suppose you could loop through the results of:
SELECT * FROM syscat.tables WHERE OWNERTYPE = 'U' AND TYPE = 'T' AND STATUS = 'C' ORDER BY TABSCHEMA, TABLEID
or something similar to determine all CHECK PENDING objects, and build up a single statement that includes them all in a comma separated list. Then execute that..
Assuming the limits on the PREPARE and EXECUTE calls are not too small to hold a potentially large statement.
In my case I just have straight foreign key relationships to deal with, and no circular dependencies though.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On