Hello, you are not logged in.  Login or sign up
Community >> Blogs
Search Toad World Search

Do you have a topic that you'd like discussed?  We'd love to hear from you.  Send us your idea for a blog topic.

Toad and Dynamic SQL Scripting
 
Location: Blogs Bert Scalzo's Blog    
 Bert Wednesday, July 09, 2008 7:02 AM
Sometimes, even in today’s world of super GUI’s, a SQL script is still the best way to do something. Plus there are always those people who steadfastly prefer command line to GUI’s no matter what. Can Toad accommodate and even facilitate these needs? Of course you know that the answer is a resounding yes.
 
Let’s say that we want to turn on and off the referential integrity within an entire schema – whose list of table names can and will change over time. Whether you use a GUI or a script, the mere fact that the list of table names can change adds an additional level of complexity or challenge. So just how can we write a one-time solution that will handle this dynamic list of table names?  As always, with Quest’s Toad, plus using the technique of dynamic SQL scripting.
 
Dynamic SQL scripting is a very old and powerful trick. You write a SQL script that will both generate and then execute the real SQL script. The primary enabling technologies have always been the Oracle data dictionary and certain SQL*Plus commands – namely SPOOL and EXECUTE. Since Toad has always strived to support a majority of SQL*Plus commands, we can use Toad to build and run all such dynamic SQL scripts. Plus Toad offers the world’s only SQL script debugger – which I’ll cover in next week’s blog.
 
Returning to our example – the scripts to dynamically turn on and off foreign keys for a schema, here is the script for the first half – turning off the referential integrity (the reverse solution left as an exercise):
-- ri_off.sql
set pagesize 0
set feedback off
set term off
spool c:\temp\ri_off.tmp
select 'alter table '||owner||'.'||table_name||' disable constraint
'||constraint_name||';'
 from user_constraints
 where constraint_type = 'R'
 and status = 'ENABLED';
spool off
set term on
set feedback on
set echo on
@c:\temp\ri_off.tmp
Below is this dynamic SQL file opened in Toad’s editor and then executed as a script (i.e. F5 – with output displayed in the script output tab). No matter how the schema may change (i.e. tables may be created or dropped), this dynamic SQL script should always function as intended. As stated earlier, the two primary enablers are the Oracle data dictionary and Toad’s support for SQL*Plus commands – especially SPOOL. The algorithm is pretty straight forward: disable formatting and feedback, spool output to a temporary file, query data dictionary to create the real SQL command for each row returned, terminate spooling, enable formatting and feedback, then finally execute the temporary file – which should now contain the dynamically correct SQL.
 
 
Permalink |  Trackback

Comment:
Add Comment   Cancel 
Search Blog Entries
 
Copyright 2008 by Quest Software  | Terms Of Use | Privacy Statement | Contact Us