 




<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Sql in RPGLE</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/itanswers/sql-in-rpgle/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-in-rpgle/</link>
	<description></description>
	<lastBuildDate>Sun, 19 May 2013 00:45:31 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: jplamontre</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-in-rpgle/#comment-37071</link>
		<dc:creator>jplamontre</dc:creator>
		<pubDate>Fri, 22 Apr 2005 06:06:13 +0000</pubDate>
		<guid isPermaLink="false">#comment-37071</guid>
		<description><![CDATA[look at http://jplamontre.free.fr/AS400/SQL2SYLK.htm, there is a SQL PREPARE inside (search for &quot;C+ prepare k1prepa from : mySFW&quot;
note : SYLK is the text format of excel spreadsheets

look at http://www.iseriesnetwork.com/artarchive/index.cfm?fuseaction=viewNewsletterArticle&amp;webID=1001&amp;newsID=5012&amp;issueID=5176&amp;articleID=50775,
there is a complete sample

here is a copy:

RPG and SQL: The Dynamic Duo
by Aaron Bartell 
Club Tech iSeries Programming Tips Contributor 



April 18, 2005 ?   

  



In the April 14th issue of the Club Tech iSeries Programming Tips newsletter, I introduced you to RPG and its ability to embed SQL. In this tip, I will expound on that first SELECT statement and show you the power of SQL?s ability to accept a dynamically built SELECT statement. I will show you how to pass parameters that tell the SQL statement to sort a customer credit code list by any field in the CUSTMST file. For review, here is the CUSTMST file structure:

A          R CUSTMST                   TEXT(&#039;CUSTOMER MASTER FILE&#039;) 
A            NAM           30A         TEXT(&#039;NAME&#039;)                 
A            ADR1          50A         TEXT(&#039;ADDRESS1&#039;)             
A            ADR2          50A         TEXT(&#039;ADDRESS2&#039;)             
A            CTY           50A         TEXT(&#039;CITY&#039;)                 
A            STT            2A         TEXT(&#039;STATE&#039;)                
A            ZIP            5A         TEXT(&#039;ZIP&#039;)                  
A            CTRY          50A         TEXT(&#039;CTRY&#039;)                 
A            PHN           11A         TEXT(&#039;PHN&#039;)                  
A            CRDCOD         1A         TEXT(&#039;CREDIT CODE&#039;)          

Here is a rudimentary example of a credit code file that will be combined into the SQL result set along with the CUSTMST file:

A          R CRDCODMST                 TEXT(&#039;CREDIT CODE MASTER FILE&#039;)
A            CRDCOD         1A         TEXT(&#039;CREDIT CODE&#039;)            
A            DSCR          50A         TEXT(&#039;DESCRIPTION&#039;)    

Like I stated above, the objective is to make the final result set dynamically sortable based on, for example, what a user clicks on. In addition to that requirement, it would be realistic for an end user to want a description of the credit code instead of a cryptic single character code. The following example is not tied to a user interface but instead allows you to call it from the command line as follows:

CALL CUSTLIST2 (&#039;cust.nam&#039;) 
Instead of using an *ENTRY PLIST to receive the parameter, this program uses a prototype. The code for the CUSTLIST2 program follows:

h dftactgrp(*no)                                               
                                                               
d sqlResult       ds                                           
d  custNam                      30a                            
d  crdCodDscr                   50a                            
                                                               
d CUSTLIST2       pr                                           
d  ordby                        10a                            
d CUSTLIST2       pi                                           
d  ordby                        10a                            
                                                               
d sql             s            256a                            

 /free                                                         
   sql = &#039;SELECT cust.nam, crd.dscr&#039; +                         
         &#039; FROM custmst AS cust, crdcodmst AS crd&#039; +           
         &#039; WHERE cust.crdcod = crd.crdcod&#039; +                   
         &#039; ORDER BY &#039; + ordby;                                 
 /end-free 
                                                    
C/exec sql                                                     
C+ PREPARE S1 FROM :sql                   
C/end-exec                                
                                          
C/exec sql                                
C+ DECLARE C1 CURSOR FOR S1               
C/end-exec                                
                                          
C/exec sql                                
C+ OPEN C1                                
C/end-exec                                
 /free                                    
                                          
  dou sqlcod  0;                        
    exsr fetchNxt;                        
    if sqlcod  0;                       
      iter;                               
    endif;                                
                                          
    dsply crdCodDscr;          
  enddo;                                  
  exsr closeCur;                               
                                               
  *inlr = *on;                                 
                                               
  //----------------------------------------   
  // Get the next record from the result set   
  //----------------------------------------   
  begsr fetchNxt;                              
 /end-free                                     
C/exec sql                                     
C+ FETCH NEXT FROM C1 INTO :sqlResult          
C/end-exec                                     
 /free                                         
  endsr;                                       
                                               
  //----------------------------------------   
  // Close the SQL Cursor                      
  //----------------------------------------   
  begsr closeCur;                              
 /end-free                                     
C/exec sql        
C+ CLOSE C1       
C/end-exec        
 /free            
  endsr;            

One thing that you will notice is different from last week?s example is that the sqlResult data structure has replaced the custDs data structure. CustDs was a mirror image of the CUSTMST file, but SqlResult is not. The reason for this can be found in the SELECT statement where it only asks for two fields ?- the customer name and the credit code description.

There are four things that I want to point out in the SELECT statement above:

a) &quot;FROM custmst AS cust, crdcodmst AS crd&quot; states that files custmst and crdcodmst are to be used in this SELECT statement and they are to have alias names of cust and crd, respectively. This allows you to make specific references to fields in the files without naming collisions. For example, the CRDCOD field from each file can be referred to independently as CRD.CRDCOD and CUST.CRDCOD.

b) Look at the code &quot;SELECT cust.name, crd.dscr&quot;. This code states that I want to select two specific fields rather than all fields. I only want the customer name and credit code description fields. Note that the dot is used to qualify which file and field is being referenced, much like using the QUALIFIED keyword on an RPG data structure.

c) &quot;WHERE cust.crdcod = crd.crdcod&quot; states that there should be a join between files CUSTMST and CRDCODMST where the credit codes are equal to one another. There are other ways to &quot;join&quot; two tables together, but this is a simple way to show you a join for the first time.

d) &quot;&#039;ORDER BY&#039; + ordby&quot; builds the string that will be used to order the sql result set. Note that ordby is passed into the program as a parameter and in this example will contain the value &#039;cust.nam&#039;.

The above article was written by Aaron Bartell. If you have any questions about this article, contact Aaron at aaronbartell@gmail.com.
]]></description>
		<content:encoded><![CDATA[<p>look at <a href="http://jplamontre.free.fr/AS400/SQL2SYLK.htm" rel="nofollow">http://jplamontre.free.fr/AS400/SQL2SYLK.htm</a>, there is a SQL PREPARE inside (search for &#8220;C+ prepare k1prepa from : mySFW&#8221;<br />
note : SYLK is the text format of excel spreadsheets</p>
<p>look at <a href="http://www.iseriesnetwork.com/artarchive/index.cfm?fuseaction=viewNewsletterArticle&#038;webID=1001&#038;newsID=5012&#038;issueID=5176&#038;articleID=50775" rel="nofollow">http://www.iseriesnetwork.com/artarchive/index.cfm?fuseaction=viewNewsletterArticle&#038;webID=1001&#038;newsID=5012&#038;issueID=5176&#038;articleID=50775</a>,<br />
there is a complete sample</p>
<p>here is a copy:</p>
<p>RPG and SQL: The Dynamic Duo<br />
by Aaron Bartell<br />
Club Tech iSeries Programming Tips Contributor </p>
<p>April 18, 2005 ?   </p>
<p>In the April 14th issue of the Club Tech iSeries Programming Tips newsletter, I introduced you to RPG and its ability to embed SQL. In this tip, I will expound on that first SELECT statement and show you the power of SQL?s ability to accept a dynamically built SELECT statement. I will show you how to pass parameters that tell the SQL statement to sort a customer credit code list by any field in the CUSTMST file. For review, here is the CUSTMST file structure:</p>
<p>A          R CUSTMST                   TEXT(&#8216;CUSTOMER MASTER FILE&#8217;)<br />
A            NAM           30A         TEXT(&#8216;NAME&#8217;)<br />
A            ADR1          50A         TEXT(&#8216;ADDRESS1&#8242;)<br />
A            ADR2          50A         TEXT(&#8216;ADDRESS2&#8242;)<br />
A            CTY           50A         TEXT(&#8216;CITY&#8217;)<br />
A            STT            2A         TEXT(&#8216;STATE&#8217;)<br />
A            ZIP            5A         TEXT(&#8216;ZIP&#8217;)<br />
A            CTRY          50A         TEXT(&#8216;CTRY&#8217;)<br />
A            PHN           11A         TEXT(&#8216;PHN&#8217;)<br />
A            CRDCOD         1A         TEXT(&#8216;CREDIT CODE&#8217;)          </p>
<p>Here is a rudimentary example of a credit code file that will be combined into the SQL result set along with the CUSTMST file:</p>
<p>A          R CRDCODMST                 TEXT(&#8216;CREDIT CODE MASTER FILE&#8217;)<br />
A            CRDCOD         1A         TEXT(&#8216;CREDIT CODE&#8217;)<br />
A            DSCR          50A         TEXT(&#8216;DESCRIPTION&#8217;)    </p>
<p>Like I stated above, the objective is to make the final result set dynamically sortable based on, for example, what a user clicks on. In addition to that requirement, it would be realistic for an end user to want a description of the credit code instead of a cryptic single character code. The following example is not tied to a user interface but instead allows you to call it from the command line as follows:</p>
<p>CALL CUSTLIST2 (&#8216;cust.nam&#8217;)<br />
Instead of using an *ENTRY PLIST to receive the parameter, this program uses a prototype. The code for the CUSTLIST2 program follows:</p>
<p>h dftactgrp(*no)                                               </p>
<p>d sqlResult       ds<br />
d  custNam                      30a<br />
d  crdCodDscr                   50a                            </p>
<p>d CUSTLIST2       pr<br />
d  ordby                        10a<br />
d CUSTLIST2       pi<br />
d  ordby                        10a                            </p>
<p>d sql             s            256a                            </p>
<p> /free<br />
   sql = &#8216;SELECT cust.nam, crd.dscr&#8217; +<br />
         &#8216; FROM custmst AS cust, crdcodmst AS crd&#8217; +<br />
         &#8216; WHERE cust.crdcod = crd.crdcod&#8217; +<br />
         &#8216; ORDER BY &#8216; + ordby;<br />
 /end-free </p>
<p>C/exec sql<br />
C+ PREPARE S1 FROM :sql<br />
C/end-exec                                </p>
<p>C/exec sql<br />
C+ DECLARE C1 CURSOR FOR S1<br />
C/end-exec                                </p>
<p>C/exec sql<br />
C+ OPEN C1<br />
C/end-exec<br />
 /free                                    </p>
<p>  dou sqlcod  0;<br />
    exsr fetchNxt;<br />
    if sqlcod  0;<br />
      iter;<br />
    endif;                                </p>
<p>    dsply crdCodDscr;<br />
  enddo;<br />
  exsr closeCur;                               </p>
<p>  *inlr = *on;                                 </p>
<p>  //&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
  // Get the next record from the result set<br />
  //&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
  begsr fetchNxt;<br />
 /end-free<br />
C/exec sql<br />
C+ FETCH NEXT FROM C1 INTO :sqlResult<br />
C/end-exec<br />
 /free<br />
  endsr;                                       </p>
<p>  //&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
  // Close the SQL Cursor<br />
  //&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
  begsr closeCur;<br />
 /end-free<br />
C/exec sql<br />
C+ CLOSE C1<br />
C/end-exec<br />
 /free<br />
  endsr;            </p>
<p>One thing that you will notice is different from last week?s example is that the sqlResult data structure has replaced the custDs data structure. CustDs was a mirror image of the CUSTMST file, but SqlResult is not. The reason for this can be found in the SELECT statement where it only asks for two fields ?- the customer name and the credit code description.</p>
<p>There are four things that I want to point out in the SELECT statement above:</p>
<p>a) &#8220;FROM custmst AS cust, crdcodmst AS crd&#8221; states that files custmst and crdcodmst are to be used in this SELECT statement and they are to have alias names of cust and crd, respectively. This allows you to make specific references to fields in the files without naming collisions. For example, the CRDCOD field from each file can be referred to independently as CRD.CRDCOD and CUST.CRDCOD.</p>
<p>b) Look at the code &#8220;SELECT cust.name, crd.dscr&#8221;. This code states that I want to select two specific fields rather than all fields. I only want the customer name and credit code description fields. Note that the dot is used to qualify which file and field is being referenced, much like using the QUALIFIED keyword on an RPG data structure.</p>
<p>c) &#8220;WHERE cust.crdcod = crd.crdcod&#8221; states that there should be a join between files CUSTMST and CRDCODMST where the credit codes are equal to one another. There are other ways to &#8220;join&#8221; two tables together, but this is a simple way to show you a join for the first time.</p>
<p>d) &#8220;&#8216;ORDER BY&#8217; + ordby&#8221; builds the string that will be used to order the sql result set. Note that ordby is passed into the program as a parameter and in this example will contain the value &#8216;cust.nam&#8217;.</p>
<p>The above article was written by Aaron Bartell. If you have any questions about this article, contact Aaron at <a href="mailto:aaronbartell@gmail.com">aaronbartell@gmail.com</a>.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Database Caching 3/10 queries in 0.038 seconds using memcached
Object Caching 267/273 objects using memcached

Served from: itknowledgeexchange.techtarget.com @ 2013-05-19 01:53:34 -->