Hello, you are not logged in.  Login or sign up
Toad on Twitter Follow Toad Search Toad World Search
Blogger List   

All Recent Blog Entries
 

Johannes Ahrends
Unicode and Toad

Ben Boise
Toad SC Discussions

Kevin Dalton
Benchmark Factory

Steven Feuerstein
PL/SQL Obsession

Devin Gallagher
Toad SC discussions

Stuart Hodgins
JProbe Discussions

  Henrik "Mauritz" Johnson
Toad Tips & Tricks on the "other" Toads
  Mark Kurtz
Toad SC discussions
  Michael Lumbard
Toad SC discussions
Daniel Norwood
Toad for Data Analysts,
Toad Extension for Visual Studio
Debbie Peabody
Toad for Data Analysts
Gary Piper
Toad Reports Manager
John Pocknell
Toad for Oracle, JProbe
Kuljit Sangha
Toad SC discussions
Bert Scalzo Indicates Oracle ACE status
Toad for Oracle, Data Modeling, Benchmarking
Jeff Smith
Toad product family
Richard To
SQL Optimization
Jim Wankowski
DB2 - LUW and z/OS
John Weathington
  Toad World Editor
Toad World issues

  Toad Data Modeler Opens in a new window
Data Modeling
 
  Real Automated Code Testing for Oracle
Quest Code Tester blog

Blogs
Toad and Database Commentaries

Toad World blogs are a mix of insightful how-tos from Quest experts as well as their commentary on experiences with new database technologies.  Have some views of your own to share?  Post your comments!  Note:  Comments are restricted to registered Toad World users.

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
Search Blog Entries
 
Copyright 2010 by Quest Software  | Terms Of Use | Privacy Statement | Contact Us