Automatically Generating SQL Statements


In this last section, I ll briefly show you a technique of writing SQL statements that produce other SQL statements. This is very useful and can save you a lot of typing when writing SQL statements that are similar. One simple example is a SQL statement that produces DROP TABLE statements that remove tables from a database. The following query produces a series of DROP TABLE statements that drop the tables in the store schema:

  SELECT 'DROP TABLE '  table_name  ';'   FROM user_tables;  'DROPTABLE'TABLE_NAME';' ------------------------------------------ DROP TABLE COUPONS; DROP TABLE CUSTOMERS; DROP TABLE EMPLOYEES; DROP TABLE PRODUCTS; DROP TABLE PRODUCT_TYPES; DROP TABLE PROMOTIONS; DROP TABLE PURCHASES; DROP TABLE PURCHASES_TIMESTAMP_WITH_TZ; DROP TABLE PURCHASES_WITH_LOCAL_TZ; DROP TABLE PURCHASES_WITH_TIMESTAMP; DROP TABLE SALARY_GRADES; 
Note  

user_tables contains the details of the tables in the user s schema. The table_name column contains names of the tables.

You can spool the generated SQL statements to a file and use them later.




Oracle Database 10g SQL
Oracle Database 10g SQL (Osborne ORACLE Press Series)
ISBN: 0072229810
EAN: 2147483647
Year: 2004
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net