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 > How to write a script to drop a schema

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-19-09, 15:15
wayneb64 wayneb64 is offline
Registered User
 
Join Date: Aug 2003
Posts: 58
How to write a script to drop a schema

Since DB2 will not allow dropping a schema with anything in it, I would like to make a little script to save me the pain and agony of clearing it out by hand every time. I tried this:

BEGIN ATOMIC
FOR V AS
SELECT TABNAME FROM SYSCAT.TABLES
WHERE TABSCHEMA='DIE_SCHEMA'
DO DROP TABLE TABNAME;
END FOR;
END@

but of course that would be to easy. It fails because DROP TABLE is not DML.

So how does one automate this all to common yet painful task?
Reply With Quote
  #2 (permalink)  
Old 03-19-09, 15:18
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Use a cursor, fetch loop and dynamic SQL.

Andy
Reply With Quote
  #3 (permalink)  
Old 03-19-09, 17:53
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
There is a stored procedure that will do that for you:

connect to sample;
call SYSPROC.ADMIN_DROP_SCHEMA('DB2INST1',NULL,'ERROR', 'DROP_ERROR');


This assumes that the database is sample and the schema to drop is 'DB2INST1'

There is also a SP to copy one schema name to another:

call SYSPROC.ADMIN_COPY_SCHEMA('DB2INST1','DB2INST2,'CO PY',NULL,NULL,NULL,'ERROR','ADMIN_COPY_ERRORS');

do a DB2 infocenter search for more info on these and other useful SP's.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 03-19-09, 18:00
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
For some reason the word 'COPY' displays as 'CO PY' in the second command and I can't fix it (looks fine in edit mode).
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #5 (permalink)  
Old 03-20-09, 03:20
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I would also use the ADMIN_DROP_SCHEMA procedure that Marcus mentioned for the simple reason that not only table objects are an issue but you could also have routines (stored procs or UDFs), triggers, or other things in the schema, which may have cross-dependencies and need to be dropped in a specific order.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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