Newsletters Subscriptions Forums Media Kit About Us Contact Search Home

mgo
OS/400 Edition
Volume 3, Number 68 -- October 8, 2003

More Conversion of Query/400 to SQL


Hey, Ted:

We do exactly the same thing here that Rich does to create Query Management queries. I came across one the other day I didn't know how to solve.

I had created result fields in the Query/400 query and changed the default size. When I converted the Query/400 query to a QM query and ran it, the output file it created was not the same. I discovered that the QM query was not using my result field size overrides. Since I was trying to use the result field to link to another file, I got no data from the secondary file. How can I define the size of a result field in a QM query?

--Ken


Don't try to define the size of the result field. Instead, create the file in advance. You can use your Query/400 query, or you can write DDS or SQL to build the file.

Edit the source member that was created when you ran the Retrieve Query Management Query (RTVQMQRY) command. You can delete all the junk that appears in the first few records, before the SELECT command begins. Then add an INSERT clause to the beginning of the source member. Here's an example of the edited query:

INSERT INTO SOMEFILE          
SELECT                              
  ALL       T01.ITEM, T02.IMDESC,   
            (S1*SALES01+S2*SALES02) AS YTD, T01.SALES01,
            T01.SALES02, T01.SALES03, T01.SALES04
  FROM      SALES T01,       
            IMIN T02,        
            SALESSLT T03     
  WHERE     T01.ITEM = T02.IMITNO   
    AND     T01.ITEM <> T03.SID     

Use the Create Query Management Query (CRTQMQRY) command to build the new QM query, but do not specify OUTPUT(*OUTFILE). You don't need to tell the name of the output file, because you've specified the file name in the INSERT clause.

Before you run the query, be sure to clear the output file first, if necessary.

For file processing, Query/400 is not robust enough for my taste. Converting Query/400 queries to QM queries is a good move. Query/400 is okay for reporting, especially ad hoc reporting. Query/400 makes a good prototyping tool; that is, you can use it to help determine the output someone desires, then convert the query to SQL.

I think that embedding the generated SQL in programs is an even better alternative than conversion to Query Management. Use RTVQMQRY to generate SQL for a Query/400 query, but don't use CRTQMQRY. Instead, place the generated SQL into a program and compile it.

If you decide to go that route, follow the SQL command with a test of the SQL state (SQLSTT) variable. If the SQL state is not equal to five zeros after the query executes, you can take appropriate action. Here's an example in RPG.

D QCMDEXC         pr                  extpgm('QCMDEXC') 
D   Command                     80a   const             
D   CommandLen                  15p 5 const             
                                                        
C/exec sql                                              
C+                                                      
C+  set option commit=*none,closqlcsr=*endmod           
C+                                                      
C/end-exec                                              
 /free                                                  
                                                        
    QCMDEXC('clrpfm join03b':80);                       
                                                        
 /end-free                                              
C/exec sql                                              
C+                                                      
C+  INSERT INTO SOMEFILE
C+  SELECT                                              
C+    ALL       T01.ITEM, T02.IMDESC,                         
C+              (S1*SALES01+S2*SALES02) AS YTD, T01.SALES01,  
C+              T01.SALES02, T01.SALES03, T01.SALES04         
C+    FROM      SALES T01,                                    
C+              IMIN T02,                                     
C+              SALESSLT T03                                  
C+    WHERE     T01.ITEM = T02.IMITNO                         
C+      AND     T01.ITEM <> T03.SID                           
C+                                                            
C/end-exec                                                    
 /free                                                        
                                                              
    if SQLSTT <> '00000';                                     
       // do something                                        
    endif;                                                    
    *inlr = *on;                                              
                                                              
 /end-free                                                    

--Ted


Sponsored By
INFORM DECISIONS

ELIMINATE THE COSTS OF PRE-PRINTED FORMS,
LABOR AND POSTAGE WITH inFORM Decisions iDocs™ 'Suite'

iSeries based e-Forms, e-Checks, e-Mail, e-FAX, and Document Retrieval
from the Web are available as individual modules or as a
complete e-Document processing 'Suite'.

Click to Download the Complete Suite or Individual Modules today
www.inFORMDecisions.com
or call (800) 858-5544



THIS ISSUE
SPONSORED BY:

Advanced Systems Concepts
inFORM Decisions


BACK ISSUES

TABLE OF
CONTENTS

Display QCDRCMDD XML Output Using XSLT Stylesheet

More Conversion of Query/400 to SQL

Reader Feedback and Insights: Spool Files and SQL


Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris

Managing Editor
Shannon Pastore

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.