fhg
Volume 6, Number 41 -- November 15, 2006

Create Database Files from SQL Queries

Published: November 15, 2006

Hey, Ted:

While using interactive SQL/400 from a green-screen session, I can direct the output of an SQL command to a database file. Is there a way to direct the output of a dynamically created SQL command to a database file from a batch job?

--Rich


Yes, you have a few options. First, let's review the steps you use in your interactive session, for the benefit of the readers.

From the interactive session, press F13 to access the Services panel. From there select option 1 (change session attributes), and enter 3 at the SELECT output prompt. Fill in file name, library, and member name, and choose option 1 to indicate that you wish to create a file. Press Enter twice to return to the Enter SQL Statements panel, then run the SQL statement. A handy feature sometimes, but not available in batch mode.

One way to solve your challenge is to embed your select command within the Create Table command. Here's a short RPG program--QAD1021--that I wrote to illustrate how this works. (If you try this out, create a module from this source, then bind this module to the assert module, which was previously published in this newsletter, to create a program.)

H option(*srcstmt: *nodebugio) 

D QAD1021         pr                                       
D  inSqlCmd                    512a                        
D QAD1021         pi                                       
D  inSqlCmd                    512a                        
                                                           
D/copy prototypes,assert                                   
                                                           
C/exec sql                                                 
C+     set option commit=*none,closqlcsr=*endmod 
C/end-exec                                                 
                                                           
C/free                                                     
      *inlr = *on;                                         
 /end-free                                                                
C/exec sql                                                                
C+                                                                        
C+      execute immediate :inSqlCmd    
C+                                                                        
C/end-exec                                                                
C/free                                                                    
     assert (SqlStt < '02000': 'Failed with status ' + Sqlstt); 
     return;                                                              
 /end-free                                                                
Here's an example Create Table command you might pass to this program:
create table mylib/somefile as                       
  ( SELECT c.CUSNUM, c.LSTNAM, c.INIT,                 
           c.STREET, c.CITY, s.Name, c.ZIPCOD, c.baldue
      FROM qcustcdt as c                          
      LEFT JOIN states as s                      
        ON c.state = s.abbr                            
     WHERE c.baldue >= 250                             
  ) with data

Voilą! You now have a new physical file on the system.

Shops that don't have the SQL Development Kit might prefer to use Query Management instead. Create a source physical file to hold the SQL source.

CRTSRCPF FILE(MYLIB/QQMQRYSRC) RCDLEN(91)

Use some mechanism, such as an RPG program, to write the SQL command to a member the source file. You will only need the Select statement, not Select embedded within Create Table, because Query Management can write to an outfile.

SELECT c.CUSNUM, c.LSTNAM, c.INIT,                 
       c.STREET, c.CITY, s.Name, c.ZIPCOD, c.baldue
  FROM qcustcdt as c                          
  LEFT JOIN states as s                      
    ON c.state = s.abbr                            
 WHERE c.baldue >= 250                             

Create the Query Management query. (In this example, the SQL source is in member MYQRY.)

CRTQMQRY QMQRY(MYLIB/MYQRY) SRCFILE(MYLIB/QQMQRYSRC)

Then run the query. Name the database file in the OUTFILE parameter, and be sure to specify OUTPUT(*OUTFILE).

STRQMQRY QMQRY(MYLIB/MYQRY) OUTPUT(*OUTFILE) OUTFILE(MYLIB/SOMEFILE)

If you have other, non-IBM SQL software products, you may have other ways to create database files from SQL commands.

--Ted



Sponsored By
T.L. ASHFORD

BARCODE400 by T.L. Ashford is the easiest, fastest way to create and print Compliance Labels directly from the AS/400 and iSeries.

Ashford's comprehensive library of Compliance formats is available to Barcode400 users. AIAG labels for Ford and Motorcraft, GM, and many more are available.

BARCODE400 is backed by the best Technical Support Team in the industry.

FREE Guide to Bar Code Labeling
www.tlashford.com
or call 800.541.4893



Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

Patrick Townsend & Associates:  Alliance AES/400 - database field encryption
New Generation Software:  Leading provider of iSeries BI and financial management software
COMMON:  Join us at the Annual 2007 Conference & Expo, April 29 - May 3, in Anaheim, California

 


 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement